Search code examples
nginxurl-rewritinguriproxypass

Rewrite rules without changing uri in Nginx


How can I rewrite an uri without returning a redirect in the process in Nginx ? The rewrite result is in the same host.

Exemple: rewrite "mysite.com/foo" returning the same result as "mysite.com/bar", but we dont change the uri in the process.

It's kinda like a proxy_pass but for the same host.


Solution

  • You can proxy_pass to host+uri.

    location  ~ ^/foo/(.*)$ {
        include proxy_params;
        proxy_pass   http://127.0.0.1/bar/$1;
    }
    

    Or rewrite and proxy_pass should work:

    location  ~ ^/foo {
        rewrite ^foo(.*) /bar$1
        proxy_pass   http://example.com;
    }
    

    I hope any of those 2 works for you.