目录
一、Nginx的Httpp配置简介
二、Location匹配规则
1、=:精确匹配
2、^~:最佳匹配
3、~ :正则表达式要区分大小写
4、~* :正则表达式不区分大小写
5、/ 开头:通用匹配
6、综合示例
7、root 、alias指令区别
一、Nginx的Httpp配置简介
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;
}
}
}
二、Location匹配规则
优先级:(location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
以 = 开头,表示精确匹配;如只匹配根目录结尾的请求,后面不能带任何字符串。
以^~ 开头,表示如果该符号后面的字符是最佳匹配,采用该规则,当匹配成功后,不再进行后续的查找。
以~ 开头,表示区分大小写的正则匹配;
以~* 开头,表示不区分大小写的正则匹配
以/ 开头,通用匹配, 如果没有其它匹配,任何请求都会匹配到
1、=:精确匹配
server {
server_name sish
location = /abc {
……
}
}
那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1
如下是错的:
http://baidu.com/abc/
http://baidu.com/abcde
2、^~:最佳匹配
server {
server_name baidu.com;
location ^~/abc {
……(不再进行后续的查找)
}
}
那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1
http://baidu.com/abc/
http://baidu.com/abcde
3、~ :正则表达式要区分大小写
server {
server_name baidu.com;
location ~ ^/abc$ {
……
}
}
那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1=11&p2=22
如下是错的:
http://baidu.com/ABC
http://baidu.com/abc/
http://baidu.com/abcde
4、~* :正则表达式不区分大小写
server {
server_name baidu.com;
location ~* ^/abc$ {
……
}
}
那么,如下是对的:
http://baidu.com/abc
http://baidu..com/ABC
http://baidu..com/abc?p1=11&p2=22
如下是错的:
http://baidu..com/abc/
http://baidu..com/abcde
5、/ 开头:通用匹配
server {
server_name baidu.com;
location ^~/abc {
……
}
}
那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1
http://baidu.com/abc/
http://baidu.com/abcde
6、综合示例
location = / {
# 精确匹配 / ,主机名后面不能带任何字符串
[ configuration A ]
}
location / {
#通用匹配, 如果没有其它匹配,任何请求都会匹配到
[ configuration B ]
}
location /documents/ {
# 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
[ configuration C ]
}
location ~ /documents/Abc {
# 匹配任何以 /documents/ Abc开头的地址,匹配符合以后,还要继续往下搜索
[ configuration CC ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
[ configuration D ]
}
location ~* \.(gif | jpg | jpeg)$ {
# 匹配所有以 gif,jpg或jpeg 结尾的请求
[ configuration E ]
}
location /images/ {
# 字符匹配到 /images/,继续往下,会发现 ^~ 存在
[ configuration F ]
}
location /images/abc {
# 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
# F与G的放置顺序是没有关系的
[ configuration G ]
}
location ~ /images/abc/ {
# 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
[ configuration H ]
}
按照上面的location写法,以下的匹配示例成立:
/ -> config A
精确完全匹配,即使/index.html也匹配不了
/downloads/download.html -> config B
匹配B以后,往下没有任何匹配,采用B
/images/1.gif -> configuration D
匹配到F,往下匹配到D,停止往下
/images/abc/def -> config D
最长匹配到G,往下匹配D,停止往下
你可以看到 任何以/images/开头的都会匹配到D并停止,FG写在这里是没有任何意义的,H是永远轮不到的,这里只是为了说明匹配顺序
/documents/document.html -> config C
匹配到C,往下没有任何匹配,采用C
/documents/1.jpg -> configuration E
匹配到C,往下正则匹配到E
/documents/Abc.jpg -> config CC
最长匹配到C,往下正则顺序匹配到CC,不会往下到E
7、root 、alias指令区别
alias是一个目录别名的定义,root则是最上层目录的定义。还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无!
location /img/ {
alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去 /var/www/image/ 目录找文件
location /img/ {
root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去 /var/www/image/img/ 目录下找文件。