使用 nginx 和 docker 测试本地子域

Dav*_*nes 6 subdomain localhost nginx local docker

我正在尝试在我的 Mac 上本地设置一个简单的网络堆栈。

  • nginx 作为反向代理
  • 在本地主机上响应 Web 应用程序 #1
  • 响应 web 应用程序 #2 以在 demo.localhost 上提供服务

我正在使用 docker-compose 一次旋转所有服务,这是文件:

version: "3"

services:
    nginx:
      container_name: nginx
      build: ./nginx/
      ports:
        - "80:80"
      networks:
        - backbone
    landingpage:
      container_name: landingpage
      build: ./landingpage/
      networks:
        - backbone
      expose:
        - 3000
    frontend:
      container_name: frontend
      build: ./frontend/
      networks:
        - backbone
      expose:
        - 3001

networks:
  backbone:
    driver: bridge
Run Code Online (Sandbox Code Playgroud)

这是 nginx 配置文件(使用 Dockerfile 中的 COPY 命令复制到容器中):

worker_processes  1;

events {
    worker_connections  1024;
}

http {
      include /etc/nginx/mime.types;
      gzip  on;
      gzip_http_version 1.1;
      gzip_comp_level 2;
      gzip_types text/plain text/css
      application/x-javascript text/xml
      application/xml application/xml+rss
      text/javascript;

      upstream landingpage {
          server landingpage:3000;
      }

      upstream frontend {
          server frontend:3001;
      }

      server {
          listen 80;
          server_name localhost;
          location / {
              proxy_pass http://landingpage;
          }
      }

      server {
          listen 80;
          server_name demo.localhost;
          location / {
              proxy_pass http://frontend;
          }
      }
}
Run Code Online (Sandbox Code Playgroud)

我可以成功运行 docker-compose up,但只能打开 web 应用程序,而 demo.localhost 不能。

我还更改了 Mac 上的主机文件内容,因此我有

127.0.0.1 localhost
127.0.0.1 demo.localhost
Run Code Online (Sandbox Code Playgroud)

无济于事。

恐怕我错过了一些东西,因为我不是网络开发专家,也不是 docker 或 nginx!

Dav*_*nes 3

仅供参考:我们能够使用 AWS lightsail 远程运行此程序,使用以下设置

worker_processes  1;

events {
    worker_connections  1024;
}

http {
  include /etc/nginx/mime.types;
  gzip  on;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types text/plain text/css
  application/x-javascript text/xml
  application/xml application/xml+rss
  text/javascript;

  upstream landingpage {
      server landingpage:5000;
  }

  upstream frontend {
      server frontend:5000;
  }

  server {
      listen 80;
      if ($http_x_forwarded_proto != 'https') {
        return 301 https://$host$request_uri;
      }
      server_name domain.com www.domain.com;
      location / {
          proxy_pass http://landingpage;
      }
  }

  server {
      listen 80;
      if ($http_x_forwarded_proto != 'https') {
        return 301 https://$host$request_uri;
      }

      server_name demo.domain.com www.demo.domain.com;
      location / {
          add_header  X-Robots-Tag "noindex, nofollow, nosnippet, noarchive, notranslate, noimageindex";
          proxy_pass http://frontend;
      }
  }
}
Run Code Online (Sandbox Code Playgroud)

对于两个 React 应用程序使用以下 dockerfile(基本上为两个服务公开端口 5000)

FROM node:latest 

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/
RUN npm install --verbose

COPY . /usr/src/app

RUN npm run build --production
RUN npm install -g serve

EXPOSE 5000
CMD serve -s build
Run Code Online (Sandbox Code Playgroud)

不幸的是,我无法提供有关在本地计算机上执行此操作的更多详细信息