Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The problem I encounter is tag in my jsp pages is printing out "incorrect" (it is correct from tomcat point of view) context path.

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

My nginx is configured to proxy_pass as follows:

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

With this configuration, the web app is supposed to work with 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.

Has anyone fixed this before.

All answers are much appreciated.

z.

like image 994
zhou Avatar asked Oct 08 '22 22:10

zhou


1 Answers

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).

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.

Hope this helps others who encountered this problem too.

z.

like image 53
zhou Avatar answered Oct 15 '22 08:10

zhou