nginx rewrite command memo

rewite

Under the server block, the rewrite part will be executed first, and then it will match 
the rewrite break and last in the location block server. There is no difference, both will match the location, so there is no need to use last to initiate a new request, you can leave it blank

rewirte in location:

Don't write last and break - then the process is to execute these rewrites in sequence 
1. rewrite break

  After the url is rewritten, the current resource is used directly, and the remaining statements in the location are no longer executed to complete the request. The url in the address bar remains unchanged. 

2. rewrite last

  After the url is rewritten, immediately initiate a new request, enter the server block again, and retry the location matching, if more than 10 matches fail to report a 500 error, the url in the address bar remains unchanged. 

3. rewrite redirect

  Return 302 temporary redirection, the address bar displays the redirected url, the crawler will not update the url (because it is temporary) 

4. rewrite permanent

  Returns 301 permanent redirection, the address bar displays the redirected url, and the crawler updates the url

Using last will re-request the server tag

If the rewrite in the location is a request for static resources, no other matching is required. Generally, you need to use break or no write, and directly use the data source in the current location to complete this request 
. If the rewrite in the location, you need to perform other For processing, such as dynamic fastcgi requests (. PHP , .jsp), etc., use last to continue to initiate new requests 
(it is better to use last for the root location, because if there are fastcgi requests such as .php, it will continue to be processed)

Use alias to specify source: last must be used

The if statement is mainly used to judge some conditions that cannot be directly matched in the rewrite statement, such as detecting the existence of a file, http header, cookies, etc.

location matching rules and priorities

  1. = exactly matches this query. If found, stop searching.
  2. ^~ matches the prefix of the path, and stops searching if found.
  3. ~ is a case-sensitive regular match
  4. ~* is a case-insensitive match 
    precedence: =, ^~, ~/~*, none

break statement

Put it in front of the rewrite statement in the server block. 
If you directly request a real file, use the break statement to stop the rewrite check. 

if (-f $request_filename) { 
    break;
}

From:https://www.cnblogs.com/fengchi/p/6525021.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325979498&siteId=291194637