分类: LINUX
2011-05-31 17:32:21
LB1上的配置:
-------------------------
listen 127.0.0.1:8000
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
option httpchk HEAD /index.html HTTP/1.0
server webA 192.168.1.11:80 cookie A check
server webB 192.168.1.12:80 cookie B check
server webC 192.168.1.13:80 cookie C check
server webD 192.168.1.14:80 cookie D check
-------------------------
- LB1上的apache在443端口上接收clients的请求
- apache前传请求至绑定在127.0.0.1:8000上的haproxy
- 如果request不带cookie,则被前传至一台有效的server
- 在回复时,haproxy会在回复中插入一个包含server名称(如:A)的"SERVERID" cookie,和一个"Cache-control: private" header。那样apache就不会cache带这样cookie的page;
- 如果client再次访问时带了"SERVERID=A" cookie,则LB1会知道必须把该request前传至server A。haproxy会删除该cookie,而server不会看到它;
- 如果server "webA"宕机,request会被前传至另外一个有效的server,而cookie会被重置;
提示:
-------------------------
- 如果cookie工作在"prefix"模式下,haproxy就不需要配置"nocache"选项。因为,这个application cookie会被修改,并且application flags会被保留;
- 如果haproxy的前段使用了apache1.3,则它会一直禁用后端HTTP keep-alive,因此可以不必在haproxy上配置 "httpclose";
- 如果application需要知道client's IP,则在apache上配置X-Forwarded-For header,而不要在haproxy上配置;
数据流:
-------------------------
(apache) (haproxy) (server A)
>-- GET /URI1 HTTP/1.0 ------------> |
( no cookie, haproxy forwards in load-balancing mode. )
| >-- GET /URI1 HTTP/1.0 ---------->
| <-- HTTP/1.0 200 OK -------------<
( the proxy now adds the server cookie in return )
<-- HTTP/1.0 200 OK ---------------< |
Set-Cookie: SERVERID=A |
Cache-Control: private |
>-- GET /URI2 HTTP/1.0 ------------> |
Cookie: SERVERID=A |
( the proxy sees the cookie. it forwards to server A and deletes it )
| >-- GET /URI2 HTTP/1.0 ---------->
| <-- HTTP/1.0 200 OK -------------<
( the proxy does not add the cookie in return because the client knows it )
<-- HTTP/1.0 200 OK ---------------< |
>-- GET /URI3 HTTP/1.0 ------------> |
Cookie: SERVERID=A |
( ... )
如果只需要SSL而不需要cache,则stunnel是一个比Apache+mod_ssl更廉价的解决方案。stunnel默认不出HTTP并且不能增加X-Forwarded-For header,但是在haproxy的官方网站上有针对最新stunnel versions版本支持该特性的patch。这时,stunnel只是处理HTTPS而不处理HTTP。这也意味着haproxy会接收所有的HTTP访问。因此,haproxy需要在HTTP访问中增加X-Forwarded-For header,而不用对HTTPS访问做处理。因为,stunnel已经做过处理了。我们可以使用"except"关键字告诉haproxy,来自本地的连接已经有有效的header了。
192.168.1.1 192.168.1.11-192.168.1.14 192.168.1.2
-------+-----------+-----+-----+-----+--------+----
| | | | | _|_db
+--+--+ +-+-+ +-+-+ +-+-+ +-+-+ (___)
| LB1 | | A | | B | | C | | D | (___)
+-----+ +---+ +---+ +---+ +---+ (___)
stunnel 4 cheap web servers
haproxy
stunnel(LB1)上的配置:
cert=/etc/stunnel/stunnel.pem
setuid=stunnel
setgid=proxy
socket=l:TCP_NODELAY=1
socket=r:TCP_NODELAY=1
[https]
accept=192.168.1.1:443
connect=192.168.1.1:80
xforwardedfor=yes
haproxy(LB1)上的配置:
listen 192.168.1.1:80
mode http
balance roundrobin
option forwardfor except 192.168.1.1
cookie SERVERID insert indirect nocache
option httpchk HEAD /index.html HTTP/1.0
server webA 192.168.1.11:80 cookie A check
server webB 192.168.1.12:80 cookie B check
server webC 192.168.1.13:80 cookie C check
server webD 192.168.1.14:80 cookie D check
描述:
-------------
- LB1上的stunnel会在443上接收client的requests;
- stunnel前传request至绑定在80的haproxy;
- haproxy会在80端口接收HTTP client request,在同一个端口(80)上解析来自stunnel的SSL requests;
- stunnel增加X-Forwarded-For header(SSL 请求)
- haproxy在除了来自本地的地址(stunnel)的每个request中增加X-Forwarded-For header;