简体   繁体   English

tomcat Web应用程序的上下文路径,前面是Nginx作为反向代理

[英]Context path for tomcat web application fronted with Nginx as reverse proxy

I'm trying to deploy an web application on tomcat server fronted with Nginx. 我正在尝试在面向Nginx的tomcat服务器上部署Web应用程序。 The problem I encounter is tag in my jsp pages is printing out "incorrect" (it is correct from tomcat point of view) context path. 我遇到的问题是我的jsp页面中的标签是打印出“不正确”(从tomcat的角度来看是正确的)上下文路径。

My Web app on tomcat is deployed on context path: /webApp1 with tomcat running on port 8080. So the web application is accessible via http://localhost:8080/webApp1 tomcat上的我的Web应用程序部署在上下文路径上:/ webApp1,在端口8080上运行tomcat。因此可以通过http:// localhost:8080 / webApp1访问Web应用程序

My nginx is configured to proxy_pass as follows: 我的nginx配置为proxy_pass,如下所示:

location / {
    http://localhost:8080/webApp1;
}

With this configuration, the web app is supposed to work with url http://localhost 使用此配置,Web应用程序应与url http:// localhost一起使用

This only works for the home page text. 这仅适用于主页文本。 The home page loaded successfully but all the links on home page have /webApp1 prefixed as tomcat think that it is running by itself hence the output the contextpath as prefix for all links. 主页已成功加载,但主页上的所有链接都以/ webApp1为前缀,因为tomcat认为它本身正在运行,因此输出contextpath作为所有链接的前缀。

Has anyone fixed this before. 有谁修过这个。

All answers are much appreciated. 所有答案都非常感谢。

z. ž。

I managed to fix this problem after spending lots of time. 我花了很多时间设法解决了这个问题。

There's a 3rd party module for nginx HttpSubsModule , which allows you to replace strings in the response body (eg. html). nginx HttpSubsModule有一个第三方模块,它允许你替换响应体中的字符串(例如.html)。

So the problem can be fixed by: 所以问题可以解决:

location / {
    http://localhost:8080/webApp1;
    subs_filter_types text/html;
    subs_filter '/webApp1' '';
}

It'll remove all the context '/webApp1' from the html responses. 它将从html响应中删除所有上下文'/ webApp1'。

Hope this helps others who encountered this problem too. 希望这也有助于遇到此问题的其他人。

z. ž。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM