I have a set of url paths which having changed in my application, e.g.:
- /dataview/unknownvvvo/ => /backend/unknown-vvvo
- /dataview/servicecounter => /backend/servicecounter-events
I try to address this with rewrite rules in nginx:
user app app;
error_log /dev/stderr debug;
events {}
http {
include mime.types;
default_type application/octet-stream;
rewrite_log on;
charset utf-8;
server {
listen 80;
listen [::]:80;
root /app/app/public;
index index.php index.html;
rewrite ^/dataview/unknownvvvo(.*)$ /backend/unknown-vvvo$1 last;
rewrite ^/dataview/servicecounter(.*)$ /backend/servicecounter-events$1 last;
location / {
try_files $uri /index.php$is_args$args;
}
location /index.php {
fastcgi_pass php-fpm_cms:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
But when I call ‘http://localhost/dataview/unknownvvvo’ in the browser for example, the nginx error log tells me the rewrite was successful, but it gives me a 404 error:
nginx_err
According to the nginx docs this should work, so what did I miss here?
2
Answers
Changing "last" flags to "permanent" works. But changing fastcgi param REQUEST_URI to $uri$is_args$args not:
From console output it looks like that still the original request uri is passed to cgi server instead the rewritten one:
You are rewriting (with rewrite) from
/dataview/unknownvvvo
to/backend/unknown-vvvo
, then rewriting (with try_files) from/backend/unknown-vvvo
to/index.php
. Yourindex.php
script will use the values defined infastcgi_params
(probably REQUEST_URI) to determine the requested path – which of course, has not been changed.The simplest solution is to use an external redirect, using
permanent
instead oflast
on the end of therewrite
statements.