简体   繁体   中英

Linux: Setting up node.js on nginx

I just got myself a new raspberry pi 3 and installed nginx with node onto it. The nginx webserver is up and running. But somehow I don't get the javascripts of my website work. Do I need to enable the javascript for each location of my webserver or is there the possibility to set node up in a way I just works for every site on my server?

Here you can see the directory hierarchy of my websites:

#########################################
###            Directories            ###
#########################################
# /www/var/                             #
# | - > index.html (the hub/mainpage)   #
# | proj1/                              #
# | | - > index.html (website 1)        #
# | proj2/                              #
# | | - > index.html (website 2)        #
# | | - > js/somescript.js              #
# | proj3/                              #
# | | - > index.html (website 3)        #
# | | - > js/anotherscript.js           #
# | proj4/                              #
# | | - > index.html (website 4)        #
# | proj5/                              #
# | | - > index.html (website 5)        #
# | | - > js/morescript.js              #
# | ...                                 #
#########################################

When you go to my website ( http://strtpg.xyz ) the mainpage out of /www/var/ is beeing shown. It's kind of a hub where I got access to all other sites hosted on the server. And you can access the different sites by appending it's folder name to the link: eg ' http://strtpg.xyz/proj1 ' or ' http://strtpg.xyz/proj5 '. Some of the sites got javascript and some don't.

And this is my config file from /etc/nginx/sites-available/ :

# Server configuration
#upstream pnkpnd {
#   server localhost:3420;
#   keepalive 8;
#}

server {
    listen 0.0.0.0:80;
    listen [::]:80;
    server_name localhost;
    index index.html index.htm;

    location / {
        root /var/www;
        try_files $uri $uri/ =404;
        # Default root of site won't exist.
        #return 410;
    }

    location /strtpg {
        root /var/www;
        try_files $uri $uri/ =404;
    }

    location /dshbrd {
        root /var/www;
        try_files $uri $uri/ =404;
    }

    location /pnkpnd {
        root /var/www;
        try_files $uri $uri/ =404;
#       proxy_http_version 1.1;
#       proxy_set_header Upgrade $http_upgrade;
#       proxy_set_header Connection 'upgrade';
#       proxy_set_header X-Real-IP $remote_addr;
#       proxy_set_header X-Forward-For $proxy_add_x_Forwarded_for;
#       proxy_set_header Host $http_host;
#       proxy_set_header X-NginX-Proxy true;
#       proxy_pass http://strtpg.xyz/;
#       proxy_redirect off;
    }

    location ~ /\.ht {
        deny all;
    }
}

All the stuff I tried out to get node working with my websites is commented out, so I can at least see the website itself.

Node is usually used as a Web server that listens to a port, serves static files and has API endpoints that will run javascript in the server. To achieve that developers use application frameworks like ExpressJS.

Pointing nginx to a directory like /var/www is wrong.

Usually you will have to run your NodeJS app like this

$ node app.js

After that you server will listen to a port. Lets say that in this case is 3000

so using curl http://localhost:3000/ will get your root site. Its your job from inside app.js to setup all your static and dynamic content that your application will serve. Example: if you want to send index.html you have to do a sendfile .

In the nginx side the only thing you will have to do is to create a reverse proxy.

Now nginx will proxy all requests that are made to example.com to your node application server at http://localhost:3000

server {
    listen 80;

    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM