3

Relevant configuration:

location /myapp {
    root /home/me/myapp/www;
    try_files $uri $uri/ /myapp/index.php?url=$uri&$args;

    location ~ \.php {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include       /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}

I absolutely have a file foo.html in /home/me/myapp/www but when I browse to /myapp/foo.html it is handled by PHP, the final fallback in the try_files list.

Why is this happening?

1 Answer 1

4

If you browse to /myapp/foo.html, Nginx checks the file at /home/me/myapp/www/myapp/foo.html. When it doesn't find it there, it goes on to the fallback in the try_files list.

If you need Nginx to check the file at /home/me/myapp/www/foo.html, then you'd need to use alias instead of root, in your use case. Here's the solution...

location /myapp {
    alias /home/me/myapp/www;
    try_files $uri $uri/ /myapp/index.php?url=$uri&$args;

    location ~ \.php {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include       /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }
}

Note that there is a longstanding bug that alias and try_files don't work together. Also, note that using alias may break other parts of your configuration, such as PHP processing!

2
  • 1
    argh, so basically I should just use Apache. Oct 23, 2013 at 17:06
  • 1
    @WellsOliver: yep, if you want to have to use much heavier hardware for the same kind of lifting. I prefer servers that have evolved to solve the C10K problem, though. YMMV. Nginx has its shortcomings (as do lighttpd and Apache and essentially any software), but Apache doesn't beat Nginx when it comes to efficiency. Sep 20, 2016 at 8:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .