NGINX Rewrite to have a particular jpg go to GD Library PHP file to

I have image files created on fly with GD Library that look like:

http://www.traileraddict.com/imagecreater.php?d=summit-entertainment&f=divergent&p=2

Google isn’t liking this for pagespeed, and for jpg cache, so I’m hoping to rewrite it in a way:

http://www.traileraddict.com/imgc/summit-entertainment/divergent/2.jpg

How can I do this? More importantly, how do I require the jpg to be on it, though I’m removing for rewrite.

Something like format below?

location /imgc {
rewrite ^/?imgc/(+)/(+)/(+)$.jpg /imagecreater.php?d=$1&f=$2&p=$3
}

Don’t know how to require jpg in there.

Cheers!
Ryan

Judging from your syntax you seem to be using NGiNX?

Your code is almost correct, but allow me to make some changes:


location /imgc {
   rewrite ^/imgc/([^/]+)/([^/]+)/(.*)\\.jpg$ /imagecreater.php?d=$1&f=$2&p=$3
}

This won’t help anything with JPEG caching though, for that you’d have to send additional headers in PHP

Take a look at this thread on StackOverflow: http://stackoverflow.com/questions/1971721/how-to-use-http-cache-headers-with-php

Thanks so much! Going to make adjustments.

Cheers!
Ryan

Hmmm. Not working

[data]http://origin.traileraddict.com/ftimg/summit-entertainment/divergent/2[/data]

Works great with: rewrite ^/ftimg/([-0-9a-zA-Z]+)/([-0-9a-zA-Z]+)/([-0-9]+) /thumb.php?d=$1&f=$2&p=$3 last;

[data]http://origin.traileraddict.com/ftimg/summit-entertainment/divergent/2.jpg[/data]

Can’t get this to work with anything, be it:

rewrite ^/ftimg/([-0-9a-zA-Z]+)/([-0-9a-zA-Z]+)/([-0-9]+)\.jpg /thumb.php?d=$1&f=$2&p=$3 last;
rewrite ^/ftimg/([-0-9a-zA-Z]+)/([-0-9a-zA-Z]+)/([-0-9]+).jpg /thumb.php?d=$1&f=$2&p=$3 last;
rewrite ^/ftimg/([-0-9a-zA-Z]+)/([-0-9a-zA-Z]+)/([-0-9]+)\.jpg$ /thumb.php?d=$1&f=$2&p=$3 last;

Can’t figure it out.

Cheers!
Ryan

I figured it out, well, sort of.

I have this above my rewrites:

location ~* ^.+\.(ico|gif|jpg|jpeg|png|bmp|svg)$ {
expires 30d;
}

With that also on the nginx conf, the jpg cannot be added to rewrite. If I remove jpg from expires rule, then the rewrite works. Weird. Work around?

Cheers
Ryan

And I fixed… by putting the rewrite inside the location brackets.

Indeed, as you have found NGiNX tests a request URI against all defined location blocks and stops when it’s found one that matches. After that any remaining location won’t be processed anymore. This can be quite annoying, but it’s possible to work around, as you’ve shown :slight_smile:

Yes, annoying is putting it lightly. I literally had to prove that rewrite with .j worked, and .jp worked, but debunked on .jpg. nginx has been a huge learning curve for me, and I’ve recently changed all our rewrite rules, which has added to the confusion.

Cheers!
Ryan

Yes if you’re used to Apache it’s quite a step to get your head around NGiNX. I do feel it’s worth it though, because in the end the syntax does make a lot more sense than Apache’s, plus NGiNX is a heck of a lot more stable and faster than Apache.

Or nginx makes boatloads more sense if you are not used to apache :slight_smile:

Lol, well that is very true too. And there are a lot of things I do love about nginx in terms of the settings (and the fact that my server now stays up).

thanks, it also solve my problem <snip>