9

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?

10

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/;
}
| improve this answer | |
  • This directive wasn't available at the time of my question. It's available since version 0.8.22. Thanks for your help :) – msbrogli Sep 4 '13 at 21:50
1

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
| improve this answer | |
0

Searching through the nginx wiki and especially the part about ProxyModule, I did not find anything related.

If you have the possibility to experiment a bit, I would suggest that you try different values in the listen directive and check the access logs at your backend server to see what IP-address nginx comes from.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.