-1

For a node.js project I'm doing, I have a tree like this.

├── public
│   ├── components
│   ├── css
│   └── img
├── routes
└── views

Essentially, I have the root to be set to public. I want all requests destined to

/components/
/css/
/img/

To check to see if their appropriate destinations exist on disk. However, I don't want requests to other directories to even run an IO operation,

/foo/asdf
/bar
/baz/index.html

None of those should result in the disk being touched.

I have a stansa that does the proxy to node.js,

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

I just would like to know how to arrange this. My problem would be easily solved if try_files took a single argument, but it always wants a file first.

location /components/ { try_files $uri @proxy }
location /css/ { try_files $uri @proxy }
location /img/ { try_files $uri @proxy }

However, there is nothing that I can find that will give me,

location / { try_files @proxy }

How do I get the effect I want?

Evan Carroll
  • 2,373
  • 10
  • 34
  • 53
  • 1
    There are no commas in the `try_files` directive! Your configuration will not work with commas. – VBart Nov 11 '13 at 16:01
  • 1
    btw, you don't need `internal` for named location. It's already `internal` by design. – VBart Nov 11 '13 at 16:11

2 Answers2

0

I'm not 100% sure this is right, but this seems to work

location /components/ { try_files $uri / }
location /css/ { try_files $uri / }
location /img/ { try_files $uri / }

location @proxy {
  internal;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://localhost:3030;
  proxy_redirect off;
}
Evan Carroll
  • 2,373
  • 10
  • 34
  • 53
0

Don't hesitate to duplicate some parts of configuration. It's nginx way to make the configuration simple and obvious.

server {
    listen 80;
    server_name example.com

    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;
    proxy_redirect off;

    root /path/to/public;

    location /components/ { try_files $uri @proxy; }
    location /css/ { try_files $uri @proxy; }
    location /img/ { try_files $uri @proxy; }

    location / {
        proxy_pass http://localhost:3030;
    }

    location @proxy {
        proxy_pass http://localhost:3030;
    }
}

But will be much better to separate static and dynamic content:

server {
    listen 80;
    server_name example.com

    root /path/to/public;

    location /components/static/ { }
    location /css/ { }
    location /img/ { }

    location / {
        proxy_pass http://localhost:3030;

        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;

        proxy_redirect off;
    }
}
VBart
  • 8,309
  • 3
  • 25
  • 26