Docker Nginx [error] 5#5: *1 connect() failed (111: Connection refused)while connecting to upstream

本文详细记录了使用Docker容器化SpringCloud服务并结合Nginx和SSL进行部署的过程,重点解决了Nginx启动后连接失败的问题,通过调整proxy_pass参数指向正确的服务地址,实现了稳定运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Docker安装nginx 启动后报错

这是nginx error.log原话

[error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.com, request: “GET /v1/api/xxxx HTTP/1.1”, upstream: “http://127.0.0.1:8091/v1/xxx”, host: “xxx.xxx.com”

我这个服务相对简单 Docker容器 spring cloud服务分别是:service服务提供、网关api、运营api、 端口分别是8090服务、8091网关api、8092运营 api
效果如下:
在这里插入图片描述
这没什么好说的
然后开始搭建nginx

docker run --name nginx -d -p 80:80 -p 443:443 -v /docker/nginx/logs:/var/log/nginx -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /docker/nginx/www:/usr/share/nginx/html nginx

开启ssl
最后把安全证书 cp到容器 :docker cp /root/cert/ 0f186a79f090:/root/
最后启动访问:docker start 0f186a79f090
访问后:
在这里插入图片描述

Nginx Error:

2020/03/18 04:45:15 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /v1/api/xxx HTTP/1.1", upstream: "http://127.0.0.1:8091/v1/api/xxx", host: "xxx.xxx.xxx.xxx"
2020/03/18 04:45:15 [error] 5#5: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8091/xxx", host: "xxx.xxx.xxx.xxx", referrer: "https://xxx.xxx.xxx.xxx/v1/api/xxxx"

解决方案:
我的conf server是这样配置的

server {
    # IPv4
    listen  80;
    listen  443 ssl;
    # IPv6
    listen  [::]:80;
    listen  [::]:443 ssl;

    server_name xxx.xxx.xxx.xxx;

    # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate
    ssl_certificate     /root/cert/xxx.xxx.xxx.xxx.pem;
    ssl_certificate_key /root/cert/xxx.xxx.xxx.xxx.key;

    # curl https://ssl-config.mozilla.org/ffdhe2048.txt > /etc/nginx/ssl/dhparam.pem
    # ssl_dhparam ssl/dhparam.pem;
    location / {
	client_max_body_size 20M;
	proxy_pass  http://xxx.xxx.xxx.xxx:8091;
	proxy_set_header Host       $host;
    proxy_set_header X-Real-IP  $remote_addr;
    }

    access_log  /var/log/nginx/xxx.xxx.xxx.xxx.log main;
    error_log   /var/log/nginx/xxx.xxx.xxx.xxx.log;
}

注意这里:

location / {
	client_max_body_size 20M;
	# 我的问题就是这里 原来是:proxy_pass  http://localhost:8091;
	# 后来改成:proxy_pass  http://127.0.0.1:8091; 也不行!
	# 改成解决:proxy_pass  http://xxx.xxx.xxx:8091; ok了!(xxx.xxx.xxx是你服务器公网IP 如:139.172.99.21:8091)
	proxy_pass  http://xxx.xxx.xxx.xxx:8091;
	proxy_set_header Host       $host;
    proxy_set_header X-Real-IP  $remote_addr;
    }

效果

在这里插入图片描述

<think>好的,用户遇到了Nginx代理Spring Boot应用返回502错误的问题,我需要帮他们分析可能的原因和解决方法。首先,我应该从常见的几个方面入手,比如后端服务是否运行、Nginx配置是否正确、网络连接是否正常,还有超时设置和资源限制等。 首先,用户需要确认Spring Boot应用是否真的在运行,并且监听了正确的端口。有时候应用可能因为启动失败或者端口被占用而没有运行起来,这时候Nginx自然无法连接,导致502错误。建议用户检查应用的日志,看看有没有启动错误,或者用netstat命令确认端口监听情况。 接下来是Nginx的配置问题。代理设置的地址和端口是否正确?比如proxy_pass后面的地址是不是127.0.0.1:8080,或者对应的内部服务地址。这里容易出错的地方是写成了localhost,有时候需要明确用IP地址。另外,可能需要检查是否有拼写错误,比如端口号是否正确。 然后是网络连接问题。如果Nginx和应用不在同一台服务器上,需要确保防火墙没有阻止端口访问。比如,云服务器可能需要配置安全组规则。用户可以使用telnet或者curl命令测试从Nginx服务器能否访问后端端口,确认连通性。 超时设置也是一个常见问题。如果后端处理时间较长,而Nginx的proxy_read_timeout设置得太短,可能会导致超时断开。这时候需要适当增加超时时间,比如设置为60秒或更长,根据应用需求调整。 资源限制方面,如果Spring Boot应用处理请求时消耗了大量资源,导致无法及时响应,也会出现502。需要检查服务器的CPU、内存使用情况,以及应用的线程池配置,确保有足够的资源处理请求。 SSL/TLS配置错误也可能导致问题,特别是当后端使用HTTPS时。需要确认Nginx的proxy_pass是否正确指向了HTTPS的地址,并且证书是否有效。有时候忽略证书验证的配置项可能能暂时解决问题,但生产环境不建议这么做。 另外,反向代理的头部信息是否正确传递也很重要。比如Host头、X-Forwarded-For头等,确保应用能正确处理这些信息。可能需要添加proxy_set_header指令来设置正确的头部。 还有可能是应用本身的问题,比如启动时发生异常,或者数据库连接失败,导致无法处理请求。查看Spring Boot的日志,尤其是启动时的错误信息和访问日志,可以帮助定位问题。 最后,如果以上步骤都检查过了,还是无法解决,可能需要逐步排查。比如临时关闭防火墙测试,调整Nginx配置,或者使用调试工具如strace查看进程状态。同时,重启Nginx和应用服务有时也能解决临时性问题。 需要提醒用户按照步骤逐一检查,确认每个环节都没有问题。同时,注意安全相关配置,比如不要在生产环境随意关闭证书验证。如果用户对某些步骤不熟悉,可能需要更详细的命令示例或配置片段来指导操作。</think>当Nginx代理Spring Boot应用返回502 Bad Gateway错误时,通常表示Nginx无法与后端服务建立有效连接。以下是逐步排查与解决方案: --- ### 一、基础检查 1. **确认Spring Boot服务状态** - 执行 `ps -ef | grep java` 或 `systemctl status your-springboot-service` 检查进程是否存活 - 查看日志 `tail -f /var/log/springboot-app.log` (路径根据实际配置调整) 2. **验证端口监听** ```bash netstat -tuln | grep 8080 # 替换为实际端口 ``` 若未显示`LISTEN`状态,说明应用未启动或端口被占用 --- ### 二、Nginx配置检查 1. **代理地址正确性** ```nginx location / { proxy_pass http://127.0.0.1:8080; # 确保IP和端口匹配Spring Boot的server.port proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } ``` - 注意:若使用Docker容器,需改用容器IP而非`127.0.0.1` 2. **超时配置调整** ```nginx proxy_connect_timeout 60s; proxy_read_timeout 60s; proxy_send_timeout 60s; ``` --- ### 三、网络连通性验证 1. **Nginx服务器测试连接** ```bash telnet 127.0.0.1 8080 # 本地测试 curl -v http://127.0.0.1:8080/health # 测试接口 ``` - 若不通,检查防火墙: ```bash sudo ufw status # Ubuntu sudo firewall-cmd --list-ports # CentOS ``` 2. **云服务器安全组配置** - 确保入站规则开放了Spring Boot端口(TCP协议) --- ### 四、后端服务深度排查 1. **Spring Boot启动参数检查** - 确认`application.properties`中: ```properties server.address=0.0.0.0 # 允许外部访问 server.port=8080 #Nginx配置一致 ``` 2. **内存溢出检查** - 在启动命令中添加内存日志: ```bash java -XX:+HeapDumpOnOutOfMemoryError -jar your-app.jar ``` - 使用`jstat -gcutil <pid> 1000`监控GC状态 --- ### 五、进阶调试方案 1. **Nginx错误日志分析** ```nginx error_log /var/log/nginx/error.log debug; # 临时开启debug日志 ``` 典型错误示例: ``` connect() failed (111: Connection refused) while connecting to upstream ``` 2. **使用tcpdump抓包** ```bash sudo tcpdump -i lo port 8080 -w nginx_debug.pcap ``` 分析Nginx与Spring Boot的TCP握手过程 --- ### 六、常见问题总结 | 问题类型 | 特征表现 | 解决方案 | |----------------|----------------------------|-------------------------------| | 端口未监听 | `Connection refused` | 重启Spring Boot应用 | | 响应超时 | `upstream timed out` | 增大`proxy_read_timeout`值 | | 请求头过大 | `client intended to send too large header` | 添加`proxy_buffer_size 128k;` | | SSL证书问题 | 代理HTTPS后端未验证证书 | 添加`proxy_ssl_verify off;`(仅测试环境)| --- 通过以上步骤逐步排查,可解决90%的Nginx 502问题。若仍无法解决,建议同时抓取Nginx访问日志、Spring Boot应用日志及系统资源监控数据进行综合分析。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

薯条大爹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值