starts with = to indicate an exact match For example, A only matches requests at the end of the root directory, and cannot be followed by any string. The beginning of
^~ means that the uri starts with a regular string, not a regular match. The beginning of
~ means a case-sensitive regular match; the beginning of
~* means a case-insensitive regular match.
/ universal matching , if there is no other match, any request will match the
order no priority: (location =) > (location full path) > (location ^~ path) > (location ~,~* regular order) > (starting path of location part) > (/)The above matching result According to the above location writing method, the following matching example is established:
/ -> config A Exact and complete match, even if /index .html can’t be matched either
/downloads/download.html -> config B After matching B, there is no match going on. Use B
/images/1.gif -> configuration D matching F, going Match down to D, stop going down
/images/abc/def -> config D The longest match is up to G, match down to D, stop going down You can see that anything starting with /images/ will match Go to D and stop. It makes no sense to write FG here. H will never be turned. This is just to illustrate the matching order
/documents/document.html -> config C matches C, and nothing below. For any match, use C
/documents/1.jpg -> configuration E to match C, and go down to regular match to E
/documents/Abc.jpg -> config CC The longest match is to C, go down The regular sequence matches to CC, not to E
Actual usage suggestions所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。#这里是直接转发给后端应用服务器了,也可以是一个静态首页# 第一个必选规则
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器#非静态文件请求就默认是动态请求,自己根据实际把握#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
proxy_pass http://tomcat:8080/
}
http://tengine.taobao.org/book/chapter_02.html http://nginx.org/en/docs/ http/ngx_http_rewrite_module.htmlRewrite rulesThe rewrite function is to use the global variables provided by nginx or the variables set by yourself, combined with regular expressions and flags to implement url rewriting and redirection. rewrite can only be placed in server{}, location{}, if{}, and can only work on the string after the domain name except for the passed parameters, such as http://seanlook.com/a/we/ index.php?id=1&u=str Only rewrites /a/we/index.php. Syntaxrewrite
regex replacement [flag];If relative domain names or parameter strings work, you can use global variable matching, or you can use proxy_pass reverse proxy. It shows that the functions of rewrite and location are somewhat similar, both can achieve jumps. The main difference is that rewrite changes the path to obtain resources within the same domain name, while location controls access or reverse proxy to a type of path. You can proxy_pass to other machines. In many cases, rewrite will also be written in location, and their execution order is:
Execute the rewrite instruction of the server block
Execute location matching
Execute the rewrite instruction in the selected location
If the URI in one of the steps is rewritten, re-loop and execute 1-3 until the real file is found ;If the loop exceeds 10 times, a 500 Internal Server Error will be returned. flag flag
last : Equivalent to Apache’s [L] flag, indicating completion of rewrite
break : Stop executing the subsequent rewrite instruction set of the current virtual host
redirect : Return 302 temporary reset Orientation, the address bar will display the address after the jump
permanent: Return to 301 permanent redirection, the address bar will display the address after the jump
Because 301 and 302 cannot simply return status codes, they must also have Redirected URL, this is why the return command cannot return 301,302. The difference between last and break here is a bit difficult to understand:
last is generally written in server and if, while break is generally used in location
last does not terminate After rewriting, the URL matches, that is, the new URL will be retrieved from the server Go through the matching process, and break terminates the rewritten matching
break and last can organize the continued execution of the subsequent rewrite instructions
if instructions and global variables if judgment instructions The syntax is if (condition) {...}, judge the given condition. If true, the rewrite instruction within the curly braces will be executed, and the if condition (condition) can be anything as follows:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn