NGINX反向代理不适用于其他Docker容器

NGINX反向代理不适用于其他Docker容器

本文介绍了NGINX反向代理不适用于其他Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在docker compose中带有两个docker容器的设置.现在,我想使用nginx的proxy_pass功能来代理从nginx到其他容器的连接.

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

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

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;
        }

    }
}

当我现在访问 http://localhost:8081/configurator-api-gw/swagger我在error.log中遇到以下错误.我也尝试了不同的方法和其他一些示例,但是我不明白为什么它不起作用.

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.

推荐答案

我已经解决了这个问题.问题出在服务器apigateway.api:58732;在这里,需要在Docker网络内部将端口80用作端口.

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.

这篇关于NGINX反向代理不适用于其他Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:12