如何托管多个Angular应用程序,带有nginx容器的子域

时间:2018-07-27 21:14:43

标签: angular docker nginx devops

所以我的情况如下-我有两个捆绑的Angular应用程序,位于Docker容器内部。我想使用相同的Nginx配置将它们托管在单独的子域(app1.example.com和app2.example.com,或现在为app1.localhost:8000和app2.localhost:8000)。

当前,我将它们捆绑在一起并将其粘贴到单独的目录中,

COPY --from=web-build-stage /app/dist/first-app/ /usr/share/nginx/html/first
COPY --from=web-build-stage /app/dist/second-app/ /usr/share/nginx/html/second

和我的nginx配置分为以下两个服务器-

server {
  listen 80;
  server_name first.localhost;
  location / {
    root /usr/share/nginx/html/first
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

server {
  listen 80;
  server_name second.localhost;
  location / {
    root /usr/share/nginx/html/second
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

我还这样更新了我的主机文件-

127.0.0.1   localhost
127.0.0.1   first.localhost
127.0.0.1   second.localhost

在我的生命中,我无法弄清楚为什么它不能正确代理或我在配置上出了错。

有见识吗?

1 个答案:

答案 0 :(得分:0)

好吧,看来我已经找到了自己的答案,这对我来说是个la脚的小姐。

工作配置:

server {
  listen 80;
  server_name first.localhost;
  root /usr/share/nginx/html/first;
  location / {
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

server {
  listen 80;
  server_name second.localhost;
  root /usr/share/nginx/html/second;
  location / {
    index index.html index.htm;
    try_files $uri $uri/ /index.html =404;
  }
}

注意:我将root从每个location块中移出(标记为here),它们需要在路径的末尾使用分号。不要错过!