Question

NginX - php location block wrongfully takes request

Posted February 4, 2018 1.3k views
NginxUbuntu 16.04

I have a php script to be executed outsite of my server root. As far as I read I need to define a location block with a different root to execute the php correctly.

Problem: NginX always takes the last php location block from my configuration and NOT the nested one under my location /de/forums nor the location /de/forums/(.+.php).

What am I doing wrong? How can I convince nginx to take a location block with the correct root defined?

As far as I understood the hierarchy - the more specific block should be used… please help!

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name domain.com;

    root /var/www/domain.com/html;
    index index.php;


    location ^~ /de/forums {
            root /var/www/domain.com/;
            #alias /var/www/domain.com/de/forums/;

            index index.php index.html index.htm;
            try_files $uri $uri/ /de/forums/index.php?$uri&$args;


                location ~* \.php(/|$) {
                    fastcgi_pass  unix:/var/run/php/php7.2-fpm.sock;
                    fastcgi_split_path_info ^(.+\.php)(/.*)$;
                    include fastcgi.conf;   
                    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                }
    }

    location ^~ /de/forums/(.+\.php) { 
        root /var/www/domain.com/;     
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        include fastcgi.conf;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    }   



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

    location ~ \.php$ {
        include fastcgi.conf;
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;

    }

}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi @ceo779382,

The issue is because the parent location is a regex location while the nested locations are prefix locations.

You may only define nested prefix locations when the parent location is also a prefix location:

location /a {
        location /a {
               # You can also skip this location and just write
               # your code directly under the parent location
        }
        location /a/b {
        ...
        }
}

When a parent location is defined by a regex, any nested locations must also be defined by regexes:

location ~ ^/(a|b) {
        location ~ ^/a {
        ...
        }
        location ~ ^/b {
        ...
        }
}

However, you may also define nested regex locations when the parent location is a prefix location:

location /a/b {
        location ~ /a {
        ...
        }
        location ~ /b {
        ...
        }
}

Hope that helps.

Regards,
KDSys

Submit an Answer