用于侦听不同端口的应用程序的Nginx反向代理

7uhlpewt  于 2022-10-06  发布在  Nginx
关注(0)|答案(0)|浏览(88)

我有三个容器--一个Nginx反向代理和两个网络应用程序。这三个都装在码头集装箱里。这似乎应该是最简单的用例,但我不能让它工作。

我已经将所有三个容器放在一个用户定义的网络上,并Map了服务器正在侦听的端口(代理8080,App1 54401,App2 54402)。每台服务器都显示一个标识自己的虚拟页面,我可以直接点击并查看所有这三个页面(位于http://localhost:8080http://localhost:54401http://localhost:54402)。

我希望能够在http://localhost:8080/app1http://localhost:8080/app2上查看app1和app2页面。但是,如果我尝试访问http://localhost:8080/app1,我会被重定向到http://localhost/app1,并显示404错误。App2也是如此。

Nginx日志中没有错误。

我已经阅读了几个教程,并相当彻底地阅读了文档。我遗漏了什么?

代理的My default.conf:

upstream app1 {
    server app1:54401;
}

upstream app2 {
    server app2:54402;
}

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /app1/ {
        proxy_pass http://app1/;
    }

    location /app2/ {
        proxy_pass http://app2/;
    }
}

对于我的第一个应用程序:

server {
    listen       54401;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

对于我的第二个应用程序:

server {
    listen       54402;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
}

我的撰写文件(为了完整):

services:
  app1:
    image: docker.io/nginx:stable-alpine
    container_name: app1
    restart: unless-stopped
    volumes:
      - ./app1/html:/usr/share/nginx/html
      - ./app1/config:/etc/nginx/conf.d
    ports:
      - "54401:54401"
    networks:
      - test_network

  app2:
    image: docker.io/nginx:stable-alpine
    container_name: app2
    volumes:
      - ./app2/html:/usr/share/nginx/html
      - ./app2/config:/etc/nginx/conf.d
    ports:
      - "54402:54402"
    networks:
      - test_network

  proxy:
    image: docker.io/nginx:stable-alpine
    container_name: proxy
    volumes:
      - ./proxy/html:/usr/share/nginx/html
      - ./proxy/config:/etc/nginx/conf.d
      - ./proxy/logs:/var/log/nginx
    ports:
      - "8080:80"
    networks:
      - test_network

networks:
  test_network: {}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题