nginx docker 无法将代理传递到本地主机,不断给出 502 bad gateway

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

我正在尝试设置一个代理 docker 并将其转发到在本地主机上的 docker 外部运行的服务器。

我的 yml 文件

services:
    proxy:
        image: nginx:alpine
        volumes:
          - "./nginx.conf:/etc/nginx/nginx.conf"
        ports:
          - "0.0.0.0:9020:9020"
        environment:
          - TZ=Europe/Amsterdam
        healthcheck:
          interval: 15s
          test: ["CMD-SHELL", "curl --fail -s http://localhost:9020/health || exit 1"]
        extra_hosts:
          - "host.docker.internal:host-gateway"

我的 nginx.conf:

# Nginx expects this section to be defined, even if empty
events {

}

http {
    server {
       server_name "proxy";
        location / {
            proxy_pass http://host.docker.internal;
        }
        location /health {
            access_log off;
            add_header 'Content-Type' 'text/plain';
            return 200 "healthy\n";
        }
        listen 9020;
    }
}

退出并出现错误:

2024/03/14 12:03:53 [error] 22#22: *1 connect() failed (110: Operation timed out) while connecting to upstream, client: 192.168.32.1, server: proxy, request: "GET / HTTP/1.1", upstream: "http://172.17.0.1:80/", host: "localhost:9020"

我试过了

proxy_pass http://172.17.0.1;

docker nginx
1个回答
0
投票

如果您访问 docker 网络之外的服务,请确保在主机上打开该端口:

sudo ufw allow 9000
© www.soinside.com 2019 - 2024. All rights reserved.