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

效果

在这里插入图片描述

发布了34 篇原创文章 · 获赞 96 · 访问量 23万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览