我有一个带有多个服务连接到同一个网络的docker-compose.yml文件。这些服务包括nginx公开端口80、运行在0.0.0.0:8000上的web应用程序(App1)、运行在0.0.0.0:7000上的第二个web应用程序(App2)、在0.0.0.0:5000上运行的另一个web应用程序(App3)等等。
当转到0.0.0.0:8000或0.0.0:5000时,我可以从浏览器访问所有应用程序。
我想从浏览器中访问第一个带有"http://localhost“的应用程序,也就是第一个作为根的web应用程序。则其他域将是子域,例如,对于app2是从以下内容访问的:"http://localhost/app2“
我已经尝试过的
我为每个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;
}
}对于其中一个子域,我有:
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;
}
}这是我的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在我的nginx.conf i中,包括启用站点和手动创建挂载站点的符号链接在内的nginx容器中可用的配置文件。
在浏览器上,"http://localhost/“工作正常,但是"http://localhost/app1”或app2或app3重定向到http://localhost/时,停靠程序日志中没有错误。
我可能做错什么了?
发布于 2021-11-09 04:48:01
下面是一个快速示例,nginx conf文件和docker-come.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"https://stackoverflow.com/questions/69891985
复制相似问题