我正在尝试使用 nginx 配置 redirect_maps,用于移动到文件夹的一系列 PNG 图像。例如/images/2022/stackoverflow.png
变成/images/anewfolder...
。我需要指定新路径的位置,所以我正在考虑使用 redirect_maps。
在我的配置文件中,我输入了:
map $request_uri $redirect_uri{
/images/2022/stackoverflow.png /images/newfolder/stackoverflow.png;
}
server {
[.. redacted for sensitive data ..]
location / {
if ($request_filename !~* .(gif|webp|jpe?g|png|json|ico|js|css|flv|swf|pdf|xml)$ ) {
rewrite ^([^.]*[^/])$ $1/ permanent;
}
if ($redirect_uri){
return 301 https://google.com$redirect_uri;
}
# First attempt to serve request as file, then # as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
nginx 的选项 2:
location / {
if ($redirect_uri){
return 301 https://google.com$redirect_uri;
}
rewrite ^([^.]*[^/])$ $1/ permanent;
# First attempt to serve request as file, then # as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}