Nginx as a proxy for tomcat with subdirectory

19,110

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;
Share:
19,110

Related videos on Youtube

Xianlin
Author by

Xianlin

Updated on September 18, 2022

Comments

  • Xianlin
    Xianlin almost 2 years

    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;
            }
    
    • Digital site
      Digital site about 10 years
      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...
    • Xianlin
      Xianlin about 10 years
      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?
    • Digital site
      Digital site about 10 years
      so how do you use Tomcat with Nginx then? isn't it as a proxy? can you explain what your server structure?
    • Xianlin
      Xianlin about 10 years
      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.
    • Digital site
      Digital site about 10 years
      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.
  • Digital site
    Digital site about 10 years
    I just updated the answer because I found out what the second issue is about!
  • Xianlin
    Xianlin about 10 years
    I remove the both '/' but example.com/demo/app1 still gives me 404 error
  • Digital site
    Digital site about 10 years
    ok, quick question, location /demo/ is it a folder or a subdomain?
  • Xianlin
    Xianlin about 10 years
    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.
  • Digital site
    Digital site about 10 years
    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