用于tomcat的Nginx反向代理

时间:2013-09-21 19:32:26

标签: nginx tomcat7 reverse-proxy

我在使用nginx + tomcat 7反向代理设置时遇到问题。

基本上我希望https://192.168.10.101提供来自上游集群/ webapp /的内容;但是我从我的应用程序中获得了404页。

任何有关出错的提示都将不胜感激。

我的配置如下。

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 个答案:

答案 0 :(得分:7)

终于明白了。该应用有一个重定向到/webapp/index.html的过滤器,这使得nginx向/webapp/webapp/index.html发出了给予404的请求。

我添加了重写规则

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;
}

这似乎现在正在发挥作用!

答案 1 :(得分:2)

完整的nginx配置传递给tomcat上下文:

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;
    }
}