关于HAProxy(1.5.2)和Tomcat(7.0.54),我有几个问题。我都是新手。
在Tomcat中,我有一个应用程序,在登录时( https://my.tomcat.host:8080/access )将用户(通过303代码)重定向到另一个网页(< EM> http://my.tomcat.host:8080/access/sessionId=1234567 )。设置HAProxy我设置前端引擎(my-frontend-https)来接收https请求并将它们发送到后端(my-backend-https) - 然后将其作为http请求发送到tomcat服务器。
这就是haproxy.cfg(对于my.haproxy.host)的样子:
frontend my-frontend-https
bind *:8443 ssl crt /my/certs/server.pem
mode http
option httplog
default_backend my-backend-https
backend my-backend-https
balance roundrobin
mode http
option httplog
option forwardfor
server my-tomcat-srv my.tomcat.host:8080 check
在发送以下查询( https://my.haproxy.host:8443/access )时,我发现从tomcat返回的Location标志的格式为: http: //my.haproxy.host:80/access/sessionId=1234567 。看一下tomcat服务器,我发现我必须在server.xml中启用RemoteIPValve类,并将httpsServerPort设置为8443(作为protocolHeaderHttpsValue - 可能不需要这样做,因为它是默认值)。这似乎有效,我将在后端添加更多服务器。
所以这似乎很好,但我有几个问题:
如果我想在同一个haproxy.cfg文件中指向同一个tomcat实例的另一个前端/后端引擎(也就是说,上面说的是跨多个服务器的负载均衡,包括这个,我想要一个只有这一个的切入点)可以做到吗? 即haproxy将包含以下行。 前端my-frontend-https1 bind *:9443 ssl crt /my/certs/server.pem 模式http 选项httplog default_backend my-backend-https1
后端my-backend-https1 平衡圆形 模式http 选项httplog 期权转发 服务器my-tomcat-srv my.tomcat.host:8080检查
Location字段是否会以http://my.haproxy.host:8443/access/sessionId=1234567的形式返回,因为这是server.xml中定义的内容。感谢
哈罗德。
答案 0 :(得分:0)
这不是特定于Tomcat的,但是除非 haproxy 告知,否则您的Tomcat后端将不知道 haproxy 是前端。
您是否正在运行 nginx ,这直接来自我的配置:
proxy_set_header X-Forwarded-Proto https; # Obviously, differentiate based on whether you're serving an HTTP or HTTPS endpoint.
proxy_set_header Host $host; # Or hardcode a value.
proxy_set_header X-Real-IP $remote_addr; # Alternative to X-Forwarded-For.
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
虽然我没有为 haproxy 做这件事,因为我将 nginx 放在 haproxy 前面,根据{{3 }},您将需要执行以下操作:
# defaults section
option forwardfor
# frontend definition
frontend www-http
bind haproxy_www_public_IP:80
reqadd X-Forwarded-Proto:\ http
default_backend www-backend
frontend www-https
bind haproxy_www_public_IP:443 ssl crt /etc/ssl/private/example.com.pem
reqadd X-Forwarded-Proto:\ https
default_backend www-backend
# backend definition
backend www-backend
redirect scheme https if !{ ssl_fc }
server www-1 www_1_private_IP:80 check
server www-2 www_2_private_IP:80 check
这是基于Tomcat将引用标准ish字段X-Forwarded-Proto
和X-Forwarded-For
的假设。