There are two question marks in the URL after nginx_rewrite

There are two question marks in the URL after nginx_rewrite

A normal url http://localhost/cmpt/document/list?name=sjh

Then after php receives

var_dump($_GET) 
array(
    name => sjh
)

In order to unify application entry files, many php frameworks will configure rewriting in the location module of nginx. Now nginx adds rewriting rules to unify the entry of all requests as index.php

location / {
    if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php?$1 last;
    }
}

...

location ~ \.php(.*)$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

According to my understanding, at this time

  • Original url http://localhost/cmpt/document/list?name=sjh
  • URL after rewrite http://localhost/index.php?cmpt/document/list?name=sjh

However, there will be two in the same URL, so ?how does PHP parse the real from this URL query_params.

On Baidu online, a netizen explained as follows:

  1. Nginx will only put the URL in the regular expression for rewrite? Match the previous part
  2. After the match is complete,? The following content will be automatically appended to the url (including?). If you do not want the latter content to be appended, please add it at the end? Can
  3. If you want to live? Please use $query_string for the following content

Tips: Here is a reminder that when debugging, do not use break last in the last configuration item of rewrite. Use redirect to see the converted address.

So, hurry up and I will configure a wave

location / {
    if (!-e $request_filename) {
        rewrite ^/(.*)  /index.php?$1 redirect;
    }
}

sudo nginx -s reload + Refresh the page and get

  • Original url: http://local.vss.vhall.com/cmpt/document/list?name=adadf
  • redirect Url : http://local.vss.vhall.com/index.php?cmpt/document/list&name=adadf

Really, the two ?parts are merged by nginx. The result printed on the php side.

array(
    'cmpt/document/list' => '',
    'name'  => 'adadf',
)

That netizen is really not bad.

Guess you like

Origin blog.csdn.net/qq_30549099/article/details/109295739