Reputation: 20654
I am trying to migrate to Nginx + PHP-FPM
from Apache + mod_php
. I am currently using mod_rewrite
to rewrite some virtual URIs ending in .php
to actual PHP scripts. This works perfectly when using mod_php
. But with with Nginx + FPM
, since we have to use proxy_pass
, this is not working. When I add a regex location block to match .php
extension, it gets highest precedence, and my rewrite rules are not applied.
How can I resolve this?
location /test/ {
rewrite "^/test/([a-z]+).php$" test.php?q=$1 last;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
set $fastcgi_script_name_custom $fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
set $fastcgi_script_name_custom "/cms/index.php";
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
Upvotes: 3
Views: 203
Reputation: 49702
You can place the rewrite
rule above your location
blocks within the context of the server
block. Or you can place the rewrite
rule within the location
block that matches the URI.
So you can use either this:
rewrite "^/test/([a-z]+).php$" /test.php?q=$1 last;
location / { ... }
location ~ [^/]\.php(/|$) { ... }
Or this:
location / { ... }
location ~ [^/]\.php(/|$) {
rewrite "^/test/([a-z]+).php$" /test.php?q=$1 break;
...
}
Notice that the rewritten URI needs a leading /
(unlike Apache convention).
See this document for details.
Upvotes: 1