I want to redirect requests to /images/someimage.png on my server to http://some.other.server/images/someimage.png . I have the following in my nginx config:
location ^/images/.*(png|jpg|gif)$ {
rewrite ^/images/(.*)(png|jpg|gif)$ http://anotherserver.com/images/$1$2 redirect;
return 302;
}
But for some reason it isn't getting hit when I request http://test.com/images/test.png (I just get a 404 because that file doesn't exist on myserver).
If i understand you correctly you want to 302 redirect /image/ url's on one server to the same path on another server. You can do that like so:
location /images/ {
rewrite ^ $scheme://anotherserver.com$request_uri redirect;
}
you don't need the return 302
the rewrite ... redirect
already does that (if you want 301 redirects instead use rewrite .... permanent
)
you don't need regexes as long as you have the same url's on the other server either, the builtin variables will suffice
NOTE: The reason your location isn't getting hit is that you have location ^/images/.*(png|jpg|gif)$
instead of location ~ ^/images/.*(png|jpg|gif)$
. In other words you forgot to signal regex matching (with ~
for case-sensitive or ~*
for case-insensitive)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.