从一台nginx服务器提供多个单页应用程序的多个版本?

从一台nginx服务器提供多个单页应用程序的多个版本?,nginx,single-page-application,nginx-config,Nginx,Single Page Application,Nginx Config,我需要从一台服务器提供多个单页应用程序(SPA)的多个版本 如果URL指定了服务器上不存在的版本,则我希望提供备用版本 有些版本实际上是指向其他版本的符号链接 # /var/www/apps/example-app 0.1.0 0.1.1 0.1.2 fooversion -> /var/www/apps/example-spa/0.1.2 latest -> /var/www/apps/example-spa/0.1.1 最新版本是我想用作后备版本的版本 所有SPA都有一个ind

我需要从一台服务器提供多个单页应用程序(SPA)的多个版本

如果URL指定了服务器上不存在的版本,则我希望提供备用版本

有些版本实际上是指向其他版本的符号链接

# /var/www/apps/example-app
0.1.0
0.1.1
0.1.2
fooversion -> /var/www/apps/example-spa/0.1.2
latest -> /var/www/apps/example-spa/0.1.1
最新版本是我想用作后备版本的版本

所有SPA都有一个
index.html
文件

我特别不想提供来自不同版本的混合文件。如果服务器上存在某个版本,则该版本的所有请求都应从该文件夹中提供

# excerpt from nginx config
...

location ~ ^/apps/(?<appname>.+?)/(?<appversion>.+?)/(?<suffix>.*)
{
    # 'latest' is the name of the folder with the fallback version
    set $effectiveversion latest;

    # NOTE: file check does not take into account `root` directive, use full path
    if (-f /var/www/apps/$appname/$appversion/index.html) {
        set $effectiveversion $appversion;
    }

    # try path, then force to SPA index
    try_files /apps/$appname/$effectiveversion/$suffix /apps/$appname/$effectiveversion/index.html;
}

# catch requests that end without a trailing slash
location ~ ^/apps/(?<appname>.+?)/(?<appversion>[^\/]+)$
{
    try_files /NONEXISTENTFILE /apps/$appname/$appversion/;
}

...
但它不适用于以下情况:

https://example.com/apps/bar-app/existentversion/nonexistentsuffix
https://example.com/apps/bar-app/existentversion/nonexistentsuffix/
目前,最后两个返回404

==> /var/log/nginx/error.log <==
2020/06/03 14:23:15 [error] 7100#7100: *1289803 open() "/var/www/apps/example-app/fooversion/somefrontendroute" failed (2: No such file or directory), client: ..., server: , request: "GET /apps/example-app/fooversion/somefrontendroute HTTP/1.1", host: "static.bar.com", referrer: ...

==> /var/log/nginx/access.log <==
[03/Jun/2020:14:23:15 +1000] ... - "GET /apps/example-app/fooversion/somefrontendroute HTTP/1.1" 404 209 - 0.000 - - - 1591158195.572 -"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" "..., ..."

==>/var/log/nginx/error.log/var/log/nginx/access.log您可能可以删除
if
块,因为
try\u files
指令实际上与
if(-f
相同

try\u files
语句的最后一个参数不是文件术语。通过在语句末尾添加
=404
,最后一个参数将成为常规文件术语,这可能是问题案例的根本原因

有关详细信息,请参阅

例如:

location ~ ^/apps/(?<appname>.+?)/(?<appversion>.+?)/(?<suffix>.*)$
{
    try_files 
        $uri
        /apps/$appname/$appversion/index.html
        /apps/$appname/latest/$suffix 
        /apps/$appname/latest/index.html
        =404
    ;
}
location~^/apps/(?。+?)/(?。+?)/(?*)$
{
试试你的文件
$uri
/apps/$appname/$appversion/index.html
/apps/$appname/latest/$suffix
/apps/$appname/latest/index.html
=404
;
}
# cover case where version has no trailing `/`
rewrite ^/apps/([^/]+?)/([^/]+)$ /apps/$1/$2/;

location ~ ^/apps/(?<app>.+?)/(?<version>.+?)/(?<rest>.*)$
{
  try_files
    /apps/$app/$version/$rest
    /apps/$app/$version/index.html
    /apps/$app/latest/$rest
    /apps/$app/latest/index.html
  ;
}
location ~ ^/apps/(?<appname>.+?)/(?<appversion>.+?)/(?<suffix>.*)$
{
    try_files 
        $uri
        /apps/$appname/$appversion/index.html
        /apps/$appname/latest/$suffix 
        /apps/$appname/latest/index.html
        =404
    ;
}