【问题标题】:How to add multiple applications to different ports using nginx如何使用 nginx 将多个应用程序添加到不同的端口
【发布时间】:2020-03-31 11:58:41
【问题描述】:

问题

我想在具有不同端口(使用 nginx)的同一域上部署一个 Web 应用程序 (React) 和一个移动应用程序 (Ionic React)。当我运行docker-compose up 时,两个应用程序应该开始在不同的端口上运行。

我目前正在成功运行我的网络应用程序 (localhost:80) 和 rest api (localhost:80/api)。

但我不知道如何使用 nginx 将移动应用程序连接到端口 81。我对此有点陌生。

源代码

我的.conf 文件目前如下所示:

server {

  listen 80;

  location / {
    proxy_pass        http://client-web-debug:3000;
    proxy_redirect    default;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Host $server_name;
  }

  location /api {
    proxy_pass        http://server-debug:3001;
    proxy_redirect    default;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Host $server_name;
  }
}

server {
  listen 81;

  location / {
    proxy_pass        http://client-mobile-debug:3002;
    proxy_redirect    default;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  X-Forwarded-Host $server_name;
  }
}

Docker 编写文件

version: '3.6'

services:
  client-web-debug:
    build:
      context: ./services/client-web # Location of the Dockerfile
      dockerfile: Dockerfile.debug # Which Dockerfile
    stdin_open: true # Enables react-script start to run (interactive mode)
    environment:
      - PORT=3000
    volumes: # Which locations on the local machine must be synced with the container
      - './services/client-web:/usr/src/app'
      - '/usr/src/app/node_modules'
    depends_on:
      - server-debug

  client-mobile-debug:
    build:
      context: ./services/client-mobile
      dockerfile: Dockerfile.debug
    stdin_open: true
    environment:
      - PORT=3002
    volumes:
      - './services/client-mobile:/usr/src/app'
      - '/usr/src/app/node_modules'
    depends_on:
      - server-debug

  server-debug:
    build:
      context: ./services/server # Location of the Dockerfile
      dockerfile: Dockerfile.debug # Which Dockerfile
    volumes: # Which locations on the local machine must be synced with the container
      - './services/server:/usr/src/app'
      - '/usr/src/app/node_modules'
    environment: # Variables that can be used in the code
      - NODE_ENV=debug
    depends_on:
      - database-debug

  database-debug:
    image: postgres:12.2 # Download postgres image from Docker Hub
    environment:
      POSTGRES_URI: 'postgres://postgres:12345@database-debug:5432/postgres' # Connection string
      POSTGRES_PASSWORD: 12345 # Must be specified! You know this by looking through the environment section of the docs
    ports:
      - '5432:5432' # Local port : Remote port

  nginx-debug:
    build:
      context: ./services/nginx # Location of the Dockerfile
      dockerfile: Dockerfile.debug # Which Dockerfile
    ports:
      - 80:80 # Application entry port
    restart: always # If crashes, keep restarting
    depends_on:
      - client-web-debug
      - client-mobile-debug
      - server-debug

结果

当我请求 localhostlocalhost/api 时,一切正常。

当我请求localhost:81 时,我的浏览器没有找到任何内容

【问题讨论】:

  • 你的 docker-compose 代码是什么样的?
  • @J.ScottElblein 添加了 docker 文件。是否可能需要将端口 81:81 添加到 nginx 中?
  • 成功了,感谢@J.ScottElblein 的提示
  • 乐于助人。只是提示,docker compose 和 dockerfile 是两个不同的东西;dockerfile(实际文件)具有构建 docker 映像的代码,docker compose 是配置和启动容器的文件/代码。我知道您可能已经知道这一点,但是在寻求或提供帮助时,了解这些措辞会对其他人有所帮助。 =)

标签: docker nginx deployment reverse-proxy nginx-reverse-proxy


【解决方案1】:

解决方案是在 ports 部分中将要访问的端口添加到 docker compose 文件中:

  nginx-debug:
    build:
      context: ./services/nginx # Location of the Dockerfile
      dockerfile: Dockerfile.debug # Which Dockerfile
    ports:
      - 80:80 
      - 81:81 # ADD IT HERE
    restart: always 
    depends_on:
      - client-web-debug
      - client-mobile-debug
      - server-debug

【讨论】:

    猜你喜欢
    • 2016-03-24
    • 1970-01-01
    • 2022-01-07
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多