首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带子域的反向代理nginx码头容器

带子域的反向代理nginx码头容器
EN

Stack Overflow用户
提问于 2021-11-09 02:03:40
回答 1查看 524关注 0票数 0

我有一个带有多个服务连接到同一个网络的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应用程序创建了一个服务器块,如下所示

代码语言:javascript
复制
 >> 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;
        }
       
        
}

对于其中一个子域,我有:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
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”或app2app3重定向到http://localhost/时,停靠程序日志中没有错误。

我可能做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-09 04:48:01

下面是一个快速示例,nginx conf文件和docker-come.yml:

代码语言:javascript
复制
~/Projects/docker/echo-test $ tree

.
├── docker-compose.yml
└── nginx
    └── app.conf

1 directory, 2 files
代码语言:javascript
复制
~/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;
        }
}
代码语言:javascript
复制
~/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"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69891985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档