Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 127.0.0.1. 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_pass  http://127.0.0.1:8080;
        proxy_set_header  X-Real-IP  $remote_addr;
    }
}

Servlet request:

Dump Servlet
getMethod:  GET
getContentLength:   -1
getContentType:     null
getRequestURI:  /dump/info
getRequestURL:  http://127.0.0.1:8080/dump/info
getContextPath:     
getServletPath:     /dump
getPathInfo:    /info
getPathTranslated:  /tmp/jetty-0.0.0.0-8080-test.war-_-any-/webapp/info
getQueryString:     null
getProtocol:    HTTP/1.0
getScheme:  http
getServerName:  127.0.0.1
getServerPort:  8080
getLocalName:   127.0.0.1
getLocalAddr:   127.0.0.1
getLocalPort:   8080
getRemoteUser:  null
getUserPrincipal:   null
getRemoteAddr:  127.0.0.1
getRemoteHost:  127.0.0.1
getRemotePort:  50905
getRequestedSessionId:  6ubs42zhm5q61k5hm84ni3ib
isSecure():     false
isUserInRole(admin):    false
getLocale:  en_US
getLocales:     en_US
getLocales:     en
share|improve this question

2 Answers 2

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_pass  http://127.0.0.1:8080;
    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.

share|improve this answer

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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