Report this

What is the reason for this report?

NginX - php location block wrongfully takes request

Posted on February 4, 2018

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;
		
	}
	
}


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.