-
Notifications
You must be signed in to change notification settings - Fork 20
/
nginx.conf
190 lines (153 loc) · 6.66 KB
/
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
server {
# Uncomment for verbose debugging
# error_log /var/log/nginx/error.log debug;
# Uses the Nginx image filter module to dynamically resize and store the resized image on server
# http://nginx.org/en/docs/http/ngx_http_image_filter_module.html
# Modified from an original gist:
# https://gist.github.com/phpdude/1451684
# Intention is to support most of the commands here:
# https://github.com/beetlebugorg/mod_dims/wiki/Webservice-API
# e.g. http://<dims-host>/resize/100x100/http://media.parker.beetlebug.org/archive/2009/03/17/DSC_D300_1872.web.jpg
listen 8080; ## listen for ipv4; this line is default and implied
server_name _;
server_tokens off;
log_subrequest on;
rewrite_log on;
# NOTE: this directory needs to have the permissions set to 777 so proxy_store will work correctly
set $root_path /mnt/media;
root $root_path;
# Needed to allow requested image uri protocol slashes from being merged (used when proxying for source image)
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
# Allows us to respond to proxy redirects with things like error_page
proxy_intercept_errors on;
proxy_pass_request_body off;
proxy_pass_request_headers off;
# Hide Amazon Headers
proxy_hide_header X-Amz-Id-2;
proxy_hide_header X-Amz-Request-Id;
# Need to also handle HEAD requests
proxy_method GET;
proxy_temp_path /tmp/images;
proxy_store_access user:rw group:rw all:r;
# This is the secret sauce to allow us to handle proxy redirects with image uri's
recursive_error_pages on;
# Sets the maximum size for images during a request
image_filter_buffer 10M;
image_filter_jpeg_quality 95;
location = /favicon.ico {
log_not_found off;
}
# http://nginx.org/en/docs/http/ngx_http_image_filter_module.html#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 error_page to see if it's on disk, else try to generate it
error_page 404 = @jpeg;
log_not_found off;
}
location ~ ^/rotate/(90|180|270)/(.*) {
set $command rotate;
set $arg1 $1;
set $arg2 '';
set $image_uri $2;
# Use error_page to see if it's on disk, else try to generate it
error_page 404 = @jpeg;
log_not_found off;
}
# The named locations @jpeg, @png and @gif get evaluated in respective order for a file on disk.
# The repetition is necessary because try_files / error_page only allow redirection for the final
# fallback argument. These named locations clear mimetypes for this location and make the default
# type be the type of resource on disk.
#
# This is essentially a work-around to allow for extensionless assets to be stored on disk.
# The mimetype is being embedded in the path on disk by using the $upstream_http_content_type
# from the proxied source when specifying proxy_store
location @jpeg {
internal;
types { }
default_type image/jpeg;
root $root_path/image/jpeg;
add_header Dims-cached yes;
# Attempt to get it on disk, if not found move to the next named location
error_page 404 = @png;
}
location @png {
internal;
types { }
default_type image/png;
root $root_path/image/png;
add_header Dims-cached yes;
# Attempt to get it on disk, if not found move to the next named location
error_page 404 = @gif;
}
location @gif {
internal;
types { }
default_type image/gif;
root $root_path/image/gif;
add_header Dims-cached yes;
# Finally if there is no jpeg, png or gif of the requested uri on disk then we proceed to attempt to
# get it from the image uri requested in the last part of the path of the request
error_page 404 = @process;
}
location @process {
internal;
add_header Dims-cached no;
# Generate the image if it doesn't exist
proxy_pass http://127.0.0.1:8080/internal/$command?uri=$image_uri&arg1=$arg1&arg2=$arg2;
# Store the image in proxy storage for later retrievals (using $uri as filename which is urldecoded but lacks $args)
proxy_store $document_root/$upstream_http_content_type/$uri;
}
location @process_redirect {
internal;
# Note: we are not reusing the @process named location since we don't want to save two copies on disk
# Set the Location header for a redirect to a the image uri (further proxying will reset it)
set $image_uri $upstream_http_location;
# This is intended to be the exact url that's also in the @process named location
proxy_pass http://127.0.0.1:8080/internal/$command?uri=$image_uri&arg1=$arg1&arg2=$arg2;
}
# Note: the image_filter directive does not work in the context of anything other than location (i.e. that's why the repetition that follows)
location ~ /internal/(resize) {
allow 127.0.0.0/8;
deny all;
proxy_pass $arg_uri;
# Handle redirects
error_page 301 302 307 = @process_redirect;
# We reset these variables because technically we're in a different request
set $command $1;
set $arg1 $arg_arg1;
set $arg2 $arg_arg2;
# Perform the filter
image_filter resize $arg1 $arg2;
}
location ~ /internal/(crop) {
allow 127.0.0.0/8;
deny all;
proxy_pass $arg_uri;
# Handle redirects
error_page 301 302 307 = @process_redirect;
# We reset these variables because technically we're in a different request
set $command $1;
set $arg1 $arg_arg1;
set $arg2 $arg_arg2;
# Perform the filter
image_filter crop $arg1 $arg2;
}
location ~ /internal/(rotate) {
allow 127.0.0.0/8;
deny all;
proxy_pass $arg_uri;
# Handle redirects
error_page 301 302 307 = @process_redirect;
# We reset these variables because technically we're in a different request
set $command $1;
set $arg1 $arg_arg1;
set $arg2 $arg_arg2;
image_filter rotate $arg1;
}
}