我有一个 nginx 服务器,运行一个 React 单页应用程序,简单的东西:
server{
server_name testing001.example.com;
location / {
root /var/www/testing001;
try_files $uri /index.html;
}
}
在我的
/var/www/testing001
目录中,除了应用程序文件之外,我还有一个目录:
\runtimes
\runtimes\1.js
\runtimes\2.js
\runtimes\3.js
...
\runtimes\default.js
我希望所有请求
https://testing.example.com/theRuntime.js?id={1,2,3,4}
:
location ~* ^/theRuntime\.js {
if ($arg_id != "") {
set $id $arg_id;
rewrite ^ /runtimes/$id.js last;
}
}
index.html
server{
server_name testing001.example.com;
root /var/www/testing001;
location / {
try_files $uri /index.html;
}
location = /theRuntime.js {
try_files /runtimes/$arg_id.js /runtimes/default.js =404;
}
}
将
root
移至 server
块中,以便它服务于两个 location
块。
您需要匹配单个 URL,因此使用 “完全匹配”
location
语法。
id
参数可用作 $arg_id
。
try_files
将按顺序测试 /var/www/testing001
下是否存在多个路径名。
最终的
=404
永远不会到达。参见try_files