使用nginx和docker测试本地子域

问题描述 投票:3回答:1

我正试图在我的Mac上本地设置一个简单的Web堆栈。

  • nginx用作反向代理
  • 将web应用程序#1反应到localhost上
  • 将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

这是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;
          }
      }
}

我可以成功运行docker-compose up,但只打开Web应用程序,而demo.localhost则没有。

我也改变了Mac上的主机文件内容,所以我有

127.0.0.1 localhost
127.0.0.1 demo.localhost

无济于事。

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

谢谢你的帮助

docker nginx localhost subdomain local
1个回答
1
投票

供参考:我们可以使用AWS ligthsail远程运行此命令,使用以下设置

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;
      }
  }
}

使用以下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

不幸的是,我无法在本地计算机上提供更多详细信息

© www.soinside.com 2019 - 2024. All rights reserved.