error:\*1035 connect() failed (111: Connection refused) while connecting to upstream, client.....

本文介绍了一次在腾讯云服务器上部署项目时遇到的问题,即前端向后端发送请求失败,显示连接被拒绝的错误。通过调整Nginx配置文件,增加了proxy_set_header Host$http_host;一行代码解决了问题。

记录一次错误error:1035 connect() failed (111: Connection refused) while connecting to upstream, client: ...217, server: .com, request: “POST /api/userLogin HTTP/1.1”, upstream: "http://.1:8443/userLogin", host: "*.com"。

一、起因

该项目的部署是在腾讯云服务器上,http升级为https,使用的是腾讯ssl证书,阿里的域名,在nginx.conf配置过程中,出现前端向后端发送请求失败问题。出现以下错误:
在这里插入图片描述

nginx.conf配置如下:

SSL 证书 Nginx 服务器 SSL 证书安装部署-证书安装-文档中心-腾讯云 (tencent.com)

server{
	#SSL 默认访问端口号为 443
    listen       443 ssl;
    server_name  域名;
    default_type text/html;
    ssl_certificate 证书文件路径(.crt/.pem);
    ssl_certificate_key 私钥文件路径(.key);
    ssl_session_timeout 5m;
    #请按照以下协议配置
    ssl_protocols TLSv1.2 TLSv1.3;
    s#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    location / {
        root /usr/share/nginx/html/dist/;
        try_files $uri $uri/ /index.html;
        index index.html;
    }

    location /api/ {
        default_type application/json;
        proxy_pass http://localhost:8443/;
    }
}

二、解决办法

在nginx.conf配置中增加一条: proxy_set_header Host $http_host;

server{
	#SSL 默认访问端口号为 443
    listen       443 ssl;
    server_name  域名;
    default_type text/html;
    ssl_certificate 证书文件路径(.crt/.pem);
    ssl_certificate_key 私钥文件路径(.key);
    ssl_session_timeout 5m;
    #请按照以下协议配置
    ssl_protocols TLSv1.2 TLSv1.3;
    s#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    location / {
        root /usr/share/nginx/html/dist/;
        try_files $uri $uri/ /index.html;
        index index.html;
    }

    location /api/ {
    	# nginx反向代理重写请求头中的host字段属性
        proxy_set_header Host $http_host;
        default_type application/json;
        proxy_pass http://localhost:8443/;
    }
}

三、分析

3.1 nginx中proxy_set_header Host $host的作用

nginx为了实现反向代理的需求而增加了一个ngx_http_proxy_module模块。其中proxy_set_header指令就是该模块需要读取的配置文件。在这里,所有设置的值的含义和http请求体中的含义完全相同,除了Host外还有X-Forward-For。

Host的含义是表明请求的主机名,因为nginx作为反向代理使用,而如果后端真实服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败【默认反向代理服务器会向后端真实服务器发送请求,并且请求头中的host字段应为proxy_pass指令设置的服务器】

2025/12/03 14:27:31 [notice] 2795#0: signal process started 2025/12/03 14:29:40 [error] 4162#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:29:40 [error] 4161#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://[::1]:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:29:40 [error] 4162#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://127.0.0.1:20606/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:29:40 [error] 4162#0: *2 no live upstreams while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://localhost/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:29:40 [error] 4161#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://127.0.0.1:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:29:40 [error] 4162#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://127.0.0.1:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 14:49:24 [notice] 3956#0: signal process started 2025/12/03 18:41:07 [error] 4678#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://[::1]:20606/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 18:41:07 [error] 4677#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/03 18:54:54 [error] 4677#0: *20 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/calibration HTTP/1.1", upstream: "http://[::1]:20606/api/v1/calibration", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:55:00 [error] 4678#0: *26 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://[::1]:20606/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:55:31 [error] 4677#0: *34 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "DELETE /api/v1/logout HTTP/1.1", upstream: "http://[::1]:20606/api/v1/logout", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:58:04 [error] 4677#0: *37 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/calibration HTTP/1.1", upstream: "http://[::1]:20606/api/v1/calibration", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:58:04 [error] 4678#0: *46 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://[::1]:20606/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:58:16 [error] 4678#0: *46 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/jamming HTTP/1.1", upstream: "http://[::1]:20606/api/v1/jamming", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/system" 2025/12/03 18:58:19 [error] 4677#0: *41 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/05 10:09:12 [error] 926#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/05 10:09:12 [error] 927#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/config HTTP/1.1", upstream: "http://[::1]:20606/api/v1/config", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/dashboard" 2025/12/05 10:09:23 [error] 927#0: *13 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map" 2025/12/05 10:09:31 [error] 926#0: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/compasscal HTTP/1.1", upstream: "http://[::1]:20606/api/v1/compasscal", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map" 2025/12/05 10:09:43 [error] 926#0: *90 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://[::1]:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/detection" 2025/12/05 10:09:56 [error] 926#0: *99 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://[::1]:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map" 2025/12/05 10:09:57 [error] 927#0: *91 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://[::1]:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map" 2025/12/05 10:10:31 [error] 926#0: *130 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/state HTTP/1.1", upstream: "http://[::1]:20606/api/v1/state", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map" 2025/12/05 10:10:33 [error] 927#0: *133 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.4.110, server: localhost, request: "GET /api/v1/detect/message HTTP/1.1", upstream: "http://[::1]:20606/api/v1/detect/message", host: "192.168.4.222:21606", referrer: "http://192.168.4.222:21606/map"
最新发布
12-06
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值