Is there a way if file not exist to server another app-*.js
file in other folder, example:
Go to URL: http://www.example.com/assets/build/app/app-48fee50a26.js
I am getting the following error message:
[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:""
I want to serve instead any other js file in the same folder /var/app/assets/build/app/
, but the name of the other file can constantly change, for example app-62a5962b0a11.js
. I was thinking about app\-(.*)\.js
.
all the examples below does not work:
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;
}
}
Your
location
directive syntax is wrong. If you want to specify a regular expression, you must use the~
or~*
modifier. See this document for details.Also, you cannot expect
nginx
to look for a random file that matches a pattern. Which means that your last attempt is closest to a working solution. For example:I have added the
=404
as the default term to avoid generating a loop in the case thatapp-62a5962b0a11.js
does not exist. But in general,app-62a5962b0a11.js
must exist for proper operation. See this document fortry_files
syntax.