版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/qq_24095055/article/details/91411242
location正则表达式书写示例
1. 等号(=)
表示完全匹配规则才执行操作
location = /index {
[ configuration A ]
}
URL为 http://{domain_name}/index
时,才会执行配置中操作。
2. 波浪号(~)
表示执行正则匹配,但区分大小写
location ~ /page/\d{1,2} {
[ configuration B ]
}
URL 为 http://{domain_name}/page/1
匹配结尾数字为1-99时,配置生效。
3.波浪号与星号(~*)
表示执行正则匹配,但不 区分大小写
location ~* /\.(jpg|jpeg|gif) {
[ configuration C ]
}
匹配所有url以jpg、jpeg、gif
结尾时,配置生效。
4.脱字符与波浪号(^~)
表示普通字符匹配,前缀匹配有效,配置生效
location ^~ /images/ {
[ cofigurations D ]
}
URL 为 http://{domain_name}/images/1.gif
时,配置生效。
5.@
定义一个location,用于处理内部重定向
location @error {
proxy_pass http://error;
}
error_page 404 @error;
各字符有效优先级
= > ^~ > ~/~*
当(~/~*)
中有多个正则匹配时,选择正则表达式最长的配置执行。
使用示例
修改Nginx配置,使接口中只有web部分可以被外部访问,设置如下:
扫描二维码关注公众号,回复:
6415908 查看本文章
location ~ /.*/web/ {
include /data/pre/nginx/nginx/conf/nginx_cors;
proxy_pass http://127.0.0.1:8017;
proxy_set_header X-Real-IP $clientRealIp;
proxy_set_header isHttps true;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
}