nginx反向代理配置以支持websock通信

作者:fyupeng
技术专栏:☞ https://github.com/fyupeng
项目文档:☞ https://rnf.cool
项目同步地址:☞ 预览


在nginx的conf/nginx.cnf配置文件中,配置http模块server代理模块

配置 http 模块

添加变量$http_upgrade$connection_upgrade

http{
	  map $http_upgrade $connection_upgrade { 
		    default          keep-alive;  #默认为keep-alive 可以支持 一般http请求
		    'websocket'      upgrade;     #如果为websocket 则为 upgrade 可升级的。
	  }
}

配置 server 代理模块

引用变量

server {
        listen       80;
        server_name  localhost;

        location / {
                proxy_pass http://localhost:8080/;
                # support websocket
                proxy_set_header Upgrade $http_upgrade; #此处配置 上面定义的变量
                proxy_set_header Connection $connection_upgrade;
        }
}