Skip to content

Commit 6f30ca6

Browse files
committed
Added rewrite rules for base64-encoded URL (see #18)
1 parent aa78a18 commit 6f30ca6

File tree

3 files changed

+98
-10
lines changed

3 files changed

+98
-10
lines changed

README.md

+66-6
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,50 @@ This setup serve images from other public origin servers, as well as from Amazon
199199
You can learn how to serve files from private storage in the [configurations section](#serving-files-from-private-storage).
200200

201201

202+
#### Base64 encoded URLs
203+
204+
The source URL can be encrypted with URL-safe Base64, prepended by the `/@base64/` prefix. So you can access the remote images like the below:
205+
206+
<small>Note: the image source is from NASA, it may be unavailable in the future.</small>
207+
208+
<p style="color:red">⚠️ Warning: Since this project has simplified the "URL signature" function of imgproxy, please be cautious with the use of Base64-encoded URLs. Malicious actors could exploit this to process images from any untrusted sources for unethical purposes.</p>
209+
210+
> Image with no preset (it is resized to max-width=1600 as default).<br/>
211+
> http://localhost/@base64/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
212+
213+
214+
> The image with preset `_w200` applied (`200` is a dynamic number).<br/>
215+
> http://localhost/@base64/_w200/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
216+
217+
218+
> The image with preset `_blurry` applied.<br/>
219+
> http://localhost/@base64/_blurry/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
220+
221+
222+
> The image with preset `_small` applied.<br/>
223+
> http://localhost/@base64/_small/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
224+
225+
226+
> The image with preset `_medium` applied.<br/>
227+
> http://localhost/@base64/_medium/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
228+
229+
230+
> The image with preset `_thumb` applied.<br/>
231+
> http://localhost/@base64/_thumb/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
232+
233+
234+
> The image with preset `_square` applied.<br/>
235+
> http://localhost/@base64/_square/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
236+
237+
238+
> The image with preset `_masked` applied.<br/>
239+
> http://localhost/@base64/_masked/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
240+
241+
242+
> Or just to download the image (with filters applied).<br/>
243+
> http://localhost/@base64/_download/aHR0cHM6Ly9tYXJzLm5hc2EuZ292L3N5c3RlbS9kb3dubG9hZGFibGVfaXRlbXMvNDAzNjglNUZQSUEyMjIyOC5qcGc=
244+
245+
202246
### Customize resizing via query string
203247

204248
#### Image width and height
@@ -329,7 +373,7 @@ If you like this setup, please [support my works](#support-my-activities) 😉.
329373

330374
Create a folder `certs/` in the same place with the `docker-compose.yml` file, then rename and put your SSL certificates `server.crt` and `server.key` to that `certs/` folder.
331375

332-
Open the file at [`nginx/nginx.conf`](nginx/nginx.conf#L113~L116) and uncomment 4 lines right after the `# SSL` line, like this:
376+
Open the file at [`nginx/nginx.conf`](nginx/nginx.conf#L120~L124) and uncomment 4 lines right after the `# SSL` line, like this:
333377

334378
```nginx
335379
# SSL
@@ -344,7 +388,7 @@ Then run the command in the [Start the server](#start-the-server) section to rec
344388

345389
### Serving files from private storage
346390

347-
Please uncomment settings in `docker-compose.yml` file to enable serving files from [Amazon S3 buckets](docker-compose.yml#L179~L184), [Google Cloud](docker-compose.yml#L186~L189) or [Azure Blob](docker-compose.yml#L191~L196), etc. Then run the command in the [Start the server](#start-the-server) section to recreate and restart the service.
391+
Please uncomment settings in `docker-compose.yml` file to enable serving files from [Amazon S3 buckets](docker-compose.yml#L177~L182), [Google Cloud](docker-compose.yml#L184~L187) or [Azure Blob](docker-compose.yml#L189~L194), etc. Then run the command in the [Start the server](#start-the-server) section to recreate and restart the service.
348392

349393
You can find more details on [imgproxy documentation](https://docs.imgproxy.net/configuration/options?#image-sources).
350394

@@ -362,22 +406,30 @@ Then run the command in the [Start the server](#start-the-server) section to res
362406

363407
### Advanced settings
364408

365-
All settings for handling image URLs are written in the [`imgproxy.conf`](imgproxy.conf#L70~L270) file using [Nginx's map directives](https://Nginx.org/en/docs/http/ngx_http_map_module.html#directives).
409+
All settings for handling image URLs are written in the [`imgproxy.conf`](imgproxy.conf#L70~L295) file using [Nginx's map directives](https://Nginx.org/en/docs/http/ngx_http_map_module.html#directives).
366410

367411
I keep all configurations in very simple variables. You can also make your own version from this base.
368412

369413

370414
> **`$use_imgproxy`**<br/>
371415
> This flag indicates that the request will be proceeded by `imgproxy`.
372416
> ```nginx
373-
> map $file_uri $use_imgproxy
417+
> map $uri_omitted_origin_preset $use_imgproxy
374418
> {
375419
> default 0;
376420
>
377421
> # Add any rules that you want to skip image processing.
378422
> #> E.g. this line excludes files under "hq-cactus" folder.
379423
> ~^/hq-cactus/ 0;
380424
>
425+
> # File URL is base64-encoded
426+
> #> Warning: Since this project has simplified the "URL signature" function of imgproxy,
427+
> #> please be cautious with the use of Base64-encoded URLs.
428+
> #> Malicious actors could exploit this to process images from any untrusted sources for unethical purposes.
429+
> #> Comment out these two lines to disable Base64-encoded URLs.
430+
> ~^/@base64/ 1;
431+
> ~[-A-Za-z0-9+/]*=*$ 1;
432+
>
381433
> # Else, process all image files with these file extensions
382434
> ~*\.(jpe?g|png|gif|tiff?|bmp)$ 1;
383435
> }
@@ -399,6 +451,14 @@ I keep all configurations in very simple variables. You can also make your own v
399451
> ~^/@myhost/ 'http://myhost.com';
400452
> ~^/@nasa/ 'https://mars.nasa.gov/system/downloadable_items';
401453
> ~^/@pinterest/ 'https://i.pinimg.com/originals';
454+
>
455+
> # Source URL can be encoded with URL-safe Base64 (please be cautious!)
456+
> #> See: https://docs.imgproxy.net/usage/processing#source-url
457+
> #> Warning: Since this project has simplified the "URL signature" function of imgproxy,
458+
> #> please be cautious with the use of Base64-encoded URLs.
459+
> #> Malicious actors could exploit this to process images from any untrusted sources for unethical purposes.
460+
> #> Comment out the below line to disable Base64-encoded URLs.
461+
> ~^/@base64/ ''; # no origin server
402462
> }
403463
> ```
404464
@@ -532,8 +592,8 @@ I keep all configurations in very simple variables. You can also make your own v
532592
> ```nginx
533593
> map $arg_skip $imgproxy_options
534594
> {
535-
> default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}${imgproxy_quality}${imgproxy_dpr}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
536-
> ~.+ '/unsafe/plain/${origin_server}${origin_uri}';
595+
> default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}${imgproxy_quality}${imgproxy_dpr}${imgproxy_type}/${origin_server}${origin_uri}${imgproxy_extension}';
596+
> ~.+ '/unsafe${imgproxy_type}/${origin_server}${origin_uri}';
537597
> }
538598
> ```
539599

imgproxy.conf

+29-3
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,22 @@ map $uri_omitted_origin $uri_omitted_origin_preset
7272

7373
## **`$use_imgproxy`**
7474
## This flag indicates that the request will be proceeded by `imgproxy`.
75-
map $file_uri $use_imgproxy
75+
map $uri_omitted_origin_preset $use_imgproxy
7676
{
7777
default 0;
7878

7979
# Add any rules that you want to skip image processing.
8080
#> E.g. this line excludes files under "hq-cactus" folder.
8181
~^/hq-cactus/ 0;
8282

83+
# File URL is base64-encoded
84+
#> Warning: Since this project has simplified the "URL signature" function of imgproxy,
85+
#> please be cautious with the use of Base64-encoded URLs.
86+
#> Malicious actors could exploit this to process images from any untrusted sources for unethical purposes.
87+
#> Comment out these two lines to disable Base64-encoded URLs.
88+
~^/@base64/ 1;
89+
~[-A-Za-z0-9+/]*=*$ 1;
90+
8391
# Else, process all image files with these file extensions
8492
~*\.(jpe?g|png|gif|tiff?|bmp)$ 1;
8593
}
@@ -106,6 +114,14 @@ map $uri $origin_server
106114
~^/@myhost/ 'http://myhost.com';
107115
~^/@nasa/ 'https://mars.nasa.gov/system/downloadable_items';
108116
~^/@pinterest/ 'https://i.pinimg.com/originals';
117+
118+
# Source URL can be encoded with URL-safe Base64 (please be cautious!)
119+
#> See: https://docs.imgproxy.net/usage/processing#source-url
120+
#> Warning: Since this project has simplified the "URL signature" function of imgproxy,
121+
#> please be cautious with the use of Base64-encoded URLs.
122+
#> Malicious actors could exploit this to process images from any untrusted sources for unethical purposes.
123+
#> Comment out the below line to disable Base64-encoded URLs.
124+
~^/@base64/ ''; # no origin server
109125
}
110126

111127
## **`$origin_uri`**
@@ -224,13 +240,23 @@ map $http_accept $imgproxy_extension
224240
# ~*avif '@avif';
225241
}
226242

243+
##! **`$imgproxy_type`**
244+
## Detect WebP or AVIF supports from the request header `Accept`.
245+
map $origin_server $imgproxy_type
246+
{
247+
default '/plain';
248+
249+
# omit the /plain/ prefix when the URL is base64-encoded
250+
'' '';
251+
}
252+
227253
## **`$imgproxy_options`**
228254
## Generate final URL for `imgproxy` following [their documentation](https://docs.imgproxy.net/usage/processing).
229255
## When URL query `?skip=1` is set, use another rule to skip `imgproxy` processing.
230256
map $arg_skip $imgproxy_options
231257
{
232-
default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}${imgproxy_quality}${imgproxy_dpr}/plain/${origin_server}${origin_uri}${imgproxy_extension}';
233-
~.+ '/unsafe/plain/${origin_server}${origin_uri}';
258+
default '/unsafe/${imgproxy_preset}${imgproxy_preset_query}${imgproxy_quality}${imgproxy_dpr}${imgproxy_type}/${origin_server}${origin_uri}${imgproxy_extension}';
259+
~.+ '/unsafe${imgproxy_type}/${origin_server}${origin_uri}';
234260
}
235261

236262
##! **`$imgproxy_rewrite`**

nginx/nginx.conf

+3-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ http {
223223
error_page 401 403 404 405 422 429 =404 @fallback;
224224
error_page 500 501 502 503 504 =503 @fallback;
225225
add_header X-Origin ${origin_server}${origin_uri} always;
226+
add_header X-Debug $debug_imgproxy_rewrite always;
226227

227228
etag off;
228229
proxy_pass ${origin_server}${origin_uri};
@@ -232,9 +233,10 @@ http {
232233
location @fallback {
233234
internal;
234235
etag off;
236+
add_header X-Fallback $fallback_uri always;
237+
add_header X-Debug $debug_imgproxy_rewrite always;
235238

236239
try_files $fallback_uri $fallback_uri/;
237-
add_header X-Fallback $fallback_uri always;
238240
}
239241

240242
# for localhost

0 commit comments

Comments
 (0)