简体   繁体   English

反向代理多个 angular 应用程序使用 Nginx

[英]Reverse proxy multiple angular applications using Nginx

I have two different angular applications (lets say alpha and beta) which runs in two docker nginx based containers where I need to route to the angular app based on the subdomain. I have two different angular applications (lets say alpha and beta) which runs in two docker nginx based containers where I need to route to the angular app based on the subdomain. There I have used another nginx instance in docker which used as a reverse proxy.There i want to use SSL too.我在 docker 中使用了另一个 nginx 实例,它用作反向代理。我也想使用 SSL。 The initial routing works fine.初始路由工作正常。 But when I refresh the browser it gives 404. I tried try_files $uri $uri/ /index.html;但是当我刷新浏览器时它给出了 404。我试过try_files $uri $uri/ /index.html; but it didn't work.但它没有用。 Can someone plz help me here.有人可以在这里帮助我吗?

Basically there are 3 containers running.基本上有3个容器在运行。 Two for angualr apps and one for proxy.两个用于角度应用程序,一个用于代理。 All runs in a single docker host.全部在单个 docker 主机中运行。

The mentioned angular apps runs in nginx containers and they use below nginx configuration.提到的 angular 应用程序在 nginx 容器中运行,它们在 nginx 配置下使用。

Alpha version uses: Alpha 版本使用:

server {
    listen  5100;
    server_name  localhost;

    location / {
        root    /usr/share/nginx/html;
    }
}

Beta version uses: Beta版使用:

server {
    listen  5200;
    server_name  localhost;

    location / {
        root    /usr/share/nginx/html;
    }
}

They both running in two different ports other than port 80;它们都在端口 80 以外的两个不同端口中运行;

Below is the nginx reverse proxy configuration that I wrote.下面是我写的nginx反向代理配置。

upstream valpha {
    server alpha:5100;
}

upstream vbeta {
    server beta:5200;
}

server {
    listen       80;
    server_name  *.mydomain.com;
    return       301 https://$host$request_uri;
}

server {
    listen  443 ssl;
    server_name  subdomainalpha.mydomain.com;

    ssl_certificate /etc/ssl/mydomain.com.crt;
    ssl_certificate_key /etc/ssl/mydomain.com.key;

    location / {
        proxy_pass http://valpha;
    }
}

server {
    listen  443 ssl;
    server_name  subdomainbeta.mydomain.com;

    ssl_certificate /etc/ssl/mydomain.com.crt;
    ssl_certificate_key /etc/ssl/mydomain.com.key;

    location / {
        proxy_pass http://vbeta;
    }
}

Thanks in advance.提前致谢。

You are using the same root for both of them.您对它们都使用相同的root You should be doing something like below你应该做类似下面的事情

location / {
  alias /var/www/html/app_one/dist/;
  try_files $uri$args $uri$args/ /app_one/index.html;
}

And the other server should use some different folder like alias /var/www/html/app_two/dist/ ;另一台服务器应该使用一些不同的文件夹,例如alias /var/www/html/app_two/dist/

I achieved my what I wanted to do by overriding nginx configuration of the angular docker image (not the reverse proxy).我通过覆盖 angular docker 映像(不是反向代理)的 nginx 配置实现了我想做的事情。

file - Dockerfile文件-Dockerfile

FROM nginx:alpine
COPY /www /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d

file - default.conf文件 - default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

I achieved it by running each angular container with default port 80 and used the container name inside as proxy_pass.我通过使用默认端口 80 运行每个 angular 容器并使用里面的容器名称作为 proxy_pass 来实现它。 Since all containers in the same bridge network I could access them by name.由于同一桥接网络中的所有容器我都可以通过名称访问它们。 With above configuration I cloud achieve the reloading issue.通过上述配置,我实现了重新加载问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM