nginx路由匹配规则
常见的路由匹配符号有:
=
:精确匹配^~
:精确前缀匹配~
:区分大小写的正则匹配;~*
:不区分大小写的正则匹配/uri
:普通前缀匹配/
:通用匹配
1 | location = / { |
请求URI | 匹配路由规则 |
---|---|
http://localhost/ | 规则A |
http://localhost/login | 规则B |
http://localhost/register | 规则F |
http://localhost/static/a.html | 规则C |
http://localhost/static/files/a.txt | 规则X |
http://localhost/a.png | 规则D |
http://localhost/a.PNG | 规则E |
http://localhost/img/a.gif | 规则D |
http://localhost/img/a.tiff | 规则Y |
同优先级精确度越高, 优先级越高
同级别的定义顺序越靠前, 优先级越高
反向代理配置
访问 http://xxx/a 时1
2
3
4
5
6
7
8location /a {
root /data/www/html;
add_header Cache-Control no-store;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.1.100:80;
}
1 | location /a/ { |
则转到了 http://192.168.1.100:80/, 注意规则里的两个/
1 | location /proxy/ { |
1 | location /proxy/ { |
1 | location /proxy/ { |
1 | location /proxy/ { |
负载均衡配置
1 | upstream groups1 { |