重定向问题nginx到haproxy到tomcat

时间:2014-05-12 13:53:15

标签: nginx haproxy

我有这样的问题。如果我在nginx配置中使用tomcat端口,那么应用程序运行正常。或者,如果我在nginx配置中使用ha-proxy端口并将请求重定向到来自nginx的tomcat端口,则安装程序不起作用,并且应用程序发生503错误.Below是我的nginx和haproxy配置文件以及tomcat / ha-proxy端口的详细信息。

应用程序是分布式的,下面是tomcat端口描述

/cas running on tomcat port 10039
/service running on tomcat port 10040
/running on tomcat port 10038

以下是使用的nginx端口的详细信息

 105 forwards request to 10039
 106 forwards request to 10040
 107 forwards request to 10038

以下是nginx配置文件的内容。

server {
    listen 80;
    server_name example-wh-qa.example.com;
    charset utf-8;
    rewrite     ^(.*)   https://$server_name$1 permanent;
  }

 server {
    listen 443;
    ssl on;
    ssl_certificate /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
    server_name example-wh-qa.example.com;
    error_log /var/log/nginx/example-warehouse-qa-error.log;
    charset utf-8;
    root /webroot-wh;

    location ~ ^/cas/(.*)$ {
            proxy_pass http://localhost:105;
            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;
    }

    location ~ ^/service/(.*)$ {
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_pass http://localhost:106;
            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;
    }

    location / {
            proxy_pass http://localhost:107;
            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;
            proxy_read_timeout 300;
            proxy_connect_timeout 300;
    }


   }

以下是haproxy文件的内容

global
    maxconn 200
    daemon
    #nbproc 8
    user haproxy
    group haproxy
    log 127.0.0.1 local0 warning
    log-send-hostname example.com
    defaults
    mode http
    log global
    timeout client 10m
    timeout server 10m
    timeout connect 4s
    option httpclose # Disable Keepalive
    option http_proxy
    balance roundrobin # Load Balancing algorithm
    option httpchk
    option forwardfor
    option redispatch
    option srvtcpka
    cookie JSESSIONID prefix
    stats enable
    stats refresh 10s
    stats realm Haproxy\ Statistics
    stats auth username1:passwd1
    stats auth username2:passwd2
    listen app1-proxy
    bind 127.0.0.1:105
    appsession JSESSIONID len 52 timeout 45m
    server app localhost:10039 weight 1 maxconn 1024 check cookie app1

 listen app2-proxy
    bind 127.0.0.1:106
    appsession JSESSIONID len 52 timeout 45m
    server app localhost:10040 weight 1 maxconn 1024 check cookie app2

 listen app3-sso-proxy
    bind 127.0.0.1:107
    appsession JSESSIONID len 52 timeout 45m
    server app localhost:10038  weight 1 maxconn 1024 check cookie app3-sso

现在在nginix配置文件的上面内容中,如果我替换" proxy_pass"中的haproxy端口直接使用tomcat端口,然后应用程序运行正常,但如果我在其间使用haproxy,则应用程序在访问时会开始抛出503错误。

作为附加信息,如果我使用tomcat端口(如

)在服务器上本地运行应用程序单个模块
elinks http://localhost:10039/cas/login  it works
elinks http://localhost:10040/service   it works
elinks http://localhost:10038   it works

但是在haproxy端口上运行它们除了最后一个链接外没有用。

elinks http://localhost:105/cas/login  it does not works
elinks http://localhost:106/service   it does not works
elinks http://localhost:107   it works

我非常需要配置haproxy以便在生产环境中对我的应用程序进行负载均衡。请帮我解决此配置问题。

1 个答案:

答案 0 :(得分:0)

你终于搞清楚了吗?我在设置中看到的东西

proxy_set_header    X-NginX-Proxy           true;

proxy_pass xx//localhost:106/service/;

,同样适用于xx//localhost:105/cas/login/。请注意使用' /'

结束网址