Why doesn't nginx like my $args?

Jonathan asked:

I have a WordPress site located at mysite.com/blog, the files of which are located in /var/www/mysite/wordpress. Why, when I visit mysite.com/blog/test, does my nested location block only get hit if I remove $args from try_files?

server {
    server_name mysite.com;

    location /blog {
        alias /var/www/mysite/wordpress;
        try_files $uri $uri/ /blog/index.php$args;

        location ~ \.php {
            # This is only reached if I remove $args from try_files
            return 401; # For testing purposes 

Here is the associated error in the log:

"/usr/share/nginx/html/index.php" failed (2: No such file or directory)

My answer:

Because you forgot the ? between index.php and $args. The convenience variable $is_args contains a ? when necessary, and is empty when there was no query string.

try_files $uri $uri/ /blog/index.php$is_args$args;

Of course, with WordPress you do not need to use $args at all, as it gets the query string elsewhere:

try_files $uri $uri/ /blog/index.php;

is sufficient.

It appears you are using try_files with alias directive. That’s a long standing bug in nginx, use root directive instead.

View the full question and any other answers on Server Fault.

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