So I´m currently trying to implement caching on my NGINX webserver. For now I have a single conf file in my sites-enabled directory linked, which looks like this:
proxy_cache_path /var/cache/nginx levels=1:2 inactive=120s keys_zone=custom_cache:10m;
server {
root /var/www/html;
server_name _;
location / {
proxy_cache custom_cache;
proxy_cache_valid 60m;
add_header X-Proxy-Cache $upstream_cache_status;
try_files $uri $uri/ /index.html =404;
}
listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
listen 443 ssl http2; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.io/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.io/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.io) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name example.io www.example.io;
return 404; # managed by Certbot
}
NGINX wont throw any errors on that configuration, yet the "X-Proxy-Cache" header wont be set, indicating that the cache obviously isn´t working (the directory stays empty as well on the server machine, that I´m using). I´ve read multiple threads, where everyone was doing pretty much the same thing. Tho I´ve read in one of them, that the issue lies within the "$upstream_cache_status", which stays empty, because I´m not using any upstream that I proxy the request to, which totally makes sense (at first I thought the "$upstream_cache_status" would refer to the cache_status of the server block I´m writing in). So how can I approach this issue, so the caching would finally work?
Also here is my nginx.conf file in case it is somehow relevant for this problem:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
# gzip on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_types text/plain text/css text/xml text/javascript image/svg+xml image/x-icon application/x-javascript application/javascript application/xml;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Cheers!