使用反向代理时如何设置 Java Web 应用程序的上下文根
我在apache中使用mod_jk并在tomcat中配置虚拟主机的老方法 在JSP文件中,我引用CSS如下
/
<%=request.getContextPath()%>/css/styles.css
而主页链接设置为,
/<%=request.getContextPath()%>/
因此当我在apache中使用mod_jk使用ajp与tomcat一起工作;
- 当我尝试按如下方式配置反向代理时
ProxyPass / http://localhost:800/mywebapp
ProxyPassReverse / http://localhost:800/mywebapp
主页可以正常检索,但 css 请求变成 http://mydomain.com/mywebapp/mywebapp/css/style.css 所以无法正确检索css文件;
- 我认为一种可能的方法是始终使用相对路径,例如 ./style.css 或 ../style.css 一个。由于页眉/页脚是共享的,并且主页与详情页处于不同的级别,因此使用相对路径不方便,因为它们处于不同的级别 b.不过,我认为主页链接必须是 /<%=request.getContextPath()%>/
所以我想知道在 java web 中设置 contextroot 并与反向代理一起正常工作的方法是什么?
非常感谢
My old way using mod_jk in apache and configure virtual host in tomcat
In the JSP file, I refer to CSS as below/
<%=request.getContextPath()%>/css/styles.css
while the home link is set to
/<%=request.getContextPath()%>/
so this worked fine when I use mod_jk in apache to work with tomcat using ajp;
- When I try to configure reverse proxy as below
ProxyPass / http://localhost:800/mywebapp
ProxyPassReverse / http://localhost:800/mywebapp
the home page can be retrieved fine but the css request becomes
http://mydomain.com/mywebapp/mywebapp/css/style.css
so the css file can not be retrieved correctly;
- I think one possible way is to always use relative path like ./style.css or ../style.css
a. since header/footer are shared, and the home page is in a different level with detail page, it's inconvenient to use relative path because they're at a different level
b. still, I think the home link will have to be /<%=request.getContextPath()%>/
so I wonder what's the way to set contextroot fine in java web and also work fine with reverse proxy?
thx a lot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您的应用程序服务器(Tomcat)无法意识到反向代理的存在。一般来说,可以通过任意数量的反向代理或直接通过浏览器进行联系。通常使用网络配置来限制这一点,而不是 HTTP 或 Java。
因此,您必须准确地依赖相对 URL 才能使您的应用程序正常运行。
当我必须处理反向代理存在时(几乎总是由于 SSO 架构),我嵌入一个“junction”配置字符串项(代理中用于映射应用程序的 URL 部分),并在唯一需要的地方使用它。我需要建立一个绝对网址。
As I know your application server (Tomcat) isn't able to be aware of a reverse proxy presence. Generally speaking, it can be contacted through any number of reverse proxies or directly by browsers. A network configuration is usually used to limit this, not HTTP or Java.
So, you must accurately rely on relative URLs to make your application work well.
When I have to deal with reverse proxy presence (almost always due to SSO architectures), I embed a "junction" configuration string item (the portion of the URL used in the proxy to map the application) and use it in the only places where I need to build an absolute URL.