0

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: e.g. '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.

Bejlem
  • 1
  • 6

1 Answers1

1

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;
    }

}
Stamos
  • 3,938
  • 1
  • 22
  • 48
  • Do I have to set the proxy settings for every location that uses JavaScript and if so does each location need to have a different port? – Bejlem Dec 22 '17 at 10:30
  • No there is no need. You should start from reading "what is" and "how it works" because you understanding is superficial. – Stamos Dec 22 '17 at 10:49
  • Well look's like I should take a look at that next year. ^^ It seems to me, that it would be much easier to remove nginx and install apache to let the javascripts run on the server side. This would let me achieve my goal maybe a bit easier for now. – Bejlem Dec 22 '17 at 13:31