La caché de Nginx no establece el encabezado "X-Proxy-Cache"

La caché de Nginx no establece el encabezado "X-Proxy-Cache"

Actualmente estoy intentando implementar el almacenamiento en caché en mi servidor web NGINX. Por ahora tengo un único archivo conf vinculado en el directorio habilitado para sitios, que se ve así:

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 no arrojará ningún error en esa configuración, pero el encabezado "X-Proxy-Cache" no se configurará, lo que indica que el caché obviamente no funciona (el directorio también permanece vacío en la máquina servidor que estoy usando) . He leído varios hilos en los que todos hacían más o menos lo mismo. Aunque he leído en uno de ellos, que el problema radica en "$upstream_cache_status", que permanece vacío, porque no estoy usando ningún upstream al que represento la solicitud, lo cual tiene mucho sentido (al principio pensé que "$upstream_cache_status" se referiría al cache_status del bloque del servidor en el que estoy escribiendo). Entonces, ¿cómo puedo abordar este problema para que el almacenamiento en caché finalmente funcione?

También aquí está mi archivo nginx.conf en caso de que sea relevante para este problema:

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/*;
}

¡Salud!

Respuesta1

Primero, ¿revisaste la herramienta de configuración nginx?https://www.digitalocean.com/community/tools/nginx?global.app.lang=de

Es muy útil para escenarios de configuración complejos.

En segundo lugar, desactive sus chipers débiles

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

por reemplazo

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;

Acerca de su problema de almacenamiento en caché (Documentos encontrados aquí):

¿Seguiste los documentos? Mi ejemplo básico funciona de inmediato:

nginx -v
nginx version: nginx/1.18.0

El encabezado está configurado:

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;

La carpeta de caché:

$ /data/nginx/cache# ls
4  6  8  a  b

información relacionada