Skip to content

Instantly share code, notes, and snippets.

@hoangclinh
Forked from tmaiaroto/image-proxy.conf
Created May 2, 2018 07:20
Show Gist options
  • Save hoangclinh/3018bf833ba95bef966f29252bbcacc5 to your computer and use it in GitHub Desktop.
Save hoangclinh/3018bf833ba95bef966f29252bbcacc5 to your computer and use it in GitHub Desktop.

Revisions

  1. @tmaiaroto tmaiaroto revised this gist Mar 12, 2014. 1 changed file with 45 additions and 9 deletions.
    54 changes: 45 additions & 9 deletions image-proxy.conf
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,39 @@
    # Feel free to change this path of course (and keys_zone value as well, but also change the usage of it below).
    proxy_cache_path /var/www/cache/resized levels=1:2 keys_zone=resizedimages:10m max_size=1G;

    # Gzip was on in another conf file of mine...You may need to uncomment the next line.
    #gzip on;
    gzip_disable msie6;
    gzip_static on;
    gzip_comp_level 4;
    gzip_proxied any;
    # Again, be careful that you aren't overwriting some other setting from another config's http {} section.
    gzip_types image/jpeg image/jpg image/png image/gif;

    server {
    # Replace with your sub/domain.
    server_name image.yoursite.com;
    root /var/www/cache/store/img;
    index index.html;

    resolver 8.8.8.8 8.8.4.4;

    # This requests the original file from itself and then resizes the image.
    location ~ /resize/(\d+)x(\d+)/(.*) {
    location ~ ^/resize/(\d+)x(\d+)/(.*) {

    # Again replace with your sub/domain.
    proxy_pass http://image.yoursite.com/$3;

    proxy_cache resizedimages;
    proxy_cache_key "$host$document_uri";
    proxy_cache_valid 200 1d;
    proxy_cache_valid any 1m;
    proxy_cache_use_stale error timeout invalid_header updating;

    image_filter resize $1 $2;
    image_filter_jpeg_quality 90;
    image_filter_buffer 10M;
    image_filter_buffer 20M;
    image_filter_interlace on;
    # Do not call this directly because the resized image is NOT cached.
    #allow 127.0.0.0/8;
    #deny all;
    }

    # Access denied.
    @@ -35,13 +54,23 @@ server {

    # This saves the resized image locally.
    location @img {
    proxy_pass http://image.yoursite.com/resize$uri;
    proxy_store /var/www/cache/store/img$uri;
    set $remote_uri $uri;
    set $protocol "http";
    if ($uri ~ "^/https?\:\/(.*)") {
    set $remote_uri $1;
    }
    if ($uri ~ "^/https.*") {
    set $protocol "https";
    }
    set $full_uri "${protocol}://${remote_uri}";

    proxy_pass $full_uri;
    proxy_store /var/www/cache/store/img/$full_uri;
    }

    # This gets the remote image and saves it locally.
    location @proxy {

    set $remote_uri $uri;
    set $protocol "http";
    if ($uri ~ "^/https?\:\/(.*)") {
    @@ -52,7 +81,14 @@ server {
    }
    set $full_uri "${protocol}://${remote_uri}";

    # i thought with merge_slashes off this would work... =(
    #if ($uri ~ "^/(.*)") {
    # set $full_uri $1;
    #}
    #set $full_uri $uri;

    proxy_pass $full_uri;
    proxy_connect_timeout 30s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_store /var/www/cache/store/img$uri;
  2. @tmaiaroto tmaiaroto revised this gist Mar 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion image-proxy.conf
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    server {
    server_name image.yoursite.com;
    root /var/www/cache/store/ns365;
    root /var/www/cache/store/img;
    index index.html;

    resolver 8.8.8.8 8.8.4.4;
  3. @tmaiaroto tmaiaroto created this gist Mar 9, 2014.
    61 changes: 61 additions & 0 deletions image-proxy.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    server {
    server_name image.yoursite.com;
    root /var/www/cache/store/ns365;
    index index.html;

    resolver 8.8.8.8 8.8.4.4;

    # This requests the original file from itself and then resizes the image.
    location ~ /resize/(\d+)x(\d+)/(.*) {
    proxy_pass http://image.yoursite.com/$3;
    image_filter resize $1 $2;
    image_filter_jpeg_quality 90;
    image_filter_buffer 10M;
    image_filter_interlace on;
    # Do not call this directly because the resized image is NOT cached.
    #allow 127.0.0.0/8;
    #deny all;
    }

    # Access denied.
    location /resize {
    return 403;
    }

    # RESIZED: http://image.yoursite.com/200x200/example.jpg
    location ~ /(\d+x\d+/.*) {
    try_files /$1 @img;
    }

    # ORIGINAL: http://image.yoursite.com/example.jpg
    location / {
    # If we don't find the file locally download it.
    error_page 404 = @proxy;
    }

    # This saves the resized image locally.
    location @img {
    proxy_pass http://image.yoursite.com/resize$uri;
    proxy_store /var/www/cache/store/img$uri;
    }

    # This gets the remote image and saves it locally.
    location @proxy {

    set $remote_uri $uri;
    set $protocol "http";
    if ($uri ~ "^/https?\:\/(.*)") {
    set $remote_uri $1;
    }
    if ($uri ~ "^/https.*") {
    set $protocol "https";
    }
    set $full_uri "${protocol}://${remote_uri}";

    proxy_pass $full_uri;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_store /var/www/cache/store/img$uri;
    }

    }