I'm having an issue with Nginx and URL rewriting. By default, the Plesk generated nginx.conf file for each vhost contains the following block:
I have a situation where when I receive any requests to http://www.example.com/cat(whatever), they need to be re-written to http://www.example.com/index.php?cat=(whatever). So I added the following block using 'Additional nginx directives':
Now if I try to request any of the following URLs, this works exactly as expected:
But as soon as I add the trailing slash, I start getting a 404 error:
The error log tells that this is because corresponding index.html files cannot be found. Here is one example:
So it looks like as soon as nginx hits a URL that matches the 'location ~ /$', it abandons looking at the other location blocks. So I have 2 questions here:
1. According to the nginx docs, prefix matches (i.e. location /cat) should be processed BEFORE any regex matches (i.e. location ~ /$). So this is unexpected behavior. Shouldn't nginx look at location /cat first?
2. Commenting out the 'location ~ /$' block in the nginx.conf file fixes the problem. However, this file is automatically generated by Plesk. Plus, I don't want to disable it for all requests, only requests directed to 'http://www.example.com/cat(whatever)'. How can I override this behavior in my 'Additional nginx directives'?
Your help is much appreciated.
Code:
location ~ /$ {
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}
I have a situation where when I receive any requests to http://www.example.com/cat(whatever), they need to be re-written to http://www.example.com/index.php?cat=(whatever). So I added the following block using 'Additional nginx directives':
Code:
location /cat {
try_files $uri /index.php?cat=$uri;
}
Now if I try to request any of the following URLs, this works exactly as expected:
Code:
http://www.example.com/cat-test
http://www.example.com/cat/test
http://www.example.com/cat/test/test
http://www.example.com/cat/t
But as soon as I add the trailing slash, I start getting a 404 error:
Code:
http://www.example.com/cat/
http://www.example.com/cat/test/
http://www.example.com/cat/test/test/
http://www.example.com/cat/t/
The error log tells that this is because corresponding index.html files cannot be found. Here is one example:
Code:
2017/03/21 00:32:49 [error] 25711#0: *20566477 "/var/www/vhosts/example.com/httpdocs/cat/test/index.html" is not found (2: No such file or directory), client: <ip redacted>, server: example.com, request: "GET /cat/testing/ HTTP/1.1", host: "www.example.com"
So it looks like as soon as nginx hits a URL that matches the 'location ~ /$', it abandons looking at the other location blocks. So I have 2 questions here:
1. According to the nginx docs, prefix matches (i.e. location /cat) should be processed BEFORE any regex matches (i.e. location ~ /$). So this is unexpected behavior. Shouldn't nginx look at location /cat first?
2. Commenting out the 'location ~ /$' block in the nginx.conf file fixes the problem. However, this file is automatically generated by Plesk. Plus, I don't want to disable it for all requests, only requests directed to 'http://www.example.com/cat(whatever)'. How can I override this behavior in my 'Additional nginx directives'?
Your help is much appreciated.