核心配置就是proxy_set开头的部分,主要是设置转发头Upgrade $http_upgrade和Connection ‘upgrade’这两个,下面是主要代码部分
server {
listen 80;
server_name api.wahahahaohe.com;
#当访问socket.io这个路径时转发websocket请求
#例如api.wahahahaohe.com/socket.io/userinfo
location /socket.io {
# 这里是重点,转发websocket需要的设置
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X_Forward_For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_pass http://127.0.0.1:8080/;
}
}