Configuring nginx to check for hard files in only a few directories,

Posted by Evan Carroll on Server Fault See other posts from Server Fault or by Evan Carroll
Published on 2013-11-11T08:38:06Z Indexed on 2013/11/11 9:59 UTC
Read the original article Hit count: 189

Filed under:
|

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?

© Server Fault or respective owner

Related posts about nginx

Related posts about node.js