Image Resize using Nginx [Can be used with nginx CDN]

You can use nginx for image resizing using Image Filter Module. This can also be used along with nginx CDN as shared in a previous post create-a-custom-cdn-server-using-nginx-ubuntu.

Installing the Image‑Filter Module

sudo apt-get install nginx-extras

This works for the latest Debian and ubuntu versions, for older versions you can try

sudo apt-get install nginx-module-image-filter

You have to manually load the configuration in case of older versions by adding the following line nginx.conf in the main section. It is not required for newer versions.

load_module modules/ngx_http_image_filter_module.so;

Creating the configuration file for using the Module
Create a file inside the nginx sites-available file for your cdn domain name, I have used cdn.yourdomain.com.conf, Make sure its ends with a .conf extension (steps given below)

sudo nano /etc/nginx/sites-available/cdn.yourdomain.com.conf

proxy_cache_path /var/www/imgcache levels=1 keys_zone=resized:1m max_size=256m;
server {
   listen       80;
   server_name     cdn.yourdomain.com;
   root            /path/to/imageDir;
	
   location ~* (.+).(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)/([0-9]+)x([0-9]+)$ {
    
    # Again replace with your sub/domain.
    proxy_pass        http://127.0.0.1:9001;
    proxy_cache       resized;
    proxy_cache_valid 180m;

    #image_filter                resize 300 300;
    #image_filter_jpeg_quality   90;
    #image_filter_buffer         20M;
    #image_filter_interlace      on;
  }
}


limit_req_zone "1" zone=2persec:32k rate=2r/s;

server {
    listen 9001;
    allow 127.0.0.1;
    deny all;
    limit_req zone=2persec burst=10;

    location ~* (.+).(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|htm|html|js|css|mp3|swf|ico|flv)/([0-9]+)x([0-9]+) {
        alias /path/to/imageDir/$1.$2;
        image_filter_buffer 20M;
        image_filter_jpeg_quality 90;
        image_filter resize $3 $4;
    }
}

Here we have created two server one an image server and another the default server which listens on  port no 80. we have written an exception when ever an image url is encountered it send the url to the image filter server running on port 9001.  So now you can fetch any image url by sending an Url along with the image width and height. The image quality is also compressed to 90 to further optimize the image. To use the Image Resizer. This also store the image in the cache so that second time nginx don’t have to resize. make sure you create the cache directory.

sudo mkdir /var/www/imgcache

Ex url: http://cdn.yourdomain.com/image.jpg/300X300

21 thoughts on “Image Resize using Nginx [Can be used with nginx CDN]

  1. I need to see more of this from you! Of course I am going to link back to this |post straight away. TY!

  2. I have read a few just right stuff here. Definitely value bookmarking for revisiting.
    I wonder how much attempt you place to make such a magnificent informative site.

  3. Thanks for a marvelous posting! I quite enjoyed reading it, you are a
    great author. I will remember to bookmark your blog and may come back in the future.
    I want to encourage you to definitely continue your
    great work, have a nice evening!

  4. Quality posts is the key to invite the people to pay a visit the
    web site, that’s what this website is providing.

  5. Thank you for the good writeup. It in reality
    used to be a entertainment account it. Glance advanced to far added agreeable from
    you! However, how can we keep up a correspondence?

  6. My programmer is trying to convince me to move to .net from PHP.

    I have always disliked the idea because of the costs.
    But he’s tryiong none the less. I’ve been using WordPress on a number of websites for about a year and am anxious
    about switching to another platform. I have heard good things
    about blogengine.net. Is there a way I can import all my wordpress content into it?
    Any kind of help would be greatly appreciated!

  7. of course like your website but you need to check the
    spelling on several of your posts. A number of them are rife
    with spelling problems and I in finding it very bothersome to tell the truth then again I’ll certainly come back
    again.

    1. Hello, Thanks for your interest and suggestion in the website. I will definitely try to fix the same ASAP and let you know.

  8. We stumbled over here coming from a different web page and thought I might as well check things out.

    I like what I see so i am just following you. Look forward to looking into your web page for a
    second time.

  9. It’s awesome to pay a quick visit this web page and reading the views of
    all colleagues about this post, while I am also keen of getting experience.

  10. Hello there, just became alert to your blog through Google, and found that it’s
    really informative. I’m going to watch out for brussels.
    I will appreciate if you continue this in future.
    Many people will be benefited from your writing.
    Cheers!

  11. We absolutely love your blog and find almost all of your post’s to be
    exactly what I’m looking for. Does one offer guest writers to write
    content for yourself? I wouldn’t mind producing a post
    or elaborating on many of the subjects you write with regards to here.

    Again, awesome blog!

  12. It is actually a great and helpful piece of information. I am happy that you just
    shared this useful info with us. Please stay us up to date like this.
    Thank you for sharing.

  13. Hi there, yes this article is in fact pleasant and
    I have learned lot of things from it concerning blogging.
    thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

RSS
Follow by Email
Facebook
Twitter