Reprinted from: rewrite command (break, last, redirect, permanent) in Nginx
rewite
Under the server block, the rewrite part will be executed first, and then the location block will be matched
There is no difference between rewrite break and last in the server. They 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:
Do not write last and break - then the process is to execute these in sequence rewrite
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. This request is completed, and the address bar url 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 the match fails for more than 10 times, a 500 error will be reported, and the address bar url remains unchanged
3. rewrite redirect – returns 302 temporary redirect, the address bar displays the redirected url, and the crawler will not update the url (because it is temporary)
4. rewrite permanent - returns 301 permanent redirect, the address bar displays the redirected url, and the crawler updates the url
Using last will re-initiate a request for the server tag
If the rewrite in the location is a request for static resources, no need For other matching, generally use break or do not write, directly use the data source in the current location to complete this request
If after rewriting in the location, other processing needs to be performed, such as dynamic fastcgi requests (.php, .jsp), etc., you need to 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 the source: you must use last
if statement is mainly used to determine some conditions that cannot be directly matched in the rewrite statement , such as detecting the existence of files, http headers, cookies, etc.
location matching rules and priorities
break statement
is placed in front of the server block rewrite statement
If you are directly requesting a real file, use the break statement to stop the rewrite check
if (-f $request_filename) {
break;
}
The above introduces the rewrite instructions break, last, redirect, and permanent in Nginx, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.