如果文件不存在,有没有办法将另一个app-*.js
文件发送到其他文件夹中,例如:
转至网址:http://www.example.com/assets/build/app/app-48fee50a26.js
我收到以下错误信息:
[error] 17788#17788: *1 open() "/var/app/assets/build/app/app-48fee50a26.js" failed (2: No such file or directory), client: , server: , request: "GET /assets/build/app/app-48fee50a26.js HTTP/1.1", host:""
我想提供同一文件夹中的任何其他 js 文件/var/app/assets/build/app/
,但其他文件的名称可能会不断变化,例如app-62a5962b0a11.js
。我在想app\-(.*)\.js
。
下面所有的例子都不起作用:
server {
listen 80 default_server;
root /var/app;
location /assets/build/app/app\-(.*)\.js {
try_files $uri $uri/ /assets/build/app/app\-(.*)\.js;
}
}
server {
listen 80 default_server;
root /var/app;
location /assets/build/app/app\-(.*)\.js {
try_files $uri $uri/ /var/app/assets/build/app/app\-(.*)\.js;
}
}
Also not working without regex to the location of the other file.
server {
listen 80 default_server;
root /var/app;
location /assets/build/app/app\-(.*)\.js {
try_files $uri $uri/ /var/app/assets/build/app/app-62a5962b0a11.js;
}
}
server {
listen 80 default_server;
root /var/app;
location /assets/build/app/app\-(.*)\.js {
try_files $uri $uri/ /assets/build/app/app-62a5962b0a11.js;
}
}
答案1
您的location
指令语法错误。如果要指定正则表达式,则必须使用~
或~*
修饰符。请参阅这个文件了解详情。
此外,您不能指望nginx
找到与模式匹配的随机文件。这意味着您最后一次尝试最接近可行的解决方案。例如:
location ~ ^/assets/build/app/app-.+\.js$ {
try_files $uri /assets/build/app/app-62a5962b0a11.js =404;
}
我添加了=404
作为默认项,以避免在不存在的情况下生成循环app-62a5962b0a11.js
。但一般来说,app-62a5962b0a11.js
必须存在才能正常运行。请参阅这个文件语法try_files
。