diff --git a/docker-compose.yml b/docker-compose.yml index 78b5936..2645537 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,8 @@ services: restart: ${NGINX_RESTART:-unless-stopped} expose: - 80 + volumes: + - nginx-cache:/etc/nginx/cache:rw env_file: - nginx.env networks: @@ -99,6 +101,7 @@ services: - invidious-postgres volumes: + nginx-cache: bibliogram-db: invidious-postgres-data: diff --git a/nginx/root/etc/nginx/conf.d/default.template b/nginx/root/etc/nginx/conf.d/default.template index 9dd4086..c299cae 100644 --- a/nginx/root/etc/nginx/conf.d/default.template +++ b/nginx/root/etc/nginx/conf.d/default.template @@ -59,6 +59,10 @@ proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; +# Cache for Invidious. +# Use a maximum of 512GB for the cache which will be valid for 31 days. +proxy_cache_path /etc/nginx/cache levels=1:2 use_temp_path=off keys_zone=invidious_videos_cache:10m inactive=31d max_size=16g; + server { listen ${NGINX_LISTEN_PORT}; server_name ${BG_HOSTNAME}; @@ -129,4 +133,30 @@ server { return 200 "User-agent: *\nDisallow: /\n"; } + # Cache videos + # Credits: @leonklingele + # See: https://github.com/iv-org/invidious/issues/260#issuecomment-518138476 + location ~ /(latest_version|videoplayback) { + proxy_pass http://invidious; + proxy_http_version 1.1; + proxy_set_header Connection ""; + + # Do not remove the following directives. + # Otherwise the cache will not be populated. + proxy_ignore_headers Cache-Control; + proxy_ignore_headers Expires; + proxy_ignore_headers X-Accel-Expires; + proxy_hide_header Cache-Control; + proxy_hide_header Expires; + proxy_hide_header Pragma; + + add_header X-Cache-Status $upstream_cache_status; + + proxy_cache invidious_videos_cache; + proxy_cache_key "invidious_videos-$request_uri"; + proxy_cache_methods GET; + proxy_cache_valid 200 31d; # Used for /videoplayback responses + proxy_cache_valid 302 31d; # Used for /latest_version responses + } + }