Home > Back-end >  How to redirect subpaths in nginx with nginx rewrite module?
How to redirect subpaths in nginx with nginx rewrite module?

Time:03-04

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: enter image description here

  • Related