Nginx

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
原文地址:https://www.cnblogs.com/hukey/p/11868017.html