O cache Nginx não define o cabeçalho “X-Proxy-Cache”

O cache Nginx não define o cabeçalho “X-Proxy-Cache”

Atualmente, estou tentando implementar o cache em meu servidor NGINX. Por enquanto, tenho um único arquivo conf vinculado em meu diretório habilitado para sites, que se parece com isto:

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
}

O NGINX não gerará nenhum erro nessa configuração, mas o cabeçalho "X-Proxy-Cache" não será definido, indicando que o cache obviamente não está funcionando (o diretório também permanece vazio na máquina servidora que estou usando) . Eu li vários tópicos, onde todos estavam fazendo praticamente a mesma coisa. Embora eu tenha lido em um deles, que o problema está no "$upstream_cache_status", que fica vazio, porque não estou usando nenhum upstream para o qual faço proxy da solicitação, o que faz totalmente sentido (a princípio pensei que o "$upstream_cache_status" se referiria ao cache_status do bloco do servidor no qual estou escrevendo). Então, como posso abordar esse problema para que o cache finalmente funcione?

Também aqui está meu arquivo nginx.conf, caso seja de alguma forma 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/*;
}

Saúde!

Responder1

Primeiro, você verificou a ferramenta nginx-Configuration?https://www.digitalocean.com/community/tools/nginx?global.app.lang=de

É muito útil para cenários de configuração complexos.

Em segundo lugar, desative seus chips fracos

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

substituindo

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;

Sobre seu problema de cache (Documentos encontrados aqui):

Você seguiu o Documentos? Meu exemplo básico funciona imediatamente:

nginx -v
nginx version: nginx/1.18.0

O cabeçalho está definido:

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;

A pasta Cache:

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

informação relacionada