Home > other >  nginx cache folder is empty
nginx cache folder is empty

Time:10-22

I am trying to enable the cache on my nginx server v1.20 hosted on a CentOS 7. To do that, I have followed the instructions that appear in the official manual: https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/.

The result is a config file (nginx.conf) like the following:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events  {
    worker_connections  1024;
}

http    {
    proxy_cache_path /var/nginx/cache
             levels=1:2
             keys_zone=CACHE:10m
             max_size=20g;

    proxy_cache CACHE;
    proxy_cache_valid 200 10m;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    gzip on;
    gzip_comp_level 3;
    gzip_vary on;
    gzip_min_length 512;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml rss text/javascript application/javascript text/x-js;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6].(?!.*SV1)";

    server {
        listen 80 default_server;
            server_name _;
            return 301 https://$host$request_uri;
    }
    server {
            listen       443 ssl http2 default_server;
            listen       [::]:443 ssl http2 default_server;
            server_name  _;

        server_name_in_redirect off;

        client_body_buffer_size 16k;
        client_max_body_size 128M;
        client_header_buffer_size 1M;

        client_header_timeout 10;
        client_body_timeout 10;

        root         /var/www/html/XXX;
        index        index.php;

        ssl_certificate "/etc/pki/tls/certs/XXX.crt";
        ssl_certificate_key "/etc/pki/tls/private/XXX.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ /XXX(.*)$ {
            return 301 $1;
        } 

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        location ~ .php$ {
                    try_files $uri =404;
                    #fastcgi_pass 127.0.0.1:9000;
                    fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
                    fastcgi_index index.php;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                    fastcgi_read_timeout 3600;
                    fastcgi_buffers 8 128k;
                    fastcgi_buffer_size 256k;
            }
    }
}

I have run the main web page several times but the defined cache folder (/var/nginx/cache) is empty. I have even run the Apache bench v2.3 but the folder keeps empty.

On the other hand, if I run curl -I https:XXX I obtain the following response:

HTTP/1.1 200 OK

Server: nginx/1.20.1

Date: Wed, 20 Oct 2021 11:34:21 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

X-Powered-By: PHP/7.4.24

Set-Cookie: LS-ROKZXDGTYMXRJVMG=f0ljvjpnfs0kg6m1idhkolrfho; path=/; secure;
HttpOnly

Expires: Thu, 19 Nov 1981 08:52:00 GMT

Cache-Control: no-store, no-cache, must-revalidate

Pragma: no-cache

What am I doing wrong?

CodePudding user response:

proxy_cache take effect only when nginx configured a reverse proxy directive proxy_pass.

To cache fastcgi contents you could follow this doc, replace proxy_cache with the fastcgi_cache serie directives.

  • Related