Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Add caching to frontend Nginx configuration (#4516)

sarayourfriend994854356f2f
diff --git a/.editorconfig b/.editorconfig
index ced878448..aa3d70d14 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -8,6 +8,9 @@ end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
+[nginx.conf.template]
+indent_size = 4
+
[*.py]
indent_size = 4
diff --git a/frontend/Dockerfile.nginx b/frontend/Dockerfile.nginx
index 8e58a3791..80b107d23 100644
--- a/frontend/Dockerfile.nginx
+++ b/frontend/Dockerfile.nginx
@@ -19,7 +19,10 @@ LABEL org.opencontainers.image.source = "https://github.com/WordPress/openverse"
WORKDIR /app
-COPY nginx.conf.template /etc/nginx/templates/openverse-frontend.conf.template
+RUN mkdir -p /data/nginx/cache
+
+COPY nginx/nginx.conf.template /etc/nginx/templates/openverse-frontend.conf.template
+COPY nginx/snippets /etc/nginx/snippets
# Only environment variables with this prefix will be available in the template
ENV NGINX_ENVSUBST_FILTER="OPENVERSE_NGINX_"
diff --git a/frontend/nginx.conf.template b/frontend/nginx/nginx.conf.template
similarity index 81%
rename from frontend/nginx.conf.template
rename to frontend/nginx/nginx.conf.template
index 6a963b9e8..669937b2b 100644
--- a/frontend/nginx.conf.template
+++ b/frontend/nginx/nginx.conf.template
@@ -21,7 +21,8 @@ log_format json_combined escape=json
'"request_time":"$request_time",'
'"http_referrer":"$http_referer",'
'"http_user_agent":"$http_user_agent",'
- '"upstream_response_time":$upstream_response_time,'
+ '"upstream_response_time":"$upstream_response_time",'
+ '"upstream_cache_status":"$upstream_cache_status",'
'"http_x_forwarded_for":"$http_x_forwarded_for"'
'}';
@@ -43,6 +44,8 @@ upstream ov_service {
server $OPENVERSE_NGINX_UPSTREAM_URL;
}
+proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=nuxt-cache:25m max_size=1g inactive=7d use_temp_path=off;
+
server {
access_log /var/log/nginx/access.log json_combined;
@@ -54,14 +57,28 @@ server {
absolute_redirect off;
location / {
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- proxy_set_header Host $http_host;
- proxy_redirect off;
- proxy_pass http://ov_service;
+ include snippets/nuxt_location.conf;
error_page 500 /500.json;
}
+ location /_nuxt {
+ include snippets/cached_location.conf;
+ }
+
+ location /homepage_images {
+ include snippets/cached_location.conf;
+ }
+
+ location /error_images {
+ include snippets/cached_location.conf;
+ }
+
+ # Refers to the nested src/static/static directory
+ # See README in that directory for details
+ location /static {
+ include snippets/cached_location.conf;
+ }
+
location /version {
default_type "application/json";
return 200 '{"release": "$OPENVERSE_NGINX_GIT_REVISION", "environment": "$OPENVERSE_NGINX_ENVIRONMENT"}';
diff --git a/frontend/nginx/snippets/cached_location.conf b/frontend/nginx/snippets/cached_location.conf
new file mode 100644
index 000000000..11f4824b5
--- /dev/null
+++ b/frontend/nginx/snippets/cached_location.conf
@@ -0,0 +1,12 @@
+expires 7d;
+include snippets/nuxt_location.conf;
+add_header X-Cache-Status $upstream_cache_status;
+
+proxy_ignore_headers Cache-Control;
+proxy_http_version 1.1;
+proxy_read_timeout 1m;
+proxy_connect_timeout 1m;
+proxy_cache nuxt-cache;
+proxy_cache_valid 200 302 7d;
+proxy_cache_valid 404 1m;
+proxy_cache_lock on;
diff --git a/frontend/nginx/snippets/nuxt_location.conf b/frontend/nginx/snippets/nuxt_location.conf
new file mode 100644
index 000000000..59d3ba89f
--- /dev/null
+++ b/frontend/nginx/snippets/nuxt_location.conf
@@ -0,0 +1,8 @@
+add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
+add_header X-Frame-Options "SAMEORIGIN";
+
+proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+proxy_set_header X-Forwarded-Proto $scheme;
+proxy_set_header Host $http_host;
+proxy_redirect off;
+proxy_pass http://ov_service;