Regex Nginx位置重定向正则表达式不工作

Regex Nginx位置重定向正则表达式不工作,regex,nginx,rewrite,Regex,Nginx,Rewrite,我想将服务器上的/images/someimage.png请求重定向到。我的nginx配置中有以下内容: location ^/images/.*(png|jpg|gif)$ { rewrite ^/images/(.*)(png|jpg|gif)$ http://anotherserver.com/images/$1$2 redirect; return 302; } 但由于某些原因,当我请求时,它没有被命中(我只得到一个404,因为该文件在myserver上不存在)。如

我想将服务器上的/images/someimage.png请求重定向到。我的nginx配置中有以下内容:

location ^/images/.*(png|jpg|gif)$ {
    rewrite ^/images/(.*)(png|jpg|gif)$ http://anotherserver.com/images/$1$2 redirect;
    return   302;
}

但由于某些原因,当我请求时,它没有被命中(我只得到一个404,因为该文件在myserver上不存在)。

如果我理解正确,您希望将一台服务器上的/image/url重定向到另一台服务器上的同一路径。您可以这样做:

location /images/ {
  rewrite ^ $scheme://anotherserver.com$request_uri redirect;
}
您不需要
返回302
重写
命令。。。重定向
已经这样做了 (如果您想要301重定向,请使用
重写…永久性

您不需要正则表达式,只要在另一台服务器上也有相同的url,内置变量就足够了

注:
你的位置没有被点击的原因是你有
location^/images/*(png | jpg | gif)$
而不是
location~^/images/*(png | jpg | gif)$
。换句话说,你忘了给正则表达式匹配发信号(区分大小写的是
~
,不区分大小写的是
~*

添加~。我只想重定向那个目录中的图像文件。