How to change $request_uri in nginx?

2019-02-21 23:21发布

问题:

We get information from $_SERVER['REQUEST_URI'] not from $_GET or $_POST.

I want to define $request_uri to change /example to /module/controller/action. Please note that I do not want to trigger a redirect.

I tried the code below to do this, but it doesn't work.

location /example {
    rewrite /module/controller/action;  
}

回答1:

set $request_url $request_uri;
if ($request_uri ~ ^/example(.*)$ ) {
    set $request_url /module/controller/action;
}


location ~ \.php$ {

    fastcgi_pass   127.0.0.1:9090;

    #include        fastcgi.conf;
    fastcgi_param  REQUEST_URI        $request_url;
    #fastcgi_param  REQUEST_URI        $request_uri;
}