I have a working php fastcgi config in nginx. Now I have few requests to handle such as /X.php
by /X
, /Y.php
by /Y
Sounds simple so I wrote the following for testing:
rewrite ^/X.php$ /api/v1/stat last;
it was caught by php application's 404 handler. here is the nginx error log with rewrite_log on
[notice] 15289#0: *759 "^/X.php$" matches "/X.php", client: 10.0.0.12, server: example.com, request: "GET /X.php HTTP/1.1", host: "example.com"
[notice] 15289#0: *759 rewritten data: "/api/v1/stat", args: "", client: 10.0.0.12, server: example.com, request: "GET /X.php HTTP/1.1", host: "example.com"
[notice] 15289#0: *759 "^/X.php$" does not match "/index.php", client: 10.0.0.12, server: example.com, request: "GET /X.php HTTP/1.1", host: "example.com"
If I visit /api/v1/stat directly, it works:
[notice] 15125#0: *708 "^/X.php$" does not match "/api/v1/stat", client: 223.104.3.248, server: example.com, request: "GET /api/v1/stat HTTP/1.1", host: "example.com"
[notice] 15125#0: *708 "^/X.php$" does not match "/index.php", client: 223.104.3.248, server: example.com, request: "GET /api/v1/stat HTTP/1.1", host: "example.com"
Here is my related nginx config:
rewrite ^/X.php$ /api/v1/stat last;
location / {
try_files $uri $uri/ /index.php;
}
location ~ .*\.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
upstream php {
server unix:/var/run/php-fpm.sock;
}
Please help me come up a solution. thanks.
NOTE
A little background that may help you understand my problem.
It is probably obvious that the following should work and it does:
rewrite ^/X.php$ /api/v1/stat permanent;
However, I am dealing with some kind of hardware that doesn't understand 301 redirect, so I am trying to do with internal redirect without revealing the redirect.
UPDATE 1
The solution I came up is a half only. I got /X.php
redirect to /api/v1/stat
fine. However, no parameters can be passed after the redirect. I think rewrite
will pass along the $args
but it doesnt work. I am at a loss now..
UPDATE 2
Problem solved. though I am still not sure why $args
is not passing along.
Best Answer
rewrite ^/X.php$ /api/v1/stat last; sends you to "location /"
for example you can use another location for matching this internal redirect: "/api/v1/stat"
In this location you can pass request to fcgi-backend (or additionally modify it in this location).