0

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).

0

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Adding ~ did the trick. I wanted to only redirect if it was an image file in that directory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.