Docker中的Nginx抛出502 Bad Gateway

问题描述 投票:0回答:1

我正在尝试在Nginx Web服务器后面运行名为Grafana的服务,其中这两个服务都在docker-compose文件中运行。docker-compose.yml:

version: '3.1'

services:
  nginx:
    image: nginx
    ports: ['443:443',"80:80"]
    restart: always
    volumes:
      - ./etc/nginx.conf:/etc/nginx/nginx.conf:ro
      - /home/ec2-user/certs:/etc/ssl

  grafana:
    image: grafana/grafana
    restart: always
    ports: ["3000:3000"]

nginx.conf:

events {
  worker_connections  1024;
}

    http {
        ssl_certificate /etc/ssl/cert.pem;
        ssl_certificate_key /etc/ssl/key.pem;
        server {
            listen 443 ssl;
            server_tokens off;
            location /grafana/ {
                rewrite /grafana/(.*) /$1  break;
                proxy_pass http://127.0.0.1:3000/;
                proxy_redirect     off;
                proxy_set_header   Host             $host;
                proxy_set_header   X-Real-IP        $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_bind $server_addr;
            }
        }

    }

grafana服务正在端口3000上运行。我的目标是从地址https://1.1.1.1/grafana上从外部访问此Nginx服务器(假定其公共IP地址为:1.1.1.1)。使用当前配置,我得到502 Bad Gateway和nginx端的错误:

 (111: Connection refused) while connecting to upstream, client: <<my-public-ip-here>>,
nginx docker-compose nginx-location
1个回答
0
投票

您的容器在docker网络中的两个单独IP地址上运行,默认情况下通常为172.17..

[通过在nginx容器中使用这样的代理传递:

proxy_pass http://127.0.0.1:3000/

您实际上是在告诉它要在端口3000 local上寻找一个进程,因为127.0.0.1对吗?

您需要将其指向Grafana容器的方向,尝试这样做:

docker inspect <grafana ID> | grep IPAddress

然后将代理传递设置为该IP:

proxy_pass http://172.0.0.?:3000/
© www.soinside.com 2019 - 2024. All rights reserved.