You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# What
A potential fix for #188
When the `Range` header is supplied:
* NGINX will perform subrequests to s3 in byte ranges of `PROXY_CACHE_SLICE_SIZE` until the requested range is satisfied
* Cache will be populated in slices of `PROXY_CACHE_SLICE_SIZE`.
* Only the requested byte range will be cached
When the `Range` header is not supplied:
* Normal behavior - files will be cached in their entirety
* For large files, `proxy_cache_lock` ensures that multiple requests for the same file are not cached multiple times. Requests received after the initial `MISS` will queue until they can be served from the cache (the initial request cache write is complete).
## Implementation Details
* This solution takes advantage of the existing [redirectToS3](https://github.com/nginxinc/nginx-s3-gateway/blob/656395c2b2cc8aaf79a78b59b4abbe5b5d04a5a3/common/etc/nginx/include/s3gateway.js#L347) function to change the target NGINX conf location based on the presence of the `Range` header
* The main configuration for the s3 proxy action has been broken out into `common/etc/nginx/templates/gateway/s3_location_common.conf.template`
* A separate cache is defined for the slice-based caching
* In the slice caching location, the [http_slice_module](http://nginx.org/en/docs/http/ngx_http_slice_module.html) is configured and other caching options overridden as necessary.
## Examples
### Normal Request
```bash
curl -o foo.txt localhost:8989/a/5mb.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5120k 100 5120k 0 0 111M 0 --:--:-- --:--:-- --:--:-- 113M
```
A single cache file is created
```bash
root@f339daeb2d44:/var/cache/nginx/s3_proxy# tree .
.
`-- 5
`-- 9e
`-- 447b5a707c18a4c0e90344925e6b39e5
```
The size of the cache file is equal to the requested file:
```bash
root@f339daeb2d44:/var/cache/nginx/s3_proxy# du -h .
5.1M ./5/9e
5.1M ./5
5.1M .
```
### Byte Range Request
In this example, I'm requesting a 5mb file, and the `PROXY_CACHE_SLICE_SIZE` option has been set to `1000k` (1000 [kilobytes](http://nginx.org/en/docs/syntax.html))
```bash
curl -o foo.txt -r 1000000-4000000 localhost:8989/a/5mb.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2929k 100 2929k 0 0 66.8M 0 --:--:-- --:--:-- --:--:-- 68.1M
```
Cache files are created in chunks:
```bash
root@f339daeb2d44:/var/cache/nginx/s3_proxy_slices# tree .
.
|-- 0
| `-- 5c
| `-- 18f94c01f7a1beed3afe0aa92baf05c0
|-- 4
| `-- 30
| `-- 9fac913edc79622fdcc2975d91e4f304
|-- b
| `-- 5b
| `-- 91bfb9ef86136be4b07cdc2eb51025bb
`-- d
`-- 82
`-- 339384e3e9840cf7f8fe4e54fdc8182d
```
The size of each cache file is roughly equal to the requested file the chunk size:
```bash
root@f339daeb2d44:/var/cache/nginx/s3_proxy_slices# du -h .
1008K ./d/82
1012K ./d
1008K ./0/5c
1012K ./0
1008K ./b/5b
1012K ./b
1008K ./4/30
1012K ./4
4.0M .
```
Copy file name to clipboardexpand all lines: docs/getting_started.md
+13-1
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,8 @@ running as a Container or as a Systemd service.
31
31
|`DIRECTORY_LISTING_PATH_PREFIX`| No ||| In `ALLOW_DIRECTORY_LIST=true` mode [adds defined prefix to links](#configuring-directory-listing)|
32
32
|`DNS_RESOLVERS`| No ||| DNS resolvers (separated by single spaces) to configure NGINX with |
33
33
|`PROXY_CACHE_MAX_SIZE`| No ||`10g`| Limits cache size |
34
-
|`PROXY_CACHE_INACTIVE`| No ||`60m`| Cached data that are not accessed during the time specified by the parameter get removed from the cache regardless of their freshness |
34
+
| `PROXY_CACHE_INACTIVE` | No | | `60m` | Cached data that are not accessed during the time specified by the parameter get removed from the cache regardless of their freshness
35
+
|`PROXY_CACHE_SLICE_SIZE`| No ||`1m`| For requests with a `Range` header included, determines the size of the chunks in which the file is fetched. Values much smaller than the requests can lead to inefficiencies due to reading and writing many files. See [below for more details](#byte-range-requests-and-caching)||
35
36
|`PROXY_CACHE_VALID_OK`| No ||`1h`| Sets caching time for response code 200 and 302 |
36
37
|`PROXY_CACHE_VALID_NOTFOUND`| No ||`1m`| Sets caching time for response code 404 |
37
38
|`PROXY_CACHE_VALID_FORBIDDEN`| No ||`30s`| Sets caching time for response code 403 |
@@ -112,6 +113,17 @@ S3 bucket in a subfolder on an ALB. For example, if you wanted to expose the
112
113
root of a bucket under the path "www.mysite.com/somepath", you would set this
113
114
variable to "/somepath".
114
115
116
+
## Byte-Range Requests and Caching
117
+
The gateway caches [byte-range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests) (requests sent with a `Range` header) requests differently than normal requests.
118
+
119
+
The gateway is configured to cache such requests in chunks of size `PROXY_CACHE_SLICE_SIZE`. If you don't provide this configuration value it will default to 1 megabyte.
120
+
121
+
This means that if you request 2.5 megabytes of a 1 gigabyte file, the gateway will cache 3 megabytes and nothing else.
122
+
123
+
Setting your slice size too small can have performance impacts since NGINX performs a subrequest for each slice. For more details see the [official reference](http://nginx.org/en/docs/http/ngx_http_slice_module.html).
124
+
125
+
You may make byte-range requests and normal requests for the same file and NGINX will automatically handle them differently. The caches for file chunks and normal file requests are separate on disk.
126
+
115
127
## Running as a Systemd Service
116
128
117
129
An [install script](/standalone_ubuntu_oss_install.sh) for the gateway shows
if [ "${expected_checksum}"!="${s3_file_checksum}" ];then
168
+
e "Checksum doesn't match expectation. Request [GET ${uri} Range: "${range_start}"-"${range_end}"] Expected [${expected_checksum}] Actual [${s3_file_checksum}]"
169
+
e "curl command: ${curl_cmd} -X "GET" -r "${range_start}"-"${range_end}""${uri}"${extra_arg} | ${checksum_cmd}"
170
+
exit${test_fail_exit_code}
171
+
fi
143
172
else
144
173
e "Method unsupported: [${method}]"
145
174
fi
@@ -175,7 +204,6 @@ if [ -n "${prefix_leading_directory_path}" ]; then
0 commit comments