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

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;
    }

效果

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

薯条大爹

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

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

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

打赏作者

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

抵扣说明:

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

余额充值