ubuntu 14.04 haproxy websocket配置,ws和wss请求

一、websocket请求头,和普通http请求不一样的是,使用Connection: Upgrade,Upgrade: websocket,可以通过判断Connection和Upgrade识别websocket请求

Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Cache-Control: no-cache
Connection: Upgrade
Host: www.test.com
Origin: chrome-extension://pfdhoblngboilpfeibdedpjgfnlcodoo
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: aorU1zG2arW3cv+R3IHxqQ==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36

二、ws请求配置

# HTTP
frontend http_80_in
    bind *:80
  
    ## routing based on websocket protocol header
    acl hdr_connection_upgrade hdr(Connection)  -i upgrade
    acl hdr_upgrade_websocket  hdr(Upgrade)     -i websocket
    use_backend websockets if hdr_connection_upgrade hdr_upgrade_websocket
  
backend websockets
    balance roundrobin
    cookie SERVERID
    server logic-02 172.31.1.2:80 check inter 2000 rise 3 fall 3 weight 1

三、wss请求配置,就需要使用https请求,需要将配置放到https里

# HTTPS
frontend https
    bind *:443 ssl crt /etc/ssl/haproxy-certs/www.test.com.pem crt /etc/ssl/haproxy-certs/www.test.cn.pem
    mode http
    
    ## routing based on websocket protocol header
    acl hdr_connection_upgrade hdr(Connection)  -i upgrade
    acl hdr_upgrade_websocket  hdr(Upgrade)     -i websocket
    use_backend websockets if hdr_connection_upgrade hdr_upgrade_websocket
 
backend websockets
    balance roundrobin
    cookie SERVERID
    server logic-02 172.31.1.2:80 check inter 2000 rise 3 fall 3 weight 1

四、参考

  • haproxy官方配置

你可能感兴趣的:(运维,haproxy)