How to config Nginx runs php on sub directories?

Davuz asked:

I have 2 versions of portfolio website for 2 languages. I put them in 2 dir in the same root dir.


Then I config nginx as below:

server {
    listen 8080;

    access_log /u01/projects/company/log/access.log main;
    error_log /u01/projects/company/log/error.log;
    set $rootLocation /u01/projects/company/;

    root $rootLocation;
    index index.php;

    charset utf-8;
    client_max_body_size 100m;

    location ~* /(vi|en)/admin(/|/.*\.php)$ {
       try_files $uri $uri/;
       gzip on;
       index index.php;
       include fastcgi_params;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $rootLocation$fastcgi_script_name;

    include common.conf;#some fastcgi config

Look at location, I have admin/ dir (and run php directly here) for every version. But when I access, browser always redirect to What is wrong with this configuration? How to fix it?

My answer:

The problem is here:

       try_files $uri $uri/;

By doing this you explicitly told it to redirect to index.php. A trailing / in a try_files means to try the value of the index directive.

In a PHP-FPM location this should instead be:

       try_files $uri =404;

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.