Location规则
语法规则: location [=|~|~*|^~] /url/ {… }
| 符号 | 含义 |
|---|---|
| = | = 开头表示精确匹配 |
| ^~ | ^~开头表示uri以某个常规字符串开头, 理解为匹配url路径即可。 |
| ~ | ~ 开头表示区分大小写的正则匹配 |
| ~* | ~* 开头表示不区分大小写的正则匹配 |
| !~和!~* | !~和!~*分别为区分大小写不匹配及 不区分大小写不匹配的正则 |
| / | 用户所使用的代理(一般为浏览器) |
精确匹配
=
普通匹配
无符号
^~
正则匹配
~
~
!-
!-
安装echo模块
wget https://github.com/openresty/echo-nginx-module/archive/v0.61.ziptar -zxvf v0.61.zipcd nginx-1.16.1nginx -V./configure --add-module=/usr/local/src/echo-nginx-module-0.61 --prefix=/usr/local/nginx --with-http_ssl_modulemakemv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/cp objs/nginx /usr/local/nginx/sbin/nginx -Vnginx -s stop #必须重启后生效 reload无效nginx
实例
yxj.conf
server{listen 80;server_name yxj.mxs223.com;location /jrf/{echo '/jrf/';}location ~ /jrf/cms/{echo '~ /jrf/cms/';}#location ^~ /jrf/cms/{# echo '^~ /jrf/cms/';#}location /jrf/cms/{echo '/jrf/cms/';}location =/jrf/cms/index.html{echo '= /jrf';}}
curl http://yxj.mxs223.com:8080/jrf/index.htmlcurl http://yxj.mxs223.com:8080/jrf/cmscurl http://yxj.mxs223.com:8080/jrf/cms/curl http://yxj.mxs223.com:8080/jrf/cms/index.html
正则匹配优先级
1、location 配合先后顺序与配置文件location 先后顺序无关
2、如果都是普通匹配,最长的会被命中
3、普通匹配会被正则所覆盖
3、^~ 普通匹配 不会被正则所覆盖
4、=精准匹配直接命中

用户中心配置
server {listen 443 ssl;server_name www.ruiyi126.com ;ssl_certificate /app/nginx/cert/2379966_www.ruiyi126.com.pem;ssl_certificate_key /app/nginx/cert/2379966_www.ruiyi126.com.key;location /{proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass https://192.168.0.11;}location =/{proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass https://192.168.0.11/index.jhtml;}location ~ \.(gif|jpg|png|js|css|ico)$ {root /app/work/jinrfucenter_mobile/WebRoot;}}
实例 fastdfs 图片缩略图:
location ~(.+)_([0-9]+)x([0-9]+)\.(jpg|gif|png) {set $w $2;set $h $3;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";image_filter resize $w $h;image_filter_buffer 10M;proxy_pass http://10.10.10.66/$1.$4;}
