From de906bf3709ac4ff54521509e20d8f99c011f0ec Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 31 Oct 2020 13:36:53 +0100 Subject: [PATCH] Added proper caching rules to nginx config included in docker image --- Dockerfile | 2 +- config/docker/nginx.conf | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 86a5ebb3..e73f9b1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ ENV VERSION ${VERSION} RUN cd /shlink-web-client && \ npm install && npm run build -- ${VERSION} --no-dist -FROM nginx:1.17.10-alpine +FROM nginx:1.19.3-alpine LABEL maintainer="Alejandro Celaya " RUN rm -r /usr/share/nginx/html && rm /etc/nginx/conf.d/default.conf COPY config/docker/nginx.conf /etc/nginx/conf.d/default.conf diff --git a/config/docker/nginx.conf b/config/docker/nginx.conf index b401b0ee..1cb4901f 100644 --- a/config/docker/nginx.conf +++ b/config/docker/nginx.conf @@ -4,11 +4,26 @@ server { root /usr/share/nginx/html; index index.html; + # Expire rules for static content + # HTML files should never be cached. There's only one here, which is the entry point (index.html) + location ~* \.(?:manifest|appcache|html?|xml|json)$ { + expires -1; + } + # Images and other binary assets can be saved for a month + location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ { + expires 1M; + add_header Cache-Control "public"; + } + # JS and CSS files can be saved for a year, as they are always hashed. New versions will include a new hash anyway, forcing the download + location ~* \.(?:css|js)$ { + expires 1y; + add_header Cache-Control "public"; + } + # When requesting static paths with extension, try them, and return a 404 if not found location ~* .+\.(css|js|html|png|jpe?g|gif|bmp|ico|json|csv|otf|eot|svg|svgz|ttf|woff|woff2|ijmap|pdf|tif|map) { try_files $uri $uri/ =404; } - # When requesting a path without extension, try it, and return the index if not found # This allows HTML5 history paths to be handled by the client application location / {