简介
Nginx的三大区块
在Nginx中主要配置包括三个区块,结构如下:
http { #协议级别include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;keepalive_timeout 65;server { #服务器级别listen 80;server_name wang.mingqu.com;charset utf-8;location / { #请求级别root /www/html/web/;index index.html index.htm;}}
}
什么是location?
location是配置在Server模块中的请求级别配置。
location可以根据不同的URI使用不同的配置来处理不同的请求。
location是有顺序的,会根据不同请求配置的优先级来匹配的location处理。
应用
基本语法
server {......location [=|~|~*|^~|@] pattern {......}
}
前缀匹配
符号的解释
符号 | 解释 |
---|---|
= | 表示精确匹配,优先级最高 |
^~ | 表示URI以某个常规字符串开头的匹配,匹配URL的路径 |
~ | 表示区分大小写的正则匹配 |
~* | 表示不区分大小写的正则匹配 |
!~ | 表示区分大小写且不匹配的正则 |
!~* | 表示不区分大小写且不匹配的正则 |
/ | 通用匹配符,匹配任意请求 |
@ | 内部服务跳转 |
符号的优先级
注意:有多个location配置的情况下,依照优先级匹配;当匹配成功后,停止匹配。
应用举例
主配置文件
路径:/etc/nginx/nginx.conf
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;events {worker_connections 1024;
}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;
}
无修饰符
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location /web01/ {root /www/wangmingqu/html;index index.html index.htm;}
}
- 数据准备
mkdir /www/wangmingqu/html/web01 -p
echo "无修饰符" > /www/wangmingqu/html/web01/index.html
- 启动验证
nginx -t
systemctl restart nginx
=匹配
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location / {root /www/wangmingqu/html;index index.html index.htm;}location = / {root /www/wangmingqu/html;index index.html index.htm;}
}
- 数据准备
mkdir /www/wangmingqu/html/{web01,web} -p
echo "无修饰符" > /www/wangmingqu/html/web01/index.html
echo "精确匹配" > /www/wangmingqu/html/web/index.html
- 启动验证
nginx -t
systemctl restart nginx
^~匹配
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location ^~ /yewu/ {root /www/wangmingqu/html;index index.html index.htm;}
}
- 数据准备
mkdir -p /www/wangmingqu/html/yewu
echo "以yewu开头的匹配" > /www/wangmingqu/html/yewu/index.html
- 启动验证
nginx -t
systemctl restart nginx
~匹配
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location ~* \.jpeg$ { #区分大小写匹配的所有以.jpeg结尾的文件root /www/wangmingqu/html/images/;}
}
- 数据准备
mkdir -p /www/wangmingqu/html/images/
#上传.png和.PNG结尾的图片
ll /www/wangmingqu/html/images/
total 1360
-rw-r--r-- 1 root root 1166629 Mar 4 14:22 lower.jpeg
-rw-r--r-- 1 root root 222061 Mar 4 14:22 upper.JPEG
- 启动验证
nginx -t
systemctl restart nginx
后缀大写
后缀小写
~*匹配
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location ~ \.jpeg$ { #不区分大小写匹配的所有以.jpeg结尾的文件root /www/wangmingqu/html/images/;}
}
- 数据准备
mkdir -p /www/wangmingqu/html/images/
#上传.png和.PNG结尾的图片
ll /www/wangmingqu/html/images/
total 1360
-rw-r--r-- 1 root root 1166629 Mar 4 14:22 lower.jpeg
-rw-r--r-- 1 root root 222061 Mar 4 14:22 upper.JPEG
- 启动验证
nginx -t
systemctl restart nginx
后缀大写
后缀小写
/匹配
- Nginx配置
路径:/etc/nginx/conf.d/wangmingqu.conf
server {listen 80;server_name wang.mingqu.com;charset utf-8;location / {root /www/wangmingqu/html/;index index.html index.htm;}
}
- 数据准备
mkdir -p /www/wangmingqu/html/
echo "通用匹配符" > /www/wangmingqu/html/index.html
- 启动验证
nginx -t
systemctl restart nginx
@匹配
@符号,用于定义一个location块,且该location块不能被外部client访问,只能被Nginx内部配置的指令访问,如try_files、error_page。
- try_files举例:
server {listen 80;server_name wang.mingqu.com;charset utf-8;location / {root /usr/share/nginx/html;index index.html index.htm;try_files $uri $uri/ @router;}location @router{rewrite ^(.+)$ /index.html last;}
}
- error_page举例:
server {listen 80;server_name wang.mingqu.com;charset urf-8;location / {error_page 418 = @queryone;error_page 419 = @querytwo;error_page 420 = @querythree;if ( $args ~ "service=one" ) { return 418; }if ( $args ~ "service=two" ) { return 419; }}location @queryone {return 200 'do stuff for one';}location @querytwo {return 200 'do stuff for two';}
}