簡體   English   中英

Nginx位置重定向正則表達式不起作用

[英]Nginx location redirect regex not working

我想將請求重定向到服務器上的/images/someimage.png到http://some.other.server/images/someimage.png 我的nginx配置中包含以下內容:

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

但是由於某種原因,當我請求http://test.com/images/test.png時,它沒有被擊中(我只得到一個404,因為該文件在我的服務器上不存在)。

如果我正確理解您的要求,則要302將一台服務器上的/ image / url重定向到另一台服務器上的相同路徑。 您可以這樣做:

location /images/ {
  rewrite ^ $scheme://anotherserver.com$request_uri redirect;
}

您不需要return 302 rewrite ... redirect已經做到了(如果您想301重定向,請使用rewrite .... permanent

只要您在另一台服務器上具有相同的url,就不需要正則表達式,內置變量就足夠了

注意:您的位置未被點擊的原因是您具有location ^/images/.*(png|jpg|gif)$而不是location ~ ^/images/.*(png|jpg|gif)$ 換句話說你忘記信號正則表達式匹配(具有~對於區分大小寫或~*為不區分大小寫)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM