jjunsa
jjunsa

Reputation: 47

nginx location directive is prepended to url path

I'm setting up an alias within a location directive "/folder". The alias points to "/my/alias/path".

When I navigate to a URL within the location, e.g. "mydomain.com/folder/destination", the directive name is being prepended to the request, which resolves to "/my/alias/path/folder/destination" (undesired) instead of "/my/alias/path/destination" (desired).

I'm probably missing something or don't quite understand how location and aliases work.

I've tried adding forward slashes to the location directive as well as the alias, but that doesn't work either.

Here's my location directive:

location ^~ /folder {
    alias /my/alias/path;

    index index.php index.html index.htm;

    location ~ ^/(README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {
        deny all;
    }

    location ~ ^/(bin|SQL)/ {
         deny all;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /.well-known/acme-challenge {
         allow all;
    }
}

Here's what I see in my error.log

/my/alias/path/folder/destination/index.php (No such file or directory)

Upvotes: 1

Views: 290

Answers (1)

Richard Smith
Richard Smith

Reputation: 49722

The error relates to a .php file, and is probably generated by php-fpm because the value of SCRIPT_FILENAME is set incorrectly.

Inside snippets/fastcgi-php.conf you are probably setting SCRIPT_FILENAME to $document_root$fastcgi_script_name, which is incompatible with the alias directive. Use $request_filename instead.

For example:

location ^~ /folder {
    alias /my/alias/path;

    ...

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

By placing the fastcgi_param statement after the include statement, the new value will silently overwrite the incorrect value.

Upvotes: 1

Related Questions