nginx rewrite rule to prepend something to a url

1.2k views Asked by At

I want to rewrite all my JPG file URLs using mobify CDN. For that, all I have to do is prepend the URL

https://ir0.mobify.com/jpg50/ to my existing URL. So for example, if I have the URL

http://xxx.yyy.com/wp-content/uploads/2290/07/abc.png then the user has to be redirected to

https://ir0.mobify.com/jpg50/http://xxx.yyy.com/wp-content/uploads/2290/07/abc.jpg

I wrote the following code in my nginx config. I tested the regexs at regexlib and they seem to be fine.Still do not understand what is wrong with my config. Please help.

location ~ \.jpg$ { rewrite ^http://(.*).jpg$ https://ir0.mobify.com/jpg50/$uri last; }

1

There are 1 answers

3
Dayo On

Try this ...

location ~ \.jpg$ { 
    return 301 https://ir0.mobify.com/jpg50$request_uri; 
}