Skip to content

Instantly share code, notes, and snippets.

@pristavu
Forked from pderoubaix/media.conf
Last active August 29, 2015 14:17
Show Gist options
  • Save pristavu/72c98bfcc0f9f5730f29 to your computer and use it in GitHub Desktop.
Save pristavu/72c98bfcc0f9f5730f29 to your computer and use it in GitHub Desktop.
Nginx image filter + caching of results.
Supports dynamic thumbnails images sizes processing + caching results, simple to use.
Awesome!!!
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name media.resize.dev;
error_log /var/log/nginx/debug.log debug;
log_subrequest on;
rewrite_log on;
# NOTE: this directory needs to have the permissions set to 777 so proxy_store will work correctly
root /mnt/media;
# Needed to allow uri protocol slashes from being merged
merge_slashes off;
# Will proxy to external urls and gets remote images, the following will allow it to resolve properly
resolver 8.8.8.8; # Google DNS
set $quality 95;
# http://wiki.nginx.org/HttpImageFilterModule#image_filter
# commands test, rotate, size, crop, resize
location ~ ^/(resize|crop)/(\d+)x(\d+)/(.*) {
set $command $1;
set $arg1 $2;
set $arg2 $3;
set $image_uri $4;
# Use try_files to see if it's on disk, else try to generate it
error_page 404 = @process;
log_not_found off;
}
location ~ ^/rotate/(90|180|270)/(.*) {
set $command rotate;
set $arg1 $1;
set $arg2 '';
set $image_uri $2;
# Use try_files to see if it's on disk, else try to generate it
error_page 404 = @process;
log_not_found off;
}
location @process {
# Store the image in proxy storage for later retrievals (using $request_uri for a reason)
proxy_store $document_root$request_uri;
proxy_store_access user:rw group:rw all:r;
proxy_pass_request_body off;
proxy_pass_request_headers off;
proxy_temp_path /tmp/images;
# Generate the image if it doesn't exist
proxy_pass http://127.0.0.1/internal/$command?uri=$image_uri&arg1=$arg1&arg2=$arg2;
}
location /internal/resize {
proxy_pass http://$host/$arg_uri;
# Sets the maximum size for images during this request
image_filter_buffer 10M;
image_filter_jpeg_quality $quality;
image_filter resize $arg_arg1 $arg_arg2;
#allow 127.0.0.0/8;
#deny all;
}
location /internal/crop {
proxy_pass http://$host/$arg_uri;
# Sets the maximum size for images during this request
image_filter_buffer 10M;
image_filter_jpeg_quality $quality;
image_filter crop $arg_arg1 $arg_arg2;
allow 127.0.0.0/8;
deny all;
}
location /internal/rotate {
proxy_pass http://$host/$arg_uri;
# Sets the maximum size for images during this request
image_filter_buffer 10M;
image_filter_jpeg_quality $quality;
image_filter rotate $arg_arg1;
allow 127.0.0.0/8;
deny all;
}
}
location /resize {
alias /tmp/nginx/resize;
set $width 150;
set $height 100;
set $dimens "";
if ($uri ~* "^/resize_(\d+)x(\d+)/(.*)" ) {
set $width $1;
set $height $2;
set $image_path $3;
set $demins "_$1x$2";
}
if ($uri ~* "^/resize/(.*)" ) {
set $image_path $1;
}
set $image_uri image_resize/$image_path?width=$width&height=$height;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8080/$image_uri;
break;
}
proxy_store /tmp/nginx/resize$demins/$image_path;
proxy_store_access user:rw group:rw all:r;
proxy_temp_path /tmp/images;
proxy_set_header Host $host;
}
location /image_resize {
alias /path/to/media/;
image_filter resize $arg_width $arg_height;
image_filter_jpeg_quality 75;
allow 127.0.0.0/8;
deny all;
}
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /home/example/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name dev.apigility.com;
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# Only for nginx-naxsi : process denied requests
#location /RequestDenied {
# For example, return an error code
#return 418;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_intercept_errors on;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ /index.html;
# }
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment