我正在从nginx代理请求到Jetty,但我有Jetty收到请求的问题。 Jetty请求显示请求IP地址是127.0.0.1。 但是我想要真正的服务器IP,我的站点有多个域,所以当请求从某个域名到我的服务器时,它也必须在Jetty请求中可用。
nginxconfiguration:
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请求:
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
如果我理解正确,则需要使用nginx中的一个location块代理多个域的请求。
在这种情况下,你可以修改你的configuration如下:
location / { proxy_pass http://127.0.0.1:8080; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; }
将Host头从源请求复制到代理请求,并允许后端服务器使用虚拟主机。
在确定客户端IP时,您需要让Jetty查看由nginx设置的标头,而不是IP连接的源。 基于这个论坛post,你需要设置useForwardedForHeader为true ,并configurationnginx设置一个X-Forwarded-For标题,其中包含以下configuration:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;