0

I'm following this example:

https://devtidbits.com/2015/12/08/nginx-as-a-reverse-proxy-to-apache-tomcat/

my goal is to have Tomcat running and Nginx in front of it.

I would like to have structure similar to this:

test.com/ - static site from nginx

test.com/tomcat - tomcat manger (with working links)

test.com/app - tomcat app (deployed at /app on tomcat)

app2.test.com/app

My Nginx conf is:

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}
server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-test.com.conf;
    include snippets/ssl-params.conf;
    root /var/www/test.com/html;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name test.com www.test.com;
    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location ~ /.well-known {
                allow all;
        }

    location ~ ^/tomcat(/?)(.*)$ {  # OOPS!
      proxy_pass http://tomcat/$2$is_args$args;  # OOPS!
    }

    location /tomcat/ {
       include proxy_params;
       proxy_pass http://tomcat/;

        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  expires 1M;
 }
}

Thank you in advance!

  • You have two conflicting tomcat locations - what happens if you remove the first one? – Richard Smith Mar 2 '17 at 12:24
  • Do you mean to remove this one: location ~ ^/tomcat(/?)(.*)$ { # OOPS! proxy_pass tomcat/$2$is_args$args; # OOPS! } if I remove it the css of test.com/tomcat is broken – Vasil Koicev Mar 2 '17 at 12:31
  • Try location ^~ /tomcat/ for the second block (still with the first block removed) – Richard Smith Mar 2 '17 at 12:37
  • It's working fine the problem is with the links: 1) Open test.com/tomcat but the links in the this page point to test.com/manager/html but the working address is test.com/tomcat/manager/html - so I get 404 Not Found – Vasil Koicev Mar 2 '17 at 19:59
0

Try the following one instead of your two tomcat locations:

location ~ /tomcat(.*)$ {
    include proxy_params;
    proxy_pass http://tomcat$1$is_args$args;

    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
| improve this answer | |
  • Thank you, its also working bu I get broken links: 1) Open test.com/tomcat is fine but 2) opening the links in the this page point to test.com/manager/html but the working address is test.com/tomcat/manager/html - so I get 404 Not Found – Vasil Koicev Mar 2 '17 at 20:00
  • You need to fix your Tomcat application so that it generates links with /tomcat prefix. There isn't a way nginx can do this link fixing for you. – Tero Kilkanen Mar 2 '17 at 20:01
  • In this case is it possible to open tomcat manager app via https:ip-address and what should be the port – Vasil Koicev Mar 2 '17 at 20:04
  • I don't know any details of Tomcat so I cannot help. However, with that location block you use the port specified in the server block listen directive to access the service from outside. – Tero Kilkanen Mar 2 '17 at 20:09
  • I see. One last question if possible about nginx. Can I create a separate location without domain only with ip address, in other words instead of server_name example.com; to be ip address – Vasil Koicev Mar 2 '17 at 20:14

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.