I'm proxying request from nginx to Jetty, but I have problem with request that Jetty receives. Jetty requests shows that request IP address is But I want real server IP and my site has multiple domains, so when request is coming from some domain name to my server, it must available in Jetty request too.

nginx config:

server {

    listen   80; ## listen for ipv4
    listen   [::]:80 default ipv6only=on; ## listen for ipv6

    server_name  localhost;

    access_log  /var/log/nginx/localhost.access.log;

    location / {
        proxy_set_header  X-Real-IP  $remote_addr;

Servlet request:

Dump Servlet
getMethod:  GET
getContentLength:   -1
getContentType:     null
getRequestURI:  /dump/info
getServletPath:     /dump
getPathInfo:    /info
getPathTranslated:  /tmp/jetty-
getQueryString:     null
getProtocol:    HTTP/1.0
getScheme:  http
getServerPort:  8080
getLocalPort:   8080
getRemoteUser:  null
getUserPrincipal:   null
getRemotePort:  50905
getRequestedSessionId:  6ubs42zhm5q61k5hm84ni3ib
isSecure():     false
isUserInRole(admin):    false
getLocale:  en_US
getLocales:     en_US
getLocales:     en

If I understand it correctly, you want to proxy requests for multiple domains using one location block in nginx.

In this situation you can modify your configuration as follows:

location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header Host $http_host;

That copies Host header from the source request to the proxied request and allows backend server to use virtual hosts.

You need to get Jetty to look at a header set by nginx when determining the client IP, instead of the source of the IP connection. Based on this forum post, you need to set useForwardedForHeader to true, and configure nginx to set an X-Forwarded-For header with the following config:

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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