我是靠谱客的博主 现实果汁,这篇文章主要介绍nginx.conf location匹配规则讲解,现在分享给大家,希望可以做个参考。

location语法

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
location [=|~|~*|^~] /uri/ {} = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。 ~ 开头表示区分大小写的正则匹配 ~* 开头表示不区分大小写的正则匹配 !~!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则 / 通用匹配,任何请求都会匹配到 @ :内部服务跳转 1. =,精确匹配,一般是匹配某个具体文件 location = /index.html { proxy_pass https://www.douchuanwei.com/node.html; } * 精准匹配/,可以加快首页访问速度 2. ~,大小写敏感(正则表达式) location ~* /DCW/ { [规则] } #请求示例 #https://www.douchuanwei.com/DCW/ [成功] #https://www.douchuanwei.com/dcw/ [失败] 3. ~*,大小写忽略(正则表达式) location ~* /DCW/ { [ 规则 ] } # 则会忽略 uri 部分的大小写 #http://www.douchuanwei.com/DCW/ [成功] 可以成功匹配,但是目录中要DCW文件 #http://www.douchuanwei.com/dcw/ [成功] 可以成功匹配,但是目录中要dcw文件 4. ^~,只匹配以 uri 开头,匹配成功以后,会停止搜索后面的正则表达式匹配 location ^~ /img/ { [ 规则 ] } #以 /img/ 开头的请求,都会匹配上 #http://www.douchuanwei.com/img/dcw.jpg [成功] #http://www.douchuanwei.com/img/dcw.png [成功] 5. 匹配以gif、jpg、jpeg结尾的文件 location ~* .(gif|jpg|jpeg)$ { [ 规则 ] } #http://www.dcw.com/img/dcw.jpg [成功] 6. @,nginx内部跳转 location ^~ /data/ { error_page 404 @img_err; } location @img_err { [ 规则 ] } #以 /data/ 开头的请求,如果链接的状态为 404。则会匹配到 @img_err 这条规则上

同时有多个location时,优先级如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
location /img/ { echo " /img/"; } location ~ /img/ { echo "~ /img/"; } location ~* /img/ { echo "~* /img/"; } location ^~ /img/ { echo "^~ /img/"; } location = /img/ { echo "= /img/"; } 第一步:取出uri:/img/ 第二步:去匹配localtion规则,查找有没有 = /img/的规则,有则停止匹配。 第三步:将location = /img/规则注释,继续查找有没有 ^~ /img/的规则。 第四步:将 location ^~ /img/注释,这是它会去查找有没有正则匹配规则。 第五步:其他的都注释后,因为优先匹配规则都没有找到,最后匹配到 /img/规则。

location 正则匹配

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
. : 匹配除换行符以外的任意字符 ? : 重复0次或1+ : 重复1次或更多次 * : 重复0次或更多次 d :匹配数字 ^ : 匹配字符串的开始 $ : 匹配字符串的介绍 {n} : 重复n次 {n,} : 重复n次或更多次 [c] : 匹配单个字符c [a-z] : 匹配a-z小写字母的任意一个 (a|b|c):匹配a或b或c location ~* /a { return 999; } #匹配a-z的任意一个字母 location ~* ^/[a-z]$ { return 666; } #匹配/group1/ 或数字0-9的url前缀 比如 https://www.douchuanwei.com/group9/ 或 https://www.douchuanwei.com/group8/ 都可访问成功 location ~ ^/group(d+)/ { proxy_pass http://112.47.127.31:11036; } 小括号()之间匹配的内容,可以在后面通过$1来引用,$2表示的是前面第二个()里的内容。正则里面容易让人困惑的是转义特殊字符

全局变量

下面是可以用作if判断的全局变量

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
$args : #这个变量等于请求行中的参数,同$query_string $content_length : 请求头中的Content-length字段。 $content_type : 请求头中的Content-Type字段。 $document_root : 当前请求在root指令中指定的值。 $host : 请求主机头字段,否则为服务器名称。 $http_user_agent : 客户端agent信息 $http_cookie : 客户端cookie信息 $limit_rate : 这个变量可以限制连接速率。 $request_method : 客户端请求的动作,通常为GETPOST。 $remote_addr : 客户端的IP地址。 $remote_port : 客户端的端口。 $remote_user : 已经经过Auth Basic Module验证的用户名。 $request_filename : 当前请求的文件路径,由root或alias指令与URI请求生成。 $scheme : HTTP方法(如http,https)。 $server_protocol : 请求使用的协议,通常是HTTP/1.0HTTP/1.1。 $server_addr : 服务器地址,在完成一次系统调用后可以确定这个值。 $server_name : 服务器名称。 $server_port : 请求到达服务器的端口号。 $request_uri : 包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。 $uri : 不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。 $document_uri : 与$uri相同。 例: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:/var/www/html $request_filename:/var/www/html/test1/test2/test.php

匹配规则判断条件

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-f 和 !-f 用来判断是否存在文件 -d 和 !-d 用来判断是否存在目录 -e 和 !-e 用来判断是否存在文件或目录 -x 和 !-x 用来判断文件是否可执行 if ($request_method = POST) { return 405; } //如果提交方法为POST,则返回状态405(Method not allowed)。return不能返回301,302 if ($slow) { limit_rate 10k; } //限速,$slow可以通过 set 指令设置 if (!-f $request_filename){ break; proxy_pass http://127.0.0.1; } //如果请求的文件名不存在,则反向代理到localhost 。这里的break也是停止rewrite检查 if ($args ~ post=140){ rewrite ^ http://example.com/ permanent; } //如果query string中包含"post=140",永久重定向到example.com location ~* .(gif|jpg|png|swf|flv)$ { valid_referers none blocked www.jefflei.com www.leizhenfang.com; if ($invalid_referer) { return 404; } //防盗链 } 案例:根据文件类型设置过期时间 location ~* .(js|css|jpg|jpeg|gif|png|swf)$ { if (-f $request_filename) { expires 1h; break; } }

rewrite实例

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
http { # 定义image日志格式 log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status; # 开启重写日志 rewrite_log on; server { root /home/www; location / { # 重写规则信息 error_log logs/rewrite.log notice; # 注意这里要用‘’单引号引起来,避免{} rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*).(png|jpg|gif)$' /data?file=$3.$4; # 注意不能在上面这条规则后面加上“last”参数,否则下面的set指令不会执行 set $image_file $3; set $image_type $4; } location /data { # 指定针对图片的日志格式,来分析图片类型和大小 access_log logs/images.log mian; root /data/images; # 应用前面定义的变量。判断首先文件在不在,不在再判断目录在不在,如果还不在就跳转到最后一个url里 try_files /$arg_file /image404.html; } location = /image404.html { # 图片不存在返回特定的信息 return 404 "image not foundn"; } } 对形如/images/ef/uh7b3/test.png的请求,重写到/data?file=test.png,于是匹配到location /data,先看/data/images/test.png文件存不存在,如果存在则正常响应,如果不存在则重写tryfiles到新的image404 location,直接返回404状态码

例2:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
rewrite ^/images/(.*)_(d+)x(d+).(png|jpg|gif)$ /resizer/$1.$4?width=$2&height=$3? last; 对形如/images/bla_500x400.jpg的文件请求,重写到/resizer/bla.jpg?width=500&height=400地址,并会继续尝试匹配location 这个location location ~ ^/(.+).3gp.zip$ { # access_by_lua_file "/opt/pro/nginx/lua/zip_access.lua"; rewrite_by_lua_file "/opt/pro/nginx/lua/zip_access.lua"; } 匹配 http://dcw.html/video/3gp/dcw/4555QQQww.3gp.zip?&end=5 .代表. 其中是转义字符。 单独的.代表 匹配除换行符以外的任意字符 +匹配重复1次或更多次

本内容来源于小豆包,想要更多内容请跳转小豆包 》

最后

以上就是现实果汁最近收集整理的关于nginx.conf location匹配规则讲解的全部内容,更多相关nginx.conf内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(76)

评论列表共有 0 条评论

立即
投稿
返回
顶部