nginx 代理 ws协议


ws协议在http之上,直接在http协议里面进行协议升级即可

正常的http请求正常,ws请求也正常,使用同一个端口

正常的ws请求截图

代理配置

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    charset 	    utf-8;
    #tcp_nopush     on;
    keepalive_timeout  120;
    client_header_buffer_size 10240k;
	
    server {
        listen       8900;
        server_name  localhost;
	# 使用正则表达式过滤我需要的请求进行协议升级
        location ~* /sms/.*\.(flv|hls)$ {
            proxy_pass http://YOUR_WS_SERVER:PORT;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
		
        location / {
            root html;
        }
        error_page   500 502 503 504  /50x.html;
    }
}

前端测试文件




    
    
    
    Document