微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx 反向代理到其他 nginx 502 错误网关

如何解决nginx 反向代理到其他 nginx 502 错误网关

我想创建两个服务,它们都有自己的 Nginx。我想使用第三个 Nginx 作为这两个的反向代理,但我得到了

502 错误网关

当我请求时

http://127.0.0.1:8080/
http://127.0.0.1:8080/one
http://127.0.0.1:8080/two

访问:

http://127.0.0.1:8081
http://127.0.0.1:8082

工作正常

我有这个 docker-compose.yml

version: "3.3"
services:
  Nginx-one:
    image: Nginx:1.17.8
    ports:
      - "8081:80"
    networks: 
      - frontend
      - backend
    volumes: 
      - ./Nginx-one/html:/usr/share/Nginx/html
  Nginx-two:
    image: Nginx:1.17.8
    ports:
      - "8082:80"
    networks: 
      - frontend
      - backend
    volumes: 
      - ./Nginx-two/html:/usr/share/Nginx/html
  nginx-reverse-proxy:
    image: Nginx:1.17.8
    ports:
      - "8080:80"
    networks: 
      - frontend
      - backend
    volumes: 
      - ./nginx-reverse-proxy/html:/usr/share/Nginx/html
      - ./nginx-reverse-proxy/conf.d:/etc/Nginx/conf.d
  debian-network:
    image: cslev/debian_networking
    stdin_open: true # docker run -i
    tty: true        # docker run -t
    networks: 
      - frontend
      - backend
networks:
  frontend:
    internal: false
  backend:
    internal: true

和目录结构

.
├── docker-compose.yml
├── Nginx-one
│   └── html
│       └── index.html
├── nginx-reverse-proxy
│   ├── conf.d
│   │   └── default.conf
│   └── html
│       └── index.html
├── Nginx-two
    └── html
        └── index.html

Nginx.conf 内容

user  Nginx;
worker_processes  1;

error_log  /var/log/Nginx/error.log warn;
pid        /var/run/Nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/Nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/Nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/Nginx/conf.d/*.conf;
}

conf.d/default.conf

server {

    listen 80;

    location /one {
        proxy_pass http://127.0.0.1:8081/;
    }

    location /two {
        proxy_pass http://127.0.0.1:8082/;
    }
}

当我评论这行 docker-compose.yml

# ./nginx-reverse-proxy/conf.d:/etc/Nginx/conf.d

所以 conf.d/default.conf 没有使用,我从主机的浏览器请求:

http://127.0.0.1:8080/

它从 nginx-reverse-proxy 本身给出了正确的响应,但很明显

http://127.0.0.1:8080/one
http://127.0.0.1:8080/two

不要提供来自

的任何回复
http://127.0.0.1:8081
http://127.0.0.1:8082

而是 404。

docker ps 输出

IMAGE                     COMMAND                  CREATED          STATUS         PORTS                  NAMES
cslev/debian_networking   "bash"                   25 minutes ago   Up 3 minutes                          Nginxproblem_debian-network_1
Nginx:1.17.8              "Nginx -g 'daemon of…"   47 minutes ago   Up 3 minutes   0.0.0.0:8080->80/tcp   Nginxproblem_nginx-reverse-proxy_1
Nginx:1.17.8              "Nginx -g 'daemon of…"   14 hours ago     Up 3 minutes   0.0.0.0:8082->80/tcp   Nginxproblem_Nginx-two_1
Nginx:1.17.8              "Nginx -g 'daemon of…"   14 hours ago     Up 3 minutes   0.0.0.0:8081->80/tcp   Nginxproblem_Nginx-one_1

运行这个脚本:

#!/bin/sh

docker exec Nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080
docker exec Nginxproblem_debian-network_1 curl -sS Nginxproblem_nginx-reverse-proxy_1:8080

docker exec Nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080/one
docker exec Nginxproblem_debian-network_1 curl -sS Nginxproblem_nginx-reverse-proxy_1:8080/one
docker exec Nginxproblem_debian-network_1 curl -sS 127.0.0.1:8080/two
docker exec Nginxproblem_debian-network_1 curl -sS Nginxproblem_nginx-reverse-proxy_1:8080/two

docker exec Nginxproblem_debian-network_1 curl -sS 127.0.0.1:8081
docker exec Nginxproblem_debian-network_1 curl -sS Nginxproblem_Nginx-one_1:8080

docker exec Nginxproblem_debian-network_1 curl -sS 127.0.0.1:8082
docker exec Nginxproblem_debian-network_1 curl -sS Nginxproblem_Nginx-two_1:8082

给出:

curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to Nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to Nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8080: Connection refused
curl: (7) Failed to connect to Nginxproblem_nginx-reverse-proxy_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8081: Connection refused
curl: (7) Failed to connect to Nginxproblem_Nginx-one_1 port 8080: Connection refused
curl: (7) Failed to connect to 127.0.0.1 port 8082: Connection refused
curl: (7) Failed to connect to Nginxproblem_Nginx-two_1 port 8082: Connection refused

- ./nginx-reverse-proxy/conf.d:/etc/Nginx/conf.d 没有被评论时,

与从浏览器访问 IP 地址相反,注释 - ./nginx-reverse-proxy/conf.d:/etc/Nginx/conf.d 时的输出相同。

解决方法

除非您启用了 host networking,否则 127.0.0.1 将指向容器本身。您可以通过服务名称从同一网络中的容器内部引用其他两个容器,例如nginx-onenginx-two

您还将容器端口 80 映射到主机上的端口 8080/8081/8082。然而,这对同一网络中的容器之间的通信没有任何作用。检查the docs

默认情况下,当您使用 docker create 或 docker run 创建或运行容器时,它不会向外界发布任何端口。要使端口可用于 Docker 之外的服务或未连接到容器网络的 Docker 容器,请使用 --publish 或 -p 标志。这会创建一个防火墙规则,将容器端口映射到 Docker 主机上的端口到外部世界。

因此,尝试将 http://127.0.0.1:8081/; 更改为 http://nginx-one/;,它应该可以工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。