I am using nginx as load balancer with a few tomcat servers as backends.
Web-clients are java-scripts which from the website post a request to the Load-balancer.
So the path is following:
client-xhttp-request from PC1 having IP 1
via Javascript hosted on domainA.com (server A with IP 2)
Request is posted to --> backend.domainA.com (also on server A with IP 2)
Load balancer upstreams the request to --> Tomcat (on servers B, C or D)
When executing the servlet request directly to the Tomcat Server, I get the desired client IP 1, so I can exclude that the problem lies in programming errors.
I tried below configuration but still I am receiving back only the IP 2 of the web-server A instead of the desired IP 1 of the requesting client. How do I need to configure the nginx-part to retrieve IP 1?
nginx.conf:
user www-data;
---- other stuff
http {
--- other stuff
upstream backend {
server mytomcat1.serverB.com max_fails=1 fail_timeout=2s;
server mytomcat2.serverC.com max_fails=2 fail_timeout=5s;
server mytomcat3.serverD.com max_fails=2 fail_timeout=5s;
}
}
sites-available/com.domainA.backend
server {
listen 80;
server_name backend.domainA.com;
# Redirect all traffic to SSL:
rewrite ^ https://$server_name$request_uri? permanent;
set_real_ip_from unix:;
real_ip_header X-Real-IP;
real_ip_recursive on;
}
server {
listen 443 ssl;
server_name backend.domainA.com;
...
other stuff
...
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_pass http://backend;
}
}
sites-available/com.domainA
server {
listen 80;
server_name domainA.com www.domainA.com;
# Redirect all traffic to SSL
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443 ssl;
server_name domainA.com www.domainA.com;
location / {
root /var/www/html/ ... ;
}
}