假设您有以下json文件的请求。
https://example.com/static/xxx/fileName.json
我想知道当本地没有文件时如何从 URL 获取文件。看起来我可以使用
@external
将请求移动到另一个位置,但是如何传递文件名?
server_name example.com;
# location-A
# Ignore the $0. The $1 is the file name.
location ~ ^\/static\/[0-9a-zA-Z\-_]+\/([0-9a-zA-Z\-_]+.json) {
alias /abc/nginx/html/;
try_files $1 @external;
}
# location-B
# I want to retrieve a file from an external URL, how do I pass $1?
location @external{
# proxy_pass https://example123456789.com/path/$1;
}
使用 rewrite 获取您需要的 URL 部分。
location @external {
rewrite ^\/static\/[0-9a-zA-Z\-_]+\/([0-9a-zA-Z\-_]+.json)$ $1 break;
proxy_pass https://example123456789.com/path;
}