Search code examples
nginxcaching

nginx proxy_cache unable to cache


ı trying content cache with Nginx but not working

that is my app.test.conf file

 proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g 
                 inactive=60m use_temp_path=off;
                 

server {
        listen 80;
        listen [::]:80;

        root /var/www/app.test/html;
        index index.php;
        
        server_name app.test;
        
        proxy_cache my_cache;

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

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        }

    }


Solution

  • finally I was able to solve it, reading started using fastcgi_cache instead of proxy_cache

    Of course I added some more things

    fastcgi_cache my_cache;
    fastcgi_cache_valid 200 302 60m;
    
    

    and result

    fastcgi_cache_path  /var/cache/nginx/
                     keys_zone=my_cache:60m 
                     levels=1:2 
                     inactive=3h 
                     max_size=100m;
    
    server {
            listen 80;
            listen [::]:80;
    
            root /var/www/app.test/html;
            index index.php;
            server_name app.test;
        
           error_log /var/log/nginx/$host.log;
        
           fastcgi_cache my_cache;
           fastcgi_cache_valid 200 302 60m;
    
           fastcgi_cache_valid 404 1m;
        
    
        
    
            
        location / {
       
             try_files $uri $uri/ /index.php?$query_string;
           
         }
    
    
        location ~ \.php$ {
         
                    include snippets/fastcgi-php.conf;
                    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
         }
    
        }