十三 nginx的localtion指令详解
Nginx 的 HTTP 配置主要包括三个区块,结构如下:
http { # 这个是协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
server { # 这个是服务器级别
listen 80;
server_name localhost;
location / { # 这个是请求级别
root html;
index index.html index.htm;
}
}
}
1 location 区段
-
location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
-
location 是有顺序的,会被第一个匹配的location 处理。
-
基本语法如下:
location [=|~|~*|^~|@] pattern{……}
2 location 前缀含义
= 表示精确匹配,优先级也是最高的
^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
!~ 表示区分大小写不匹配的正则
!~* 表示不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到
@ 内部服务跳转
3 location 配置示例
本地解析域名host
3.1 没有修饰符
表示:必须以指定模式开始
server {
listen 80;
server_name qf.com;
location /abc {
root /home/www/nginx;
index 2.html;
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1
http://qf.com/abc/
http://qf.com/abcde
3.2 =
表示:必须与指定的模式精确匹配
server {
listen 80;
server_name www.testpm.cn;
access_log /var/log/nginx/http_access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location = /abc {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1
http://qf.com/abc/
如下是错的
http://qf.com/abcde
3.3 ~
表示:指定的正则表达式要区分大小写
server {
server_name qf.com;
location ~ ^/abc$ {
……
}
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/abc?p1=11&p2=22
如下是错的
http://qf.com/ABC
http://qf.com/abc/
http://qf.com/abcde
3.4 ~*
表示:指定的正则表达式不区分大小写
server {
server_name qf.com;
location ~* ^/abc$ {
……
}
}
那么,如下是对的:
http://qf.com/abc
http://qf.com/ABC
http://qf.com/abc?p1=11&p2=22
如下是错的:
http://qf.com/abc/
http://qf.com/abcde
3.5 ^~
表示:类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。
server {
listen 80;
server_name localhost;
location ^~ /abc {
root /var/www/nginx;
index index.html;
}
}
3.6 @
表示:定义命名 location 区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如try_files或error_page等
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html;
try_files /index.htm /a.html /b.html @error;
}
location @error {
return 409;
}
}
server {
listen 80;
server_name 127.0.0.1;
location /abc {
return 302 https://www.baidu.com;
}
location ~ /abc {
return 302 https://www.taobao.com;
}
location ^~ /abc/ {
return 302 https://www.jd.com;
}
location ~* /ABC {
return 302 https://www.pingduoduo.com;
}
location = /abc {
return 302 https://www.vip.com;
}
}
3.7 查找顺序和优先级
- 带有“=“的精确匹配优先
- 没有修饰符的精确匹配
- 正则表达式按照他们在配置文件中定义的顺序
- 带有“^~”修饰符的,开头匹配
- 带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配
- 没有修饰符的,如果指定字符串与URI开头匹配
= 大于 ^~ 大于 ~|~*|!~|!~* 大于 /
多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
================================================
(1) =:表示完全匹配;
(2) ^~:匹配URI的前缀,并且后面的正则表达式不再匹配,如果一个URI同时满足两个规则的话,匹配最长的规则;
(3) ~:匹配正则表达式,大小写敏感;
(4) ~*:匹配正则表达式,大小写不敏感;
优先级:(1)> (2) > (3) = (4)
location 区段匹配示例
location = / {
# 只匹配 / 的查询.
[ configuration A ]
}
location / {
# 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。
[ configuration B ]
}
location ^~ /images/ { www.abc.
# 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
[ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将在Configuration C中处理。
[ configuration D ]
}
各请求的处理如下例:
/ → configuration A
/documents/document.html → configuration B
/images/1.gif → configuration C
/documents/1.jpg → configuration D
4 root & alias 指令区别
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。]
server {
listen 80;
server_name localhost;
location /cloud {
#http://localhost/cloud
#/usr/share/nginx/html/cloud
root /usr/share/nginx/html;
index index.html;
}
location /cloud {
#http://localhost/cloud
#/usr/share/nginx/html
alias /usr/share/nginx/html/;
index index.html;
}
}
- alias 是一个目录别名的定义,
- root 则是最上层目录的定义。
- 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无