skip to Main Content

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


  1. Chosen as BEST ANSWER

    Changing "last" flags to "permanent" works. But changing fastcgi param REQUEST_URI to $uri$is_args$args not:

    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 REQUEST_URI $uri$is_args$args;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }
    }
    

    From console output it looks like that still the original request uri is passed to cgi server instead the rewritten one: enter image description here


  2. You are rewriting (with rewrite) from /dataview/unknownvvvo to /backend/unknown-vvvo, then rewriting (with try_files) from /backend/unknown-vvvo to /index.php. Your index.php script will use the values defined in fastcgi_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 of last on the end of the rewrite statements.

    rewrite ^/dataview/unknownvvvo(.*)$ /backend/unknown-vvvo$1 permanent;
    rewrite ^/dataview/servicecounter(.*)$ /backend/servicecounter-events$1 permanent;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search