SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    Code:
    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:

    Code:
    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
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Judging from your syntax you seem to be using NGiNX?

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

    Code:
    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/1...aders-with-php
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much! Going to make adjustments.

    Cheers!
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. Not working
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  5. #5
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [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
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And I fixed... by putting the rewrite inside the location brackets.
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  9. #9
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  10. #10
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  11. #11
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Or nginx makes boatloads more sense if you are not used to apache

  12. #12
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  13. #13
    SitePoint Member songspk's Avatar
    Join Date
    Feb 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, it also solve my problem <snip>
    Last edited by TechnoBear; Feb 19, 2014 at 05:11. Reason: Link drop removed


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •