Nginx - How to serve 404 and static files through private networking?

December 16, 2015 1.5k views
Nginx Node.js DigitalOcean DigitalOcean Articles Ubuntu

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

In order to serve static files from the Node Droplet, you would need to set up a web server on it as well. For instance, using Nginx, your configuration might look something like:

server {
        listen private.ipaddr:8080;
        server_name assets.domain.com;
        root /var/www/app/public;
        # You'd likely want to include some caching rules as well.
}

This will serve the static assets via your private IP address on port 8080. Then on your Nginx proxy Droplet, you could add a new location block like:

        location ^~ /assets/ {
                proxy_set_header X-Real-IP  $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass 10.132.61.120:8080;
        }

This directs all request for domain.com/assets to the Node Droplet's /var/www/app/public directory. Depending on how you've named things, you may need to tweak the paths, but hopefully this points you in the right direction!

Have another answer? Share your knowledge.