nginx as proxy using a specific source ip

14,576

Solution 1

proxy_bind directive allows you to choose different source IP address.

http://wiki.nginx.org/HttpProxyModule#proxy_bind

So your configuration would look like:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

location /integracao/ {
    proxy_bind A.A.A.A;
    proxy_pass http://X.X.X.X:9080/integracao/;
}

location /solr/ {
    proxy_bind B.B.B.B;
    proxy_pass http://Y.Y.Y.Y:8080/solr/;
}

Solution 2

if nginx cannot do it you can always use netfilter and SNAT to make it appear like nginx was using specific ip:

iptables -t nat -A POSTROUTING -p tcp --dport 9080 -d ip_of_your_backend -j SNAT --to one_of_ips_bound_to_nginx_server
Share:
14,576

Related videos on Youtube

msbrogli
Author by

msbrogli

Updated on September 18, 2022

Comments

  • msbrogli
    msbrogli almost 2 years

    I'm using nginx to serve static file and proxy other requests to some Tomcat instance. The problem is that I don't know how to choose which IP address will nginx use to connect to Tomcat.

    Each Tomcat instance only accept HTTP connections from specific IP addresses. My server has all these IPs. I just can't choose which one will nginx use.

    This is my config file:

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    
    location /integracao/ {
        proxy_pass http://X.X.X.X:9080/integracao/;
    }
    
    location /solr/ {
        proxy_pass http://Y.Y.Y.Y:8080/solr/;
    }
    

    My server has one interface with two IP addresses: A and B. I need to use IP A to connect to first Tomcat and IP B to connect to Solr.

    Do anyone knows how to do it?

  • msbrogli
    msbrogli almost 11 years
    This directive wasn't available at the time of my question. It's available since version 0.8.22. Thanks for your help :)