Files

108 lines
3.3 KiB
Plaintext

proxy_cache_path /tmp/imgproxy/cache/ levels=2:2 keys_zone=imgproxy-cache:128m inactive=30d;
upstream imgproxy {
server 127.0.0.1:8080;
}
upstream imgproxy-py {
server 127.0.0.1:8091;
}
server {
listen 127.0.0.1:34533;
root /tmp/imgproxy/images;
#log_subrequest on;
#rewrite_log on;
if ($request_method !~ ^(GET|HEAD)$) { return 405; }
set $is_resize "";
set $img_url "";
set $img_path "";
set $loc_prefix "imgproxy";
set $imgproxy_opts "max_bytes:2097152"; # 2MiB, used if no resize
add_header root_dir "";
proxy_hide_header imgproxy-opts;
proxy_hide_header imgproxy-url;
proxy_hide_header imgproxy-path;
# workflow:
# 1. python code will be called
# 1.1 verify the signature
# 1.2 permanently save the image file through imgproxy if not yet
# available (unless /proxy)
# 1.3 emit headers to serve the correct image file and correct content type
# 2. nginx will, based on status code, serve the local file or forward
# the request to imgproxy
location / {
proxy_pass http://imgproxy-py;
proxy_intercept_errors on;
proxy_set_header root_dir $document_root;
error_page 302 = @serve-local;
error_page 303 = @serve-imgproxy;
error_page 305 = @serve-proxy;
}
location @serve-imgproxy {
# for resized images
set $img_path $upstream_http_imgproxy_path;
set $imgproxy_opts $imgproxy_opts/$upstream_http_imgproxy_opts;
set $img_url $upstream_http_imgproxy_url;
add_header Content-Type "";
add_header x-original-url $upstream_http_x_original_url;
rewrite .* /${loc_prefix}/${imgproxy_opts}plain/local:///${img_path};
}
location @serve-imgproxy-remote {
# for non-cached images
set $imgproxy_opts $imgproxy_opts/$upstream_http_imgproxy_opts;
set $img_url $upstream_http_imgproxy_url;
add_header Content-Type "";
add_header x-original-url $upstream_http_x_original_url;
rewrite .* /${loc_prefix}/${imgproxy_opts}/${img_url};
}
location @serve-proxy {
# serve from perma-cache if available, otherwise nocache
set $loc_prefix "imgproxy-nocache";
default_type "";
add_header Content-Type $upstream_http_content_type;
add_header x-original-url $upstream_http_x_original_url;
try_files $upstream_http_location @serve-imgproxy-remote;
}
location @serve-local {
# serve locally stored image, downloaded by python
default_type "";
add_header Content-Type $upstream_http_content_type;
add_header x-original-url $upstream_http_x_original_url;
try_files $upstream_http_location =404;
}
location /imgproxy/ {
internal;
proxy_pass http://imgproxy;
proxy_temp_path /tmp/imgproxy/tmp 1;
proxy_cache imgproxy-cache;
proxy_cache_key $imgproxy_opts/$img_url;
proxy_cache_lock on;
proxy_cache_lock_age 10s;
proxy_cache_lock_timeout 10s;
proxy_cache_valid 200 1y;
proxy_cache_valid any 0s;
proxy_cache_use_stale updating timeout error;
proxy_ignore_client_abort on;
proxy_redirect off;
}
location /imgproxy-nocache/ {
internal;
proxy_pass http://imgproxy;
proxy_buffering off;
proxy_redirect off;
}
}