user                        root root;
worker_processes            8;
worker_rlimit_nofile        8192;
worker_priority             -5;

load_module modules/ngx_http_image_filter_module.so;

error_log                  /var/log/nginx/error.resizer.local.log     error;
pid                        /var/run/nginx.resizer.local.pid;

events {
    worker_connections      4096;
    use                     epoll;
}

http {
    resolver                127.0.0.1;
    include                 mime.types;
    default_type            application/octet-stream;

    log_format  geolog '$remote_addr $upstream_addr [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for" '
                    '"$upstream_cache_status" "$geo_code" ';

    access_log              off;
    include  koi-utf;

    geo $geo_code {
        ranges; 
        default XX/0000; 
        include geo_code.conf;
    }


    sendfile                on;
    tcp_nopush              on;
    tcp_nodelay             on;

    keepalive_timeout       15;
    client_max_body_size    32M;
    
    reset_timedout_connection   on;
    client_header_buffer_size   1k;
    large_client_header_buffers 4 4k;
    output_buffers              128 256k; 
    postpone_output             1460;
    client_header_timeout       50;
    client_body_timeout         3m;
    send_timeout                3m;
    client_body_buffer_size     16k;
    proxy_buffers               8 256k;
    proxy_busy_buffers_size     256k;
    proxy_temp_file_write_size  256k;

    gzip                    on;
    gzip_disable            "MSIE [1-6]\.(?!.*SV1)";
    gzip_buffers            4 256k;
    gzip_comp_level         5;
    gzip_types              text/plain text/xml text/css text/javascript application/x-javascript application/xml application/xhtml+xml application/rss+xml image/svg+xml;
    gzip_vary               on;

    server_names_hash_max_size      1024;
    server_names_hash_bucket_size   128;

    proxy_temp_path         /var/cache/nginx.resizer.local;

        server {
            server_name                     resizer.local;
            listen                          127.0.0.1:8889;

            access_log      /var/log/nginx/resizer.local.8889.access.log geolog;
            error_log       /var/log/nginx/resizer.local.8889.error.log error;

            resolver 127.0.0.1;
            resolver_timeout 1s;


            secure_link_secret              foobar;

            error_page                      403 404 415 500 502 503 504 = @404;


            location    ~ ^/s/(?<secure>[\w-]+)/(?<dhost>[^/]+)/(?<durl>.*)$ {

                if ($secure_link = "")      { return 404; }

                proxy_pass                  http://$dhost/$durl$is_args$args;

                image_filter                size;
                    image_filter_buffer         32M;
                image_filter_jpeg_quality   85;

            }

            location ~ ^/c/[^/]+/(\d+|-)x(\d+|-)/(?<dhost>[^/]+)/(?<durl>.*)$ {

                set                         $width  $1;
                set                         $height $2;

                if ($secure_link = "") { return 404; }

                proxy_pass                  http://$dhost/$durl$is_args$args;

                image_filter                crop  $width  $height;
                image_filter_buffer         32M;
                image_filter_jpeg_quality   85;
            }

            location ~ ^/r/[^/]+/(\d+|-)x(\d+|-)/(?<dhost>[^/]+)/(?<durl>.*) {

                set                         $width  $1;
                set                         $height $2;

                if ($secure_link = "") { return 404; }

                proxy_pass                  http://$dhost/$durl$is_args$args;

                image_filter                resize  $width  $height;
                image_filter_buffer         32M;
                image_filter_jpeg_quality   85; 
            }

            location @404 { return 404; }
        }


}

