nginx tries to use named location as real file path?

Nick asked:

I’m trying to proxy_pass to an Apache2 instance using a named location, but Nginx tries to find the named location on disk?

Nginx Config:

root /var/www;

location / {
   try_files $uri @apache =404;

location @apache {
   proxy_intercept_errors on;
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;

Trying to access a file that doesn’t exist should proxy_pass, but instead fails with a 404 and the log shows:

trying to use file: "@apache" "/var/www@apache"

It’s a named location, so why is it trying to read it as a file path?

My answer:

The named location needs to appear last in try_files. You should not have anything past it (and indeed, it would never be reached anyway).

try_files $uri @apache;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.