【发布时间】:2014-06-29 22:17:02
【问题描述】:
我遇到了这样的问题。如果我在 nginx 配置中使用 tomcat 端口,则应用程序运行良好。或者,如果我在 nginx 配置中使用 ha-proxy 端口并将请求从 nginx 重定向到 tomcat 端口,则安装程序不起作用并且应用程序发生 503 错误。以下是我的 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 以在生产中平衡我的应用程序。请帮助我解决这个配置问题。
【问题讨论】: