栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

解决upstream prematurely closed connection while reading response header from upstream问题(nginx)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解决upstream prematurely closed connection while reading response header from upstream问题(nginx)

问题描述:

使用docker部署了前端和nginx,前端有需求要使用websocket,所以在nginx中配置了websocket转发,配置如图:

server {
    listen       80;
    server_name  127.0.0.1;

    charset utf-8;
    
    # websocket转发
    location /websocket/ {
        proxy_pass http://websocket:3600;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection $connection_upgrade;
    }}
}

发现报错upstream prematurely closed connection while reading response header from upstream,然后去网上找答案,有的说是ngxin缓存目录权限问题,有的说是 keepalive_timeout 时间设置太短,试了以后,发现均不能解决问题。

问题原因:

经过仔细排查,发现目标服务并没有收到转发,websocket服务需要的是http://ip:port/Infer/?token='abc',结果真实路径是http://ip:port/websocket/Infer/?token=‘abc’,网上查了那么多答案,没有一个人提到转发不对也可能造成这个错误,可能还是我太菜了,做前端太难了...      既然原因知道了,那解决很简单,就是让路径满足要求就行,修改为如下配置后websocket连接成功。

server {
    listen       80;
    server_name  127.0.0.1;

    charset utf-8;
    
    # websocket转发
    location /websocket/ {
        proxy_pass http://websocket:3600/;  #重点就是端口后的这个斜杠
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; 
        proxy_set_header Connection $connection_upgrade;
    }}
}
  知识延展:

上述问题,解决起来看似很简单,知识在proxy_pass路径后加 / ,那么这个斜杠到底该怎么用呢?规律如下:

原始请求:192.168.1.123:80/static/a.html
代理后ip: 192.168.2.321:81

# 情况1 location后没有/    转发网站没有/
实际转发后请求:192.168.2.321:81/static/a.html

# 情况2 location后有/     转发网站没有/
实际转发后请求:192.168.2.321:81/static/a.html


# 情况3 location后没有/    转发网站有/
实际转发后请求:192.168.2.321:81/a.html

192.168.1.123:80/static/a.html
# 情况4 location后有/      转发网站有/
实际转发后请求:192.168.2.321:81/a.html

#结论
proxy_pass后路径只要跟了斜杠,nginx转发时就会把location后对应的url部分去掉

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345816.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号