Nginx reverse proxy for tomcat

2019-03-21 16:10发布

I am having issues getting my nginx + tomcat 7 reverse proxy setup working.

Basically I want https://192.168.10.101 to serve content from the upstream cluster/webapp/; However I am getting a 404 page from my applicaton.

Any hints on whats going wrong would be greatly appreciated.

My configuration is given below.

server {

            server_name 192.168.10.101;
            access_log /var/log/nginx/mysite-access.log;
            listen   443;
            ssl on;
            ssl_certificate    /etc/nginx/ssl/mysite.crt;
            ssl_certificate_key    /etc/nginx/ssl/private/mysite_pvt.key;

                location  / {
                        proxy_redirect off;
                        proxy_pass      https://tccluster/webapp/;
                        rewrite_log     on;

                        proxy_set_header   Host             $host;
                        proxy_set_header   X-Real-IP        $remote_addr;
                        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                        proxy_max_temp_file_size 0;

                }
}

upstream  tccluster  {
                server   192.168.56.103:8443;
                server   192.168.56.104:8443;
}

2条回答
老娘就宠你
2楼-- · 2019-03-21 16:32

full nginx config to pass to tomcat context :

server {
  listen 80;         # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
  listen [::]:80;
  server_name tomcat-context.domain.com ;

  # individual nginx logs for this vhost
  access_log  /var/log/nginx/tomcat-context_domain_access.log main;
  error_log   /var/log/nginx/tomcat-context_domain_error.log;



    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
    location / {
                proxy_pass  http://127.0.0.1:10080/tomcat-context/;
                proxy_redirect off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                rewrite ^/tomcat-context/(.*)$ /$1 last;
    }
    location /tomcat-context {
      rewrite ^/tomcat-context(.*)$ $1  redirect;
    }
}
查看更多
祖国的老花朵
3楼-- · 2019-03-21 16:41

Finally figured it out. The app has a filter that redirects to /webapp/index.html , which made nginx make the request for /webapp/webapp/index.html which was giving the 404.

I added a rewrite rule

location / {
                proxy_pass  https://backend/webapp/;
                proxy_redirect off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                rewrite ^/webapp/(.*)$ /$1 last;
}

And this seems to be working for now !

查看更多
登录 后发表回答