当nginx.conf中没有包含`try_files`的本地文件时,如何从URL获取文件?

问题描述 投票:0回答:1

假设您有以下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;
}
nginx nginx-location
1个回答
0
投票

使用 rewrite 获取您需要的 URL 部分。

location @external {
  rewrite ^\/static\/[0-9a-zA-Z\-_]+\/([0-9a-zA-Z\-_]+.json)$ $1 break;
  proxy_pass https://example123456789.com/path;
}
© www.soinside.com 2019 - 2024. All rights reserved.