1

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;
        }
5
  • if example.com/demo/manager/ becomes example.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... May 23, 2014 at 10:27
  • the tomcat server is not running on the nginx server so I couldn't possibly set the root=/path/to/tomcat on nginx server file system. Or can I?
    – Xianlin
    May 23, 2014 at 15:38
  • so how do you use Tomcat with Nginx then? isn't it as a proxy? can you explain what your server structure? May 24, 2014 at 4:12
  • Nginx is a proxy server, Tomcat is a java app server, they are on the same network with 2 IP addresses. A very simple server structure.
    – Xianlin
    May 25, 2014 at 15:59
  • ok, I thought different thing. I just noticed that you found the solution to your issue, which is something you updated in your original question. glad to hear things now work for you. May 26, 2014 at 5:25

2 Answers 2

2

I think you added something / extra in your config settings...

Look at this line:

proxy_pass http://IP_ADD_TOMCAT_SERVER:8080/;

you need to remove the trailing slash and it should work fine.

like this:

proxy_pass http://IP_ADD_TOMCAT_SERVER:8080;

try it out and see if things go fine!!

Update#1 I just noticed that you have another mistake "same typo" in your location /

remove the 2nd slash and it should work fine!!

like this : location /demo { instead of this location /demo/ {

Update #1: you can test your url using this

$ curl -I http://yoururl.com

and see what result gives you. this way you know if it is working or not.

Update #3:

your setting to work with any .jsp extension should have this code in your vhost:

location ~ \.jsp$ {
    proxy_pass              http://localhost:8080;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header        Host $http_host;
  }

also to get /demo to work , you need to add rewrite code below server_name example.com

rewrite ^/(.*)$ /demo/$1;
5
  • I just updated the answer because I found out what the second issue is about! May 23, 2014 at 7:42
  • I remove the both '/' but example.com/demo/app1 still gives me 404 error
    – Xianlin
    May 23, 2014 at 8:19
  • ok, quick question, location /demo/ is it a folder or a subdomain? May 23, 2014 at 10:25
  • location /demo/ does not exist as a folder, you can see it as a domain in which I also can use demo.example.com. But I want to use example.com/demo/ instead.
    – Xianlin
    May 23, 2014 at 15:36
  • in this case it is a subdomain, and you need therefore to configure this subdomain so that it works either demo.example.com or example.com/demo. I will update the answer soon May 24, 2014 at 4:19
0

My case http://localhost:8080/demo first page working but image and other components not get from server So I add following mapping

server {
    listen       80;
    listen       [::]:80;
    server_name  www.example.org example.org;

    #remove /demo if it come to URI otherwise it add two time for call proxy
    rewrite ^/demo(.*)$ $1;
    rewrite ^/(.*)$ /demo/$1;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.