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区段
通过指定模式来与客户端请求的URI相匹配,基本语法如下:location [=|~|~*|^~|@] pattern{……}
1、没有修饰符 表示:必须以指定模式开始,如:server {
server_name a.com;
location /abc {
……
}
}
那么,如下是对的:
http://a.com/abc
http://a.com/abc?p1
http://a.com/abc/
http://a.com/abcde
2、=表示:必须与指定的模式精确匹配server {
server_name a.com;
location = /abc {
……
}
}
那么,如下是对的:
http://a.com/abc
http://a.com/abc?p1
如下是错的:
http://a.com/abc/
http://a.com/abcde
3、~ 表示:指定的正则表达式要区分大小写server {
server_name a.com;
location ~ ^/abc$ {
……
}
}
那么,如下是对的:
http://a.com/abc
http://a.com/abc?p1=11&p2=22
如下是错的:
http://a.com/ABC
http://a.com/abc/
http://a.com/abcde
4、~* 表示:指定的正则表达式不区分大小写server {
server_name a.com;
location ~* ^/abc$ {
……
}
}
那么,如下是对的:
http://a.com/abc
http://a..com/ABC
http://a.com/abc?p1=11&p2=22
如下是错的:
http://a.com/abc/
http://a.com/abcde
5、^~ 类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了。
6、@ :定义命名location区段,这些区段客户段不能访问,只可以由内部产生的请求来访问,如try_files或error_page等