nginx 反向代理 "ip address shows"

nginx reverse proxy "ip address shows"

我的服务器上有 nginx 服务 example.com 网站。同一台服务器是 运行 tomcat 安装服务于 http://server_ip:8080/app 网络应用程序(这是一个 spring MVC 网络应用程序,由 Spring 安全保护)。

我希望能够使用 http://app.example.com 地址访问 tomcat webapp。我所做的是将nginx配置为反向代理如下(在sites-enabled下的app.example.com配置文件中):

server {
  listen          80;
  server_name     app.example.com;
  root            /var/lib/tomcat7/webapps/app;


  location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://server_ip:8080/;
  }
}

其中 server_ip 是我的服务器的 IPv4 地址。

此外,在 /etc/tomcat7/server.xml 文件中我添加了:

<Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Context path="" docBase="app">
         <WatchedResource>WEB-INF/web.xml</WatchedResource>
     </Context>
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="localhost_access_log." suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

因此当我访问 http://server_ip:8080 时 tomcat 默认将我发送到 app 网络应用程序(无论如何 app 网络应用程序是它唯一提供的服务) .

现在,如果我访问 http://app.example.com,我就可以得到 app 网络应用程序的登录页面;但是,登录后,浏览器的 URL 字段显示 http://server_ip:8080 而不是我所期望的 http://app.example.com

有人知道是什么原因造成的吗?我在网上找到的所有指南似乎都建议使用与我拥有的配置类似的配置。如果有人可以确认配置是否正常,我会很高兴,因为这可能指向服务器配置以外的原因(可能与登录后 spring 安全重定向的方式有关)。

谢谢。

你错过了

proxy_set_header Host $host;

实际上,我看不出有任何理由需要 X-Forwarded-HostX-Forwarded-Server headers。