项目中应用了websocket以后,需要在nginx上做配置,不然请求就是404.
重点来了
nginx.conf下面
http节点中,需要增加
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
这个配置节定义了$http_upgrade 和$connection_upgrade变量,
很多网站光贴配置文件,没贴这个map,导致你配置上去nginx启动不了
然后在自己的server配置节增加
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
这个配置节,在header中增加属性,这样后台才能解析到。
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
access_log /var/log/nginx/test_websocket.access.log main;
location / {
proxy_pass http://127.0.0.1:8888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
上面配置是conf.d中vhost文件配置。