黑松山资源网 Design By www.paidiu.com
业务需求
业务和开发同事需要我这边做一条规则,所有访问 ip 为非上海、广州 office 外网 ip,url 为http://test.com/fuck/index.html 的请求都跳转到 http://test.com/index.html 。然后所有在上海和广州 office 的外网 IP 访问 http://test.com/fuck/index.html 依然还是 http://test.com/fuck/index.html。这样就可以在生产上做隔离,不影响其他用户的服务。
注:因为目前生产上的 Nginx 没有做 lua 支持,所以就无法通过使用 lua 来实现该需求,也没有安装 geoip ,所以也无法用模块来支持,只能原生的。
原始的 nginx 配置
upstream service_test { server 127.0.0.1:8080; } server { listen 80; server_name test.com; index index.html index.php; root /tmp/test.com; error_page 404 http://test.com/404.html; error_page 502 http://test.com/502.html; error_page 500 http://test.com/500.html; location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$ { rewrite ^(.*)$ /static$1 break; root /tmp/test.com; # expires 1d; } location ~* \.(html|htm)$ { rewrite ^(.*)$ /static$1 break; roo /tmp/test.com; # expires 900s; } location / { proxy_pass http://service_test; include /opt/conf/nginx/proxy.conf; }
修改后的 Nginx 配置
upstream service_test { server 127.0.0.1:8080; } server { listen 80; server_name test.com; index index.html index.php; root /tmp/test.com; error_page 404 http://test.com/404.html; error_page 502 http://test.com/502.html; error_page 500 http://test.com/500.html; location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$ { rewrite ^(.*)$ /static$1 break; root /tmp/test.com; # expires 1d; } location ~* \.(html|htm)$ { rewrite ^(.*)$ /static$1 break; roo /tmp/test.com; # expires 900s; } set $flag 0; if ($request_uri ~* "^/fuck/\w+\.html$") { set $flag "${flag}1"; } if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") { set $flag "${flag}2"; } if ($flag = "012") { rewrite ^ /index.html permanent; } location / { proxy_pass http://service_test; include /opt/conf/nginx/proxy.conf; }
在实现需求的过程中出现的问题
把 if 指令 和 proxy_pass 都放在 location 下面的话,if 指令里面的内容不会执行,只会执行 proxy_pass。
location / { if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") { rewrite ^ /index.html permanent; } proxy_pass http://service_test; include /opt/conf/nginx/proxy.conf; }
if 指令下面使用 proxy_pass 指令问题
像下面这样使用会报错,错误的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") { proxy_pass http://test.com/fuck; }
正确的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") { proxy_pass http://test.com$request_uri; }
或是
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") { proxy_pass http://test.com; }
如果你是直接另外启动一个 location 的话,比如启动如下 location :
location /fund { if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") { rewrite ^ /index.html permanent; } }
这样的方式也是不支持的,当用 IP 192.168.0.50 访问的时候,没有达到我们的业务需求,会报错 400
注:各位有其他好的建议,欢迎探讨。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年07月01日
2024年07月01日
- 《陈百强28CD合集》1979-1993 低速原抓[CUE+WAV][8.3G]
- 【蓝调摇滚】JohnHaydock-2024-StraightLineOnACurvedRoad(FLAC)
- 感受独特魅力《百万双钢琴演奏曲》5CD[WAV+CUE]
- 【古典音乐】安娜·莫福《咏叹调》美国版SACD[WAV+CUE]
- 《法拉利·汽车专用试音碟》名贵音响顶级黑胶金碟[CUE+WAV][478M]
- 群星《惠威试音碟5CD》顶级音质[CUE+APE][1.7G]
- 《101系列:百分百3CD 100首百分百经典金曲》[WAV+CUE][2.9GB]
- 群星.2004-《第八届广州国际高级音响展》HIFI珍藏版[APE+CUE]
- 红音堂《蔡琴往事系列》SQCD紫银合金5CD[wav]
- 张小英1978-可爱的人[新加坡黑胶版][WAV+CUE]
- 张小英1978-心有千千结[新加坡黑胶版][WAV+CUE]
- 谭晶《祖国万岁·作品全集》(8CD)[WAV+CUE]
- 群星.2010-《第13届汕头音响展纪念CD》非卖品[FLAC+CUE]
- 群星.2010-《第13届汕头音响展纪念CD》非卖品[FLAC+CUE]
- 吉尔·沙汉姆-《梁祝柴科夫斯基小提琴协奏曲(GilShaham)》[WAV分轨]