大网数据广告活动
服务器运维问答

nginx proxy_pass proxy_set_header of proxy host

· 52CE 我爱测

Using NGINX as a reverse proxy, I want to tag which load balancer/proxy the request can in on, and pass that to the end app servers... For example, we have 2 ingress connections, using round-robin DNS to 3 load balancers, so, con…

问题原文

原帖链接 · MrPHP · CC BY-SA 4.0

Using NGINX as a reverse proxy, I want to tag which load balancer/proxy the request can in on, and pass that to the end app servers...

For example, we have 2 ingress connections, using round-robin DNS to 3 load balancers,

so, connection one has 3 pubic IP's and connection two has 3 IP's. each pair points to a load balancer and they're using proxy pass upstream to send traffic to the 10 web/app servers.

I want to tag which of the 3 LB's the request comes in on, I'd love to tag it based on the connection too, if you know how.


upstream web_cluster {
    random;
    # web server ip addresses x 10
}

location / {
    proxy_pass                      http://web_cluser;
    proxy_set_header                Proxy $proxy_server_host_name; // here
    proxy_set_header                Route $proxy_server_public_ip; // here
    proxy_pass_request_headers      on;
}

The servers are all deployed via Ansible scripts, so I wouldn't be able to hard-code server names into the proxy_set_header options.

Thanks

已采纳解答

原帖链接 · Saxtheowl · CC BY-SA 4.0

To tag all this we could change your configuration file to use the map directive to map the remote IP address to the corresponding load balancer name, and the $proxy_host variable to set the Proxy-Host header.

upstream web_cluster {
    server 10.0.0.1;
    server 10.0.0.2;
    server 10.0.0.3;
    # add more servers here as needed
}

map $remote_addr $proxy_host {
    192.168.1.1  "lb1";
    192.168.1.2  "lb2";
    192.168.1.3  "lb3";
    # add more mappings here as needed
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://web_cluster;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Proxy-Host $proxy_host;
    }
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-04-11)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。