3

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).

  • 1
    I have a "work around", where I use a "location = / {...proxy details}" as well as the other two. Of course this will not serve any index file I place in the static directory (which I would do when the app is down for maintenance for example). – Gauss Jul 16 '13 at 13:11
4

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;
}
| improve this answer | |
-1

First sorry for my bad english, I think I undestand your problem And you need to do some think like this:

Define fist your static contect:

location ~ \.(jpg|png|etc)$ {

root  /web/sites/this.site.tld/static;
}

And then make a proxy pass to your node js server

location / {
    proxy_pass http://127.0.0.1:3000;
  }

Or your method: 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{

Or something like that.

| improve this answer | |
  • Sorry, but this doesn't answer the question – Gauss Jul 16 '13 at 13:09

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.