Nginx – Multiple node.js apps on the same server

nginxnode.js

I have nginx hosts an instance of static site generated by jekyll.

I added a nodejs app on port 4000 and do according to this tutorial on DigitalOcean

server {
  listen    80;

  root /usr/share/nginx/html/example.com;
  index index.html index.htm;

  server_name example.com;
  charset utf-8;

  location /nodeapp/ {
    proxy_pass http://localhost:4000/;
    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 app loads but all the static resources get 404 because it looks for resource at example.com/nodeapp/css/main.css for example.

How would i fix this?

Best Answer

You could add the following before any proxy logic and store assets in a directory elsewhere on the machine:

location /static {
    alias /location/for/all/static/assets;
}

Then, in your app make "/static" the prefix for all static assets.