nginx configuration location summary and rewrite rule writing

Grammar rules: location [=|~|~*|^~] /uri/ { … }
= At ​​the beginning, it means an exact match
^~ At the beginning, it means that the uri starts with a regular string, which can be understood as matching the url path. nginx does not encode URLs, so the request is /static/20%/aa, which can be matched by the rule ^~ /static/ /aa (note the space).
~ starts with case-sensitive regular matching
~* starts with case-insensitive regular matching
!~ and !~* are case-sensitive mismatch and case-insensitive mismatch regular
/universal matching, any request will match arrive.
In the case of multiple location configurations, the matching order is (from the reference material, it has not been verified yet, just try it, you don't have to be rigid, it is for reference only):
first match =, second match ^~, and then follow the order in the file The regular matching of , and finally handed over to / general matching. When there is a successful match, stop matching and process the request according to the current matching rules.
For example, there are the following matching rules:
location = / {
   #ruleA
}
location = /login { #ruleB
   }
location
^~ /static/ { #ruleC
   }
location
~ \.(gif|jpg|png|js|css) $ {
   #Rule D
}
location ~* \.png$ {
   #Rule E
}
location !~ \.xhtml$ { #rule
   F
}
location !~* \.xhtml$ { #rule
   G
}
location / {
   #rule H
}
Then the effect is as follows:
access the root directory/, such as http://localhost/  Will match rule A
access  http://localhost/login  will match rule B, http://localhost/register  will match rule H
access  http://localhost/static/a.html  will match rule C
access  http://localhost /a.gifhttp://localhost/b.jpg  will match rule D and rule E, but rule D will take precedence, rule E will not work, and  http://localhost/static/c.png  will match first Rule C
accesses  http://localhost/a.PNG  to match rule E, but not rule D, because rule E is not case-sensitive.
Visit  http://localhost/a.xhtml will not match rule F and rule G, http://localhost/a.XHTML will not match rule G because it is not case sensitive. Rule F and rule G belong to the exclusion method, which matches the matching rules but will not match, so think about where they will be used in practical applications.
Visit  http://localhost/category/id/1111  and finally match rule H, because none of the above rules match, at this time, nginx should forward the request to the back-end application server, such as FastCGI (php), tomcat (jsp), nginx exists as a direction proxy server.


所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
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/
}

三、ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
2、下面是可以用作判断的全局变量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、Redirect语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn$&quot {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
五、防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
六、根据文件类型设置过期时间
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
七、禁止访问某个目录
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
一些可用的全局变量:
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query_string
$scheme
$server_protocol
$server_addr
$server_name
$server_port
$uri

Guess you like

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