1. 需求分析
为了在线上环境提供测试分支,规定将某IP转发到测试程序地址。如果是 ngx 直接对外,采用 real_ip 就能够做限制,但是最前端确实一个7层是负载均衡就需要研究一番了。
2. 实践
业务部署在某云上,前端上挂着一个7层的负载均衡,通过查看官方可以通过 X-Forwarded-For 来获取客户端真实IP,这样就很简单了。
通过测试模拟这样的环境验证下:
192.168.118.14 - ngx :该ngx 只是充当负载均衡,开启 X-Forwarded-For
192.168.118.15 - ngx:该ngx 为 web服务器,真正的用户端IP判断在这里实现
192.168.118.16 - httpd:充当 测试程序
配置如下:
192.168.118.14(模拟负载均衡):
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发客户端真实IP location / { proxy_pass http: //192 .168.118.15/; # 仅仅用作转发 access_log logs /14_access .log main; error_log logs /14_error .log; #root html; #index index.html index.htm; } |
192.168.118.15(nginx - 用户端IP判断在此实现)
|
upstream back1 { server 192.168.118.16:8080; } server { listen 80; server_name localhost; location / { if ($http_x_forwarded_for ~* "192.168.118.2" ) { rewrite ^/(.*)$ http: //192 .168.118.15 /back1/ $1 break ; } root html; index index.html; access_log logs /15_access .log main; error_log logs /15_error .log; } location /back1 { proxy_pass http: //back1/ ; } |
因为负载均衡可以转发用户真实IP,所以在 nginx 中,直接判断 http_x_forwarded_for 就能做路由。
192.168.118.16 开启 8080 端口
|
# curl http://192.168.118.16:8080/a.html <h1>192.168.118.16: a.html< /h1 > |
直接访问 192.168.118.15
|
# curl http://192.168.118.15/a.html <h1>192.168.118.15: a.html< /h1 > |
定义的规则:当客户端 192.168.118.2 访问 192.168.118.14时,则转发到 192.168.118.16:8080,剩下其他客户端IP则直接访问 192.168.118.15
3. 验证
通过 192.168.118.2 客户端访问:
http://192.168.118.14/a.html 地址在浏览器已经经过 302 跳转到新的连接地址了。
其他客户端IP访问:
4. 后续
通过 rewrite 这种方式实现了条件判断的跳转,但是这个跳转是 HTTP 302 并不支持 head 等信息的转发,所以在提交 post 请求时,会出现错误。
在生产环境中,有这样一个实例:
需求:当IP: 192.168.118.2 访问 http://192.168.118.15/bbs/ 时,跳转到 http://192.168.118.16:8080/ uri中不能带有 /bbs
实现:
注意:在条件判断的反向代理中,proxy_pass http://192.168.118.16:8080 后面不得带 '/' 或者其他路径,否则 nginx -t 检测会报错。报错信息如下:
|
[root@localhost conf] # nginx -t nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/nginx/conf/nginx .conf:50 nginx: configuration file /usr/local/nginx/conf/nginx .conf test failed |
版权声明:本文内容来自博客园:hukey,遵循CC 4.0 BY-SA版权协议上原文接及本声明。
本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行可。
原文链接:https://www.cnblogs.com/hukey/p/11868017.html
如有涉及到侵权,请联系,将立即予以删除处理。
在此特别鸣谢原作者的创作。
此篇文章的所有版权归原作者所有,与本公众号无关,商业转载建议请联系原作者,非商业转载请注明出处。

优网科技秉承"专业团队、品质服务" 的经营理念,诚信务实的服务了近万家客户,成为众多世界500强、集团和上市公司的长期合作伙伴!
优网科技成立于2001年,擅长网站建设、网站与各类业务系统深度整合,致力于提供完善的企业互联网解决方案。优网科技提供PC端网站建设(品牌展示型、官方门户型、营销商务型、电子商务型、信息门户型、DIY体验、720全景展厅及3D虚拟仿真)、移动端应用(手机站、APP开发)、微信定制开发(微信官网、微信商城、企业微信)、微信小程序定制开发等一系列互联网应用服务。