[英]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?如何解决?
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
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
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:我可以在您的配置中找到两个问题:
upstream client {
server client;
}
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.