Score:0

Nginx cache does not set the "X-Proxy-Cache" header

mv flag

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!

Gill-Bates avatar
ps flag
Which version of nginx is running?
Score:0
ps flag

First, did you check the nginx-Configuration Tool? https://www.digitalocean.com/community/tools/nginx?global.app.lang=de

It is very handy for complex configuration scenarios.

Secondly, disable your weak chipers

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

by replacing

ssl_protocols          TLSv1.2 TLSv1.3;
ssl_ciphers            ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;

About your caching issue (Docs found here):

Did you follow the Docs? My Basic example works out of the box:

nginx -v
nginx version: nginx/1.18.0

The Header is set:

wget -S -O - http://example.com

HTTP request sent, awaiting response... 
  HTTP/1.1 200 OK
  Server: nginx/1.18.0
  Date: Wed, 01 Mar 2023 14:26:13 GMT
  Content-Type: text/html; charset=utf-8
  Content-Length: 865
  Connection: keep-alive
  X-Proxy-Cache: HIT
  Accept-Ranges: bytes

/etc/nginx/conf.d/test.conf

server {
        server_name example.com;
        listen 80;
        listen [::]:80;

        location / {
        # Reverse Proxy
            proxy_pass http://127.0.0.1:3000;
            index index.html index.htm index.php;
            limit_except HEAD GET POST {deny all;}
            proxy_ignore_headers "Set-Cookie";
            proxy_hide_header "Set-Cookie";
            proxy_cache STATIC;
            add_header X-Proxy-Cache $upstream_cache_status;
            proxy_cache_valid  200 302  60m;
            proxy_cache_valid  404      1m;
       }
}

/etc/nginx/nginx.conf

http {

        ##
        # Basic Settings
        ##


        proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m;

The Cache folder:

$ /data/nginx/cache# ls
4  6  8  a  b
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.