Score:1

Get the client ip adress from servlet when tomcat is accessed via nginx-load balancer

tc flag

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/ ... ;
   }
}
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.