NGINX reverse proxy not working to other docker container

风格不统一 提交于 2021-01-29 09:28:08

问题


I have a setup with two docker containers in a docker compose. Now I want to use proxy_pass feature from nginx to proxy the connection from nginx to other container.

docker compose

version: '3.4'
services:
  reverse_proxy: 
    image: nginx:alpine
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./error.log:/var/log/nginx/error.log
    ports:
      - "8081:8081"
  apigateway.api:
    image: ${REGISTRY:-configurator}/apigateway.api:${TAG:-latest}  
    build:
      context: .
      dockerfile: src/Services/ApiGateway/ApiGateway.Api/Dockerfile
    environment:
      - ASPNETCORE_ENVIRONMENT=Development
      - ASPNETCORE_URLS=http://0.0.0.0:80
    ports:
      - "58732:80" 

nginx conf

worker_processes 1;

events {
    multi_accept on;
    worker_connections 65535;
}

http {
    charset utf-8;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    server_tokens off;
    log_not_found off;
    types_hash_max_size 2048;
    client_max_body_size 16M;

    # MIME
    include mime.types;
    default_type application/octet-stream;

    upstream apigateway {
        server apigateway.api:58732;
    }

    server {
        listen 8081;
        # reverse proxy
        location /configurator-api-gw/ {
            proxy_pass http://apigateway/;
            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_set_header   X-Forwarded-Host $server_name;
        }

    }
}

When I now access http://localhost:8081/configurator-api-gw/swagger I'm getting following error in the error.log. I have tried also different approaches and some other examples but I don't get it why this is not working.

2019/03/08 06:30:29 [error] 7#7: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.28.0.1, server: , request: "GET /configurator-api-gw/swagger HTTP/1.1", upstream: "http://172.28.0.5:58732/swagger", host: "localhost:8081"


回答1:


I have solved the problem. The problem is with server apigateway.api:58732; Here it needs to be used Port 80 as this inside of the docker network.



来源:https://stackoverflow.com/questions/55058427/nginx-reverse-proxy-not-working-to-other-docker-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!