I want to set up a Nginx Proxy for the Tomcat server with my domain name such as
example.com/demo/sample
example.com/demo/manager
example.com/demo/other_apps
...
Here is my Nginx server block
configuration
server {
listen 80;
server_name example.com;
location /demo/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://IP_ADD_TOMCAT_SERVER:8080/;
}
location ~ /\.ht {
deny all;
}
## Only allow these request methods ##
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
## Do not accept DELETE, SEARCH and other methods ##
}
I encountered 2 problems here:
The First Problem:
if TOM_CAT_INSTALL_DIR/webapps/sample/
contains a static page hello.jsp
, it works with URL:
example.com/demo/app1/
but not works with URL:
example.com/demo/app1
Why I must add a trailing slash /
at the end of URL to make the nginx proxy working?
The Second Problem:
If TOM_CAT_INSTALL_DIR/webapps/manager
contains a index.jsp
file which is a dynamic webpage, it does not work with URL
example.com/demo/manager/
The URL becomes
example.com/manager/....
Followed by a long list of parameters.
if I manually add /demo/
string to the web browser URL, it works again.
How should I make the nginx proxy works with sub-directory /demo/
?
Update: I found out the missing rewrite problem for tomcat manager subdirecotry is that in the index.jsp file, the request.getContextPath()
will NOT automatically add /demo/ subdirecotry into the URL. It seems we have to manually modify the .jsp file code.
If you don't know how to modify the jsp code as I do, you can work around it by using the below code in Nginx
# Must add the trailing '/' for both location and proxy_pass
location /demo/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://TOM_CAT_SERVER_IP_ADDR:8080/;
}
# ONLY work for manager apps, for other apps,
# You must add more rewrite rules like the below
location /manager/ {
rewrite ^/(.*)$ /demo/$1 last;
}
example.com/demo/manager/
becomesexample.com/manager/
that means you have a rewrite rule there or the file itself has a rewrite rule. by the way, I don't see the root in your config settings!! you have to have a root so that the server can tell where the root is...