Why doesn't Nginx create a cache?

Hello,
The configuration file is as follows

server {
    listen 80;
    server_name localhost 172.16.25.201;

    # Next.js static files - Immutable caching
    location /_next/static {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        
        # Cache settings
        expires 1y;
        add_header Cache-Control "public, immutable, max-age=31536000";
    }

    # Frontend Next.js App
    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # Disable buffering for real-time chunk loading
        proxy_buffering off;
        proxy_request_buffering off;
    }

    # Backend API (PHP-FPM) with caching
    location /api {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
        include fastcgi_params;
        
        # Cache configuration
        fastcgi_cache PHP_CACHE;
        fastcgi_cache_valid 200 301 302 10m;
        fastcgi_cache_methods GET HEAD;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_cache_min_uses 1;
        add_header X-Cache-Status $upstream_cache_status;
        
        # Headers
        fastcgi_param HTTP_HOST $host;
        fastcgi_param X-Real-IP $remote_addr;
        fastcgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
        
        # CORS
        add_header 'Access-Control-Allow-Origin' 'http://localhost:3000' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Content-Type,Authorization' always;
        
        # Handle OPTIONS preflight
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }

    # phpMyAdmin
    location /phpmyadmin/ {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Script-Name /phpmyadmin;
        proxy_cookie_path / /phpmyadmin/;
        
        # Security headers
        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
    }
}

And the nginx.conf file is as follows:

user www-data;
worker_processes auto;
worker_rlimit_nofile 100000;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log warn;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 4000;
    use epoll;
    multi_accept on;
}

http {
    # FastCGI Cache Configuration
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=PHP_CACHE:1024m inactive=24h use_temp_path=off;
    fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout updating http_500 http_503;
    fastcgi_cache_background_update on;

    # Basic optimizations
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 30;
    keepalive_requests 100000;
    reset_timedout_connection on;
    client_body_timeout 10;
    send_timeout 2;
    types_hash_max_size 2048;
    server_tokens off;

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

    # SSL Settings
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    # Logging
    access_log /var/log/nginx/access.log;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    # Compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1024;
    gzip_types text/plain text/css text/xml text/javascript
               application/javascript application/x-javascript
               application/json application/xml application/wasm;

    # Cache headers for static assets
    map $sent_http_content_type $expires {
        default                    off;
        text/html                 10m;
        text/css                  max;
        application/javascript    max;
        ~image/                   max;
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

The directory permission is:

$ ls -la /var/cache/
total 44
drwxr-xr-x 11 root     root     4096 May  5 23:33 .
drwxr-xr-x 12 root     root     4096 Mar 15 04:04 ..
drwxr-xr-x  2 root     root     4096 May 25  2023 adduser
drwxr-xr-x  3 root     root     4096 Jan 24 03:39 apparmor
drwxr-xr-x  3 root     root     4096 May 10 23:00 apt
drwxr-xr-x  3 root     root     4096 Apr 30 05:07 debconf
drwxr-xr-x  2 root     root     4096 Jan 24 03:36 dictionaries-common
drwx------  2 root     root     4096 Apr 30 05:07 ldconfig
drwxr-xr-x 36 man      man      4096 May 11 03:58 man
drwxr-xr-x  3 www-data www-data 4096 May  5 23:39 nginx
drwx------  2 root     root     4096 Jan 24 03:22 private

There is only one empty directory named ab in the /var/cache/nginx directory. What is the problem?

Thank you.

2 Likes

Hi @MikeJang,
Thank you so much for your reply.
I will take a look at it.