Skip to content

Commit 682db40

Browse files
authored
Merge pull request #8 from flownative/7-make-cache-lifetime-for-static-files-configurable
Configurable lifetime for static files
2 parents 5c99174 + 5b3435e commit 682db40

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ for possible values. The default value is `warn`.
6969
| BEACH_NGINX_CUSTOM_METRICS_SOURCE_PATH | string | /metrics | Path where metrics are located |
7070
| BEACH_NGINX_CUSTOM_METRICS_TARGET_PORT | integer | 8082 | Port at which Nginx should listen to provide the metrics for scraping |
7171
| BEACH_NGINX_MODE | string | Flow | Either "Flow" or "Static"; this variable is going to be renamed in the future |
72+
| BEACH_STATIC_RESOURCES_LIFETIME | string | 30d | Expiration time for static resources; examples: "3600s" or "7d" or "max"
7273
| FLOW_HTTP_TRUSTED_PROXIES | string | 10.0.0.0/8 | Nginx passes FLOW_HTTP_TRUSTED_PROXIES to the virtual host using the value of this variable |
7374

7475
## Security aspects

root-files/opt/flownative/lib/nginx-legacy.sh

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ server {
116116
log_not_found off;
117117
}
118118
119-
location = /favicon.ico {
119+
location = /site.webmanifest {
120120
log_not_found off;
121121
access_log off;
122+
expires ${NGINX_STATIC_FILES_LIFETIME};
123+
}
124+
125+
location ~ ^/(android-chrome-.+|apple-touch-icon|favicon.*|mstile-.+|safari-pinned-tab).(png|svg|jpg|ico)$ {
126+
log_not_found off;
127+
access_log off;
128+
expires ${NGINX_STATIC_FILES_LIFETIME};
122129
}
123130
124131
EOM
@@ -184,7 +191,7 @@ EOM
184191
# pass persistent resource requests to GCS
185192
location ~* "^${BEACH_PERSISTENT_RESOURCES_BASE_PATH}([a-f0-9]{40})/" {
186193
resolver 8.8.8.8;
187-
expires 3600;
194+
expires ${NGINX_STATIC_FILES_LIFETIME};
188195
proxy_set_header Authorization "";
189196
add_header Via 'Beach Asset Proxy';
190197
${addHeaderStrictTransportSecurity}
@@ -193,9 +200,9 @@ EOM
193200
EOM
194201
elif [ -n "${BEACH_PERSISTENT_RESOURCES_FALLBACK_BASE_URI}" ]; then
195202
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
196-
location ~* ^/_Resources/Persistent/(.*)$ {
203+
location ~* "^${BEACH_PERSISTENT_RESOURCES_BASE_PATH}(.*)$ {
197204
access_log off;
198-
expires 3600;
205+
expires ${NGINX_STATIC_FILES_LIFETIME};
199206
add_header Via '\$hostname' always;
200207
${addHeaderStrictTransportSecurity}
201208
try_files \$uri @fallback;
@@ -209,7 +216,16 @@ EOM
209216
proxy_pass \$assetUri;
210217
}
211218
EOM
212-
219+
else
220+
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
221+
location ~* ^/_Resources/Persistent/(.*)$ {
222+
access_log off;
223+
expires ${NGINX_STATIC_FILES_LIFETIME};
224+
add_header Via '\$hostname' always;
225+
${addHeaderStrictTransportSecurity}
226+
try_files \$uri -404;
227+
}
228+
EOM
213229
fi
214230

215231
cat >>"${NGINX_CONF_PATH}/sites-enabled/site.conf" <<-EOM
@@ -221,9 +237,9 @@ EOM
221237
222238
# for all static resources
223239
location ~ ^/_Resources/Static/ {
224-
add_header Via '\$hostname' always;
240+
add_header X-Static-Resource '\$hostname' always;
225241
access_log off;
226-
expires 3600;
242+
expires ${NGINX_STATIC_FILES_LIFETIME};
227243
}
228244
}
229245
EOM

root-files/opt/flownative/lib/nginx.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export NGINX_CACHE_BACKGROUND_UPDATE="${NGINX_CACHE_BACKGROUND_UPDATE:-off}"
3838
export NGINX_CUSTOM_ERROR_PAGE_CODES="${NGINX_CUSTOM_ERROR_PAGE_CODES:-500 501 502 503}"
3939
export NGINX_CUSTOM_ERROR_PAGE_TARGET="${NGINX_CUSTOM_ERROR_PAGE_TARGET:-}"
4040
41+
export NGINX_STATIC_FILES_LIFETIME=${NGINX_STATIC_FILES_LIFETIME:-30d}
42+
4143
export PATH="${PATH}:${NGINX_BASE_PATH}/bin"
4244
EOF
4345
}

0 commit comments

Comments
 (0)