本文已参与「新人创作礼」活动,一起开启掘金创作之路。
修改nginx.conf配置文件
http模块添加2处
map $http_upgrade $connection_upgrade {
default upgrade;
'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。
}
server {
listen 80;
location /boot {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://localhost:8080;
}
}
完整配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 添加第一处配置
map $http_upgrade $connection_upgrade {
default upgrade;
'websocket' upgrade; #如果为websocket 则为 upgrade 可升级的。
}
limit_req_zone $binary_remote_addr zone=mylimit:30m rate=2500r/s;
server {
listen 80;
location /boot {
proxy_http_version 1.1; # 添加长连接支持
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://localhost:8080;
}
}
}
proxy_http_version 1.1 说明
nginx在反向代理HTTP协议的时候,默认使用的是HTTP1.0去向后端服务器获取响应的内容后在返回给客户端。 HTTP1.0和HTTP1.1的一个不同之处就是,HTTP1.0不支持HTTP keep-alive。
如果要转发Websocket请求,必须要指定 proxy_http_version 1.1 主要是为了长连接有效