简体   繁体   English

在 Docker 上使用 Nginx 重定向端口

[英]Redirecting ports with Nginx on Docker

I'm trying to build a simple Docker project, where you have few containers that are joined by one Nginx server.我正在尝试构建一个简单的 Docker 项目,其中只有几个容器由一个 Nginx 服务器连接。 Basically it's simpler simulation for my fullstack project.对于我的全栈项目来说,基本上它是更简单的模拟。 I have the problem with redirecting one containers main port to path in another project.我在将一个容器主端口重定向到另一个项目中的路径时遇到问题。

Project contains two modules and one docker-compose.yml file.项目包含两个模块和一个docker-compose.yml文件。 Expected behaviour is to see one html website on http://localhost and the other on http://localhost/api .预期行为是在 http://localhost 上看到一个 html 网站,在 http://localhost/api 上看到另一个。 When I run project, I see expected result on http://localhost , but to get to the other site i need to go to http://localhost:4000 .当我运行项目时,我在 http://localhost 上看到了预期的结果,但要访问其他站点,我需要转到 http://localhost:4000 。 How to fix it?如何解决?

Project files ( source code here )项目文件(源代码在这里

Module Client模块Client

index.html:索引.html:

this is website you should see under /api

Dockerfile: Dockerfile:

FROM node:14.2.0-alpine3.11 as build
WORKDIR /app
COPY . .
FROM nginx as runtime
COPY --from=build /app/ /usr/share/nginx/html
EXPOSE 80

Module Nginx模块Nginx

index.html : index.html

<p>this is index file. You should be able to go to <a href="/api">/api route</a></p>

default.conf : default.conf

upstream client {
    server client:4000;
}

server {
    listen 80;

    location /api {
        proxy_pass http://client;
    }

    location / {
        root /usr/share/nginx/html;
    }
}

Dockerfile: Dockerfile:

FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf 
COPY index.html /usr/share/nginx/html

Main directory主目录

docker-compose.yml file: docker-compose.yml文件:

version: "3"
services: 
    client: 
        build: Client
        ports:
            - 4000:80
    nginx:
        build: Nginx
        ports: 
            - 80:80
        restart: always
        depends_on: 
            - client

There is 2 issues that I can find in your configuration:我可以在您的配置中找到两个问题:

  1. You are redirecting to port 4000 on the client container, which you do not need to do, as the port 4000 is only relevant for your host machine.您正在重定向到客户端容器上的端口 4000,您不需要这样做,因为端口 4000 仅与您的主机相关。 So the upstream config should look like the following:因此上游配置应如下所示:
upstream client {
    server client;
}
  1. You are redirecting to /api on your client container, but your client container serves the content at /.您正在重定向到客户端容器上的 /api,但您的客户端容器在 / 处提供内容。 You should change your default.conf to look like the following (mind the trailing slashes!):您应该将 default.conf 更改为如下所示(注意尾部斜杠!):
upstream client {
    server client;
}

server {
    listen 80;

    location /api/ {
        proxy_pass http://client/;
    }

    location / {
        root /usr/share/nginx/html;
    }
}

With this configuration, you can enter http://localhost/api/ to get to your client container.使用此配置,您可以输入 http://localhost/api/ 以访问您的客户端容器。 If you want http://localhost/api to work, you could redirect /api to /api/ in your default.conf.如果你想让 http://localhost/api 工作,你可以在你的 default.conf 中将 /api 重定向到 /api/。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM