Nginx – 403 error; won’t proxy: NginX and Node.js

nginxnode.js

I currently have a setup where all static files are served by NginX, and if no static file is found it is moved onto a node.js server. Unfortunately I am getting a 403 error for the root request. Everything else is working fine.

server {
    listen *:80;
    error_page 404 = /404.html;
    root /web/sites/this.site.tld/static;
    index home;

    location / {
        try_files $uri $uri/ @proxy;
    }

    location @proxy {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_X_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://thisSiteApp;
        proxy_redirect off;
    }
}

upstream thisSiteApp{
    server 127.0.0.1:3000;
}

I am fairly sure the problem lies with the index directive. Placing a "index.html" file in static and changing the directive to "index index.html" works. But what I really want to do is make it look to the node server for the index location, while still serving my static files without looking at node.

NB. If I put the proxy settings under the / location the node server serves the root just fine (well without the static files, but it serves its part fine).

Best Answer

Your try_files directive has a forward slash, so nginx will try to serve the directory when just going to the bare root of example.org/.

Instead of:

location / {
    try_files $uri $uri/ @proxy;
}

use:

location / {
    try_files $uri @proxy;
}