Score:1

nginx proxy_pass proxy_set_header of proxy host

jp flag

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

Score:1
by flag

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;
    }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.