Как добавить несколько приложений в разные порты, используя nginx - PullRequest
0 голосов
/ 31 марта 2020

Проблема

Я хочу развернуть веб-приложение (React) и мобильное приложение (Ioni c React) в одном домене с разными портами (используя nginx). Когда я запускаю docker-compose up, оба приложения должны запускаться на разных портах.

В настоящее время я запускаю мое веб-приложение (localhost: 80) и остальные API (localhost: 80 / api) с успехом.

Но я не могу понять, как подключить мобильное приложение к порту 81 с nginx. Я вроде как новичок в этом.

Исходный код

Мой .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

Результат

Когда я запрашиваю localhost и localhost/api, все работает.

Мой браузер не находит ничего, когда я запрашиваю localhost:81 enter image description here

1 Ответ

0 голосов
/ 01 апреля 2020

Решением было добавить порт, к которому вы хотите получить доступ, к вашему docker составному файлу в разделе ports :

  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
...