Nginx-有一个主代理,将请求代理到另外两个Nginx

时间:2019-11-20 23:35:49

标签: nginx

我需要有一个链接到其他两个应用程序的主页:

main.com将在“ /”处有一个索引页,并且应在main.com/a处加载应用程序“ A”以及在main.com/b处加载应用程序“ B”。

两个应用程序都有一个nginx。

主要的Nginx:

   server {
        server_name nginx_server;

        location / {
            root ./;
            index index.html index.htm;
        }

        location /a/ {
            proxy_pass https://a.com/;
        }

        location /b/ {
            proxy_pass https://b.com/;
        }

    }

“ A”和“ B”应用程序Nginx:

server {
  listen 8080;
  server_name nginx_server;

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

  location /api {
     proxy_pass http://mybackend;
  }

直接进入a.com和b.com都可以,但是尝试使用main.com/a或main.com/b无效。

问题是前端应用程序(例如Angular应用程序“ A”)试图从错误的URL加载资源。

main.com/a 加载一个index.html文件,该文件具有<link rel="stylesheet" href="assets/css/bootstrap.min.css">,依次尝试加载 main.com/assets/css/bootstrap.min。 css 而不是 main.com/ a /assets/css/bootstrap.min.css

这种设计可行吗?

1 个答案:

答案 0 :(得分:0)

好,所以我找出了问题。

A和B(角度应用程序)的index.html文件都具有以下标记:<base href="/">,该标记使每个其他“ href”都指向根URL main.com

将其更改为<base href="">解决了该问题。