Nginx支持websocket

通过配置使Nginx支持websocket代理。

一、http配置
配置map模块。

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

keepalive_timeout 65;
client_max_body_size 600m;

#增加一下配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

include /etc/nginx/conf.d/*.conf;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
二、location配置
设置Upgrade和Connection两个消息头。

location / {
#增加以下两行配置
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_pass http://xxx:8080;
}
————————————————
版权声明:本文为CSDN博主「魔希达」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/l2931050/article/details/124110151

posted @ 2023-08-10 22:05  China Soft  阅读(292)  评论(0编辑  收藏  举报