[英]Reverse proxy nginx docker container with subdomains
I have a docker-compose.yml file with multiple services connected to the same network.我有一个 docker-compose.yml 文件,其中有多个服务连接到同一网络。 the services include nginx exposes port 80, a web app(app1) running on 0.0.0.0:8000, a second web app(app2) running on 0.0.0.0:7000, yet another web app(app3) running on 0.0.0.0:5000 and so on这些服务包括 nginx 公开端口 80、运行在 0.0.0.0:8000 上的网络应用程序(app1)、运行在 0.0.0.0:7000 上的第二个网络应用程序(app2),以及运行在 0.0.0.0 上的另一个网络应用程序(app3): 5000 等等
I can access all the apps from the browser when a go to 0.0.0.0:8000 or 0.0.0.0:5000 etc当转到 0.0.0.0:8000 或 0.0.0.0:5000 等时,我可以从浏览器访问所有应用程序
I want to access the first app with "http://localhost" from the browser ie the first web app to be the root.我想从浏览器访问第一个带有“http://localhost”的应用程序,即第一个作为根的 web 应用程序。 then the others to be subdomains for example;然后其他人是子域,例如; for app2 to be access from this: "http://localhost/app2"要从此访问 app2:“http://localhost/app2”
WHAT I HAVE TRIED我的尝试
I have created a server block for each web app as shown bellow我为每个 Web 应用程序创建了一个服务器块,如下所示
>> cat app1.conf
upstream app1 {
server app1:8000;
}
server {
# this server listens on port 80
listen 80 ;
server_name app1;
# the location / means that when we visit the root url (localhost:80/), we use this configuration
location / {
proxy_pass http://app1;
}
}
now for one of the subdomains i have :现在对于我拥有的子域之一:
upstream app2 {
server app2:8000;
}
server {
# this server listens on port 80
listen 80 ;
server_name app2;
# the location / means that when we visit the root url (localhost:80/), we use this configuration
location /app2 {
proxy_pass http://app2;
}
}
Here is my docker-compose.yml file:这是我的 docker-compose.yml 文件:
version: "3.8"
services:
app1:
image: app1 image
container_name: app1
networks:
- local-network
ports:
- "8000:80"
restart: unless-stopped
app2:
image: app2 image
container_name: app2
networks:
- local-network
ports:
- "7000:80"
restart: unless-stopped
app3:
image: app3 image
container_name: app3
networks:
- local-network
ports:
- "5000:80"
restart: unless-stopped
nginx:
image: nginx:latest
container_name: nginx
networks:
- local-network
command: nginx -g "daemon off;"
volumes:
- ./nginx/sites-available/app1.conf:/etc/nginx/conf.d/sites-available/app1.conf
- ./nginx/sites-available/app2.conf:/etc/nginx/conf.d/sites-available/app2.conf
- ./nginx/sites-available/app3.conf:/etc/nginx/conf.d/sites-available/app3.conf
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- "80:80"
restart: unless-stopped
depends_on:
- app1
- app2
- app3
networks:
local-network
in my nginx.conf I including the sites-enabled and manually created symlink of the mounted sites-available configuration files in the nginx container.在我的 nginx.conf 中,我在 nginx 容器中包含了已安装站点可用配置文件的启用站点和手动创建的符号链接。
On the browser "http://localhost/" works fine but "http://localhost/app1" or app2
or app3
redirects to http://localhost/ There are no errors in the docker logs在浏览器上,“http://localhost/”工作正常,但“http://localhost/app1”或app2
或app3
重定向到 http://localhost/ docker 日志中没有错误
What might I be doing wrong?我可能做错了什么?
Here is a quick example, both nginx conf file and docker-compose.yml below:这是一个简单的例子,nginx conf 文件和 docker-compose.yml 如下:
~/Projects/docker/echo-test $ tree
.
├── docker-compose.yml
└── nginx
└── app.conf
1 directory, 2 files
~/Projects/docker/echo-test $ cat nginx/app.conf
upstream app1 {
server echoer1:7777;
}
upstream app2 {
server echoer2:8888;
}
upstream app3 {
server echoer3:9999;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://app1;
}
location /app2 {
proxy_pass http://app2;
}
location /app3 {
proxy_pass http://app3;
}
}
~/Projects/docker/echo-test $ cat docker-compose.yml
version: "3.8"
services:
echoer1:
hostname: echoer1
image: mendhak/http-https-echo
environment:
- HTTP_PORT=7777
echoer2:
hostname: echoer2
image: mendhak/http-https-echo
environment:
- HTTP_PORT=8888
echoer3:
hostname: echoer3
image: mendhak/http-https-echo
environment:
- HTTP_PORT=9999
nginx:
image: nginx
command: nginx -g "daemon off;"
volumes:
- ./nginx/app.conf:/etc/nginx/conf.d/app.conf
ports:
- "80:80"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.