0

I've followed this tutorial exactly to set up, successfully my Node.Js app: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-14-04

My Node app lives on a separate droplet, as does my Nginx. I'm redirecting all my www and http traffic to https://domain.com. This all WORKS.

Now I also want to serve custom error files. But they are located in the node app on the other droplet, in the public folder /var/www/app/public. Is there anyway I can serve my 404 file from Nginx, but keep it on the Node.JS droplet?

Same question for my static files (images/fonts), is there a way I can serve my static files from Nginx, but keep them on my Node.js droplet (NOT copy them over to Nginx droplet?)

Thanks

server {
    listen 80;
    server_name www.domain.com;
    return 301 https://domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name www.domain.com;

    return 301 https://domain.com$request_uri;

    ssl_certificate /etc/nginx/ssl/www.domain.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/www.domain.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    ssl_prefer_server_ciphers on;
}

server {
    listen 443 ssl;
    server_name domain.com;

    ssl_certificate /etc/nginx/ssl/www.domain.com.chained.crt;
    ssl_certificate_key /etc/nginx/ssl/www.domain.com.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
    ssl_prefer_server_ciphers on;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location / {
      proxy_pass http://ip_of_node_droplet:4000;
      proxy_redirect off;
      proxy_http_version 1.1;
    }
}
0

Two ideas come to my mind:

  • You could setup a service like NFS on the droplet hosting the files (server 1) in question. After this, export the directory containing the files (via NFS) and mount it on your machine hosting nginx (server 2).

  • You could set up nginx as well on your server 1,.

To make this work, you would have to alter your nginx config on server 2. Include location directives for your static files and error pages; maybe something along the lines of:

location /static {
  root /srv/nfs-imported;
}

error_page 404 /custom_404.html;
location = /custom_404.html {
  root /srv/nfs-imported;
  internal;
}

error_page 500 502 503 504 /custom_50x.html;
location = /custom_50x.html {
  root /srv/nfs-imported;
  internal;
}

This is just an example, and assumes, you're going for my first outlined idea. In case you're more interested in the second proposal, you wouldn't use root directives, but instead proxy_pass, like you're using already for all requests which should go to your node app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.