nginx configuration for multiple subdirectories containing zf2/php files doesn't work

Evils asked:

My root structure looks like this

/root/
/root/projectA/…
/root/projectB/…
/root/projectC/…

Before I configured each of my subdirectories with one extra location block in my nginx file. These location “blocks” looked like this:

server {
listen   80;
listen   [::]:80;

root /usr/share/root/projectA/public/;
index index.php index.html index.cgi;

server_name www.test.de;

location / {        
    try_files $uri $uri/ /index.php?$args;      
}

location ~ .php$ {
    fastcgi_split_path_info ^(.+.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;       
    fastcgi_index index.php;
    include fastcgi_params;
}

}

This way my ZF2 application residing in /root/projectA worked as expected. But as the number of subprojects grow, I wanted to use an generic regex solution so I don’t have to write the same block over and over again for each subdirectory.

So this was my approach to make those projects available under a subdomain vendor.test.de with each project available under vendor.test.de/projectA/…:

server {
listen   80;
listen   [::]:80;

root /usr/share/root/;
index index.php index.html index.cgi;

server_name vendor.test.de;

rewrite_log on;
error_log /var/log/nginx/debug.log debug;

location ~ ^/(.+)/ {
    root /usr/share/root/$1/public;
    try_files $uri $uri/ $1/public/index.php?$args; 

    location ~ .php$ {
        fastcgi_split_path_info ^(.+.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;       
        fastcgi_index index.php;
        include fastcgi_params;
    }   
}

}

But all I get is a 404 and it’s just not working.
I debugged the log files a bit, and it seems to behave strange with trying to get /usr/share/root/index.php instead of /usr/share/root/projectA/public/index.php even though he found the right location first…

Any ideas on this?!

Thanks in advance!

My answer:


In your location you need to use alias instead of root.

    alias /usr/share/root/$1/public/;

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.