nginx匹配规则说明
2011-05-13 11:06
591 查看
Nginx的Location可以有以下几个匹配:
1. = 严格匹配这个查询。如果找到,停止搜索。
2. ^~ 匹配路径的前缀,如果找到,停止搜索。
3. ~ 为区分大小写的正则匹配
4. ~* 为不区分大小写匹配
例子:
location = / {
# matches the query / only.
# 只匹配 / 查询。
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
[ configuration D ]
}
我的添加模式,动静分离
location ^~ /(images|scripts|styles|upload)/ {
root /www/abc.com/www/htdocs;
expires 30d;
}
location ~*\.(gif|jpg|jpeg|png|css|ico|html)$ {
root /www/abc.com/www/htdocs;
expires 30d;
}
如果要定义多个location,则可以有2种方式:
使用/ :location / { client_max_body_size 200m; proxy_connect_timeout 30; proxy_set_header Host $http_host; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http://127.0.0.1:8008; } location /tmp/{ root /; internal; } 采用这种方式,/tmp可以放在/的下面,因为“/是匹配任何查询,但是正则表达式规则和长的块规则将被优先和查询匹配”
使用~ /* : location ~ /tmp/ { root /tmp; internal; } location ~ /* { client_max_body_size 20m; proxy_connect_timeout 30; fastcgi_pass fpass; include fastcgi_params; } 采用这种方式,/tmp则必须放在~ /*这个前面,因为~是正则匹配的,正则匹配是有顺序的,只要匹配上就不会再往下匹配了。除非在conf中有定义=或者^~,也就是说=和^~的优先级最高,如果匹配上,就不会再去匹配其它的规则了。
1. = 严格匹配这个查询。如果找到,停止搜索。
2. ^~ 匹配路径的前缀,如果找到,停止搜索。
3. ~ 为区分大小写的正则匹配
4. ~* 为不区分大小写匹配
例子:
location = / {
# matches the query / only.
# 只匹配 / 查询。
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
[ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然而所有 /images/ 目录的请求将使用 Configuration C。
[ configuration D ]
}
我的添加模式,动静分离
location ^~ /(images|scripts|styles|upload)/ {
root /www/abc.com/www/htdocs;
expires 30d;
}
location ~*\.(gif|jpg|jpeg|png|css|ico|html)$ {
root /www/abc.com/www/htdocs;
expires 30d;
}
如果要定义多个location,则可以有2种方式:
使用/ :location / { client_max_body_size 200m; proxy_connect_timeout 30; proxy_set_header Host $http_host; proxy_set_header x-forwarded-for $remote_addr; proxy_pass http://127.0.0.1:8008; } location /tmp/{ root /; internal; } 采用这种方式,/tmp可以放在/的下面,因为“/是匹配任何查询,但是正则表达式规则和长的块规则将被优先和查询匹配”
使用~ /* : location ~ /tmp/ { root /tmp; internal; } location ~ /* { client_max_body_size 20m; proxy_connect_timeout 30; fastcgi_pass fpass; include fastcgi_params; } 采用这种方式,/tmp则必须放在~ /*这个前面,因为~是正则匹配的,正则匹配是有顺序的,只要匹配上就不会再往下匹配了。除非在conf中有定义=或者^~,也就是说=和^~的优先级最高,如果匹配上,就不会再去匹配其它的规则了。
相关文章推荐
- nginx匹配规则说明以及匹配的优先级
- location nginx location匹配规则说明
- Nginx—核心配置location匹配规则说明
- 第一篇博客:nginx在重写url时,如果server_name有两个以上匹配的域名,重写规则里面不能包含$server_name
- nginx location匹配规则
- nginx之location 匹配规则
- nginx location匹配规则
- Nginx的alias与root的用法区别和location匹配规则
- nginx location 匹配规则
- nginx的优先匹配规则
- nginx location匹配规则
- nginx 匹配规则简介
- Nginx的location匹配规则和全局变量
- nginx接收的host值会影响alias的规则匹配
- Nginx配置中Location的匹配规则
- nginx手记 location 配置及匹配规则
- 【Nginx】location 匹配规则详解
- nginx location匹配规则
- nginx的优先匹配规则 推荐
- Nginx的alias与root的用法区别和location匹配规则