nginx location and rewrite

Location has the meaning of positioning, and different positioning
syntax is performed according to uri:
location [=|~|~*|^~] patt { //Brackets can not write parameters, roughly divided into three categories
}

location = patt {} //exact match
location patt {} // normal match
location ~ patt{} //regular match

There can be multiple locations in a server, and the matching process is as follows:
1. First judge the exact match, if it hits, return immediately;

2. Then judge the normal match, if there is one, record the current match, if there are more than one, record the "most match" "Long" match; (only record, temporarily not returned);

3. Finally, judge the regular expression match, in the order from top to bottom of the configuration file, once matched, immediately return the current matching result, and end the match, if there is no match, Then return the result of ordinary matching

Note : ordinary matching has nothing to do with the order in the configuration file, regular matching is related to the order in the configuration file;
? After the first match is successful, the path in the uri is replaced with the matching result, and the Will it continue to match in the configuration file?

location / { // matches "/" in uri
}

rewrite rewrite, mainly used in server and location
syntax:
if space (condition) {} //Set the condition and rewrite it
set //set variable
return //return status code
break //Jump out of rewrite
rewrite //rewrite

There are 3 ways to write the condition:
1. "=" is equal to judge and used for string comparison
2. "~" regular expression matching, case-sensitive; plus asterisk ("~*") is not case-sensitive
3.- f -d -e judge whether it is a file, a directory, and whether it exists

Example :
if ($request_method=POST) {
  return 405;
}

if ($http_user_agent ~ MSIE) {
  rewrite ^.*$ /ie.html;
  break;
}

if (!-e $fastigc_script_name) {
  return 403;
}

Guess you like

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