Nginx无法解析Docker容器中的服务名称

时间:2020-05-24 13:58:09

标签: docker nginx docker-compose

我试图在Docker容器中的Nginx上启动Angular客户端。

源代码为here

docker-compose文件:

 mongodb: 
    image: mongo 
    volumes:
      - mongodata:/data/db
    ports:
      - "27017:27017"
    networks:
      - backend   

  client:
    image: hantsy/angular-spring-reactive-sample-client
    environment:
      - "BACKEND_API_URL=http://api:8080/"
    ports:
      - "80:80"
    depends_on:
      - api
    networks:
      - frontend
      - backend

  api:
    image: hantsy/angular-spring-reactive-sample-server
    environment:
      - "SPRING_DATA_MONGODB_URI=mongodb://mongodb:27017/blog"
    ports:
      - "8080:8080"
    depends_on:
      - mongodb
    networks:
      - backend      

volumes:
  mongodata:  
#    driver: local-persist
#    driver_opts:
#      mountpoint: ./data  

networks:
  frontend:
  backend:

和Nginx配置一样,我使用Perl模块在运行时加载动态环境。

# see: https://hackerbox.io/articles/dockerised-nginx-env-vars/ to set env in nginx.conf

user              nginx;
worker_processes  1;
error_log         /var/log/nginx/error.log warn;
pid               /var/run/nginx.pid;
load_module       modules/ngx_http_perl_module.so;

env BACKEND_API_URL;

events {
    worker_connections  1024;
}

http {
    include                     /etc/nginx/mime.types;
    sendfile                    on;

    perl_set $baseApiUrl 'sub { return $ENV{"BACKEND_API_URL"}; }';

    server {
        listen          80;
        server_name     localhost;

        location /api { 
            proxy_http_version 1.1;
            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_set_header X-Forwarded-Proto  $scheme;

            rewrite ^/api/(.*) /$1 break;
            proxy_pass ${baseApiUrl};
        }

        location /test {
            return 200 'the environment variable contains: ${baseApiUrl}';
        }

        location / {
            try_files $uri $uri/ index.html;
        }
    }
}

用于Angular客户端的Dockerfile。

# Set nginx base image
FROM nginx:alpine-perl

LABEL maintainer="Hantsy Bai"

EXPOSE 80
## Remove default Nginx website
RUN rm -rf /usr/share/nginx/html/*

RUN rm /etc/nginx/conf.d/default.conf

ADD ./nginx/nginx.conf /etc/nginx/nginx.conf

ADD ./client/dist /usr/share/nginx/html 

使用docker-compose up命令构建和运行应用程序并尝试访问登录时,它将提示/api URI,该URI被配置为通过API服务。

但是我在浏览器控制台中获得了 502网关

在Nginx日志记录中,我看到了以下信息。

client_1   | 2020/05/24 13:47:21 [error] 9#9: *4 no resolver defined to resolve api, client: 172.20.0.1, server: localhost, request: "GET /api/auth/user HTTP/1.1", host: "localhost", referrer: "http://localhost/auth/signin"

api 服务名称在Nginx中无法解析。我已经测试过api uri http://localhost:8080,并确保它可用。

更新:我在client码头上运行了ping命令,并得到了结果。

❯ docker exec 05dd7dfb01ab ping api
PING api (172.20.0.4): 56 data bytes
64 bytes from 172.20.0.4: seq=0 ttl=64 time=0.118 ms
64 bytes from 172.20.0.4: seq=1 ttl=64 time=0.259 ms
64 bytes from 172.20.0.4: seq=2 ttl=64 time=0.171 ms
64 bytes from 172.20.0.4: seq=3 ttl=64 time=0.091 ms
64 bytes from 172.20.0.4: seq=4 ttl=64 time=0.223 ms
64 bytes from 172.20.0.4: seq=5 ttl=64 time=0.169 ms
64 bytes from 172.20.0.4: seq=6 ttl=64 time=0.220 ms

已解决::在 nginx.conf 中添加resolver 127.0.0.11硬代码以暂时解决此问题。

0 个答案:

没有答案