nginx将位置重写为本地服务

时间:2018-12-28 12:44:11

标签: nginx single-page-application nginx-location

我有两个使用两种不同语言的SPA应用程序。 index.html是相同的,唯一的区别在于js代码中内置的短语。我使用Nginx根据Cookie为他们提供服务

它可以从构建和捆绑的应用程序文件夹中提供,也可以从本地主机(在我的情况下为docker,但我想没关系)提供

构建并捆绑应用程序时,没有问题-一切都可以使用cookie或不使用cookie。 但是,一旦我要提供 english 版本,就无法重写为localhost端口。它会加载正确的index.html,但所有静态文件均会失败并显示404。

我该如何解决?

UPD :它与 redirect 一起工作,而不是重写和使用不同的base-href。但这不是一个很好的解决方案(

我的配置看起来像

 location ^~ /app {
            if ($http_cookie ~* "locale_en") {
                rewrite  ^(.*)/app(.*)$  $1/app-en$2 last;
            }
            alias /var/www/myproject/dist/apps/myapp;
            try_files $uri $uri/ /index.html?$query_string @app-site;
        }


        location ^~ /app-en {
            alias /var/www/myproject/dist/apps/myapp-en;
            try_files $uri $uri/ /index.html?$query_string @app-site-en;
        }


        location @app-site {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://host.docker.internal:42031;
        }

        location @app-site-en {
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://host.docker.internal:42030;
        }
两个应用程序的

index.html看起来

<!doctype html>
<html>
<head>
  <base href="/app/">
  <link rel="stylesheet" href="styles.css"></head>
<body>
  <app-root></app-root>
<script type="text/javascript" src="main.js"></script>
</body>
</html>

0 个答案:

没有答案