How to Configure Nginx to Ignore Pragma: no-cache Header for Caching Proxies


5 views

When implementing Nginx as a caching proxy, many developers encounter an unexpected behavior where clients sending Pragma: no-cache headers bypass the cache completely. This becomes particularly problematic when you can't modify client applications (like legacy IoT devices or mobile apps with frozen versions).

First, verify your current setup with curl:

curl -I -H "Pragma: no-cache" http://yourdomain.com/resource
curl -I http://yourdomain.com/resource

Compare the X-Cache headers (if configured) or response times to confirm caching is being bypassed.

Add this to your nginx.conf or virtual host configuration:

proxy_ignore_headers Pragma;
proxy_cache_bypass $http_pragma;
proxy_no_cache $http_pragma;

Here's a full caching proxy configuration that ignores Pragma headers:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;

server {
    listen 80;
    server_name cache.proxy.example.com;
    
    location / {
        proxy_pass http://backend;
        proxy_cache my_cache;
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        
        # Critical directives to ignore Pragma
        proxy_ignore_headers Pragma;
        proxy_cache_bypass $http_pragma;
        proxy_no_cache $http_pragma;
        
        # Optional but recommended
        add_header X-Cache-Status $upstream_cache_status;
    }
}

After implementing, test with:

curl -v -H "Pragma: no-cache" http://cache.proxy.example.com/test

Check for these indicators:

  • The response should include X-Cache-Status: HIT on subsequent requests
  • No Cache-Control: no-cache in the response unless explicitly set by backend
  • Consistent response times indicating cache is being used

For maximum backward compatibility, you might also want to handle Cache-Control headers:

map $http_pragma $cache_control {
    default $http_cache_control;
    "no-cache" "";
}

server {
    # ... existing config ...
    proxy_cache_bypass $cache_control;
    proxy_no_cache $cache_control;
}

Remember that ignoring client cache directives affects:

  • Browser caching behavior
  • CDN interactions
  • Potential stale content delivery

Always implement proper cache invalidation strategies when overriding client directives.


When implementing Nginx as a caching proxy, you might encounter situations where clients send Pragma: no-cache headers, effectively bypassing your carefully configured cache. This is particularly frustrating when you can't modify client applications (like IoT devices or legacy systems).

The Pragma: no-cache header is an HTTP/1.0 relic that modern browsers still send for backward compatibility. While Nginx respects this header by default, we can override this behavior with some configuration magic.

Add these directives to your Nginx configuration:

proxy_cache_bypass $http_pragma;
proxy_no_cache $http_pragma;

To completely ignore Pragma headers:

proxy_ignore_headers Pragma;

Here's a full working example for a reverse proxy scenario:

http {
    proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m inactive=60m;
    
    server {
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            
            # Ignore Pragma headers
            proxy_ignore_headers Pragma;
            proxy_cache_bypass $http_pragma;
            proxy_no_cache $http_pragma;
        }
    }
}

After applying changes, test with:

curl -I -H "Pragma: no-cache" http://your-nginx-server/resource

Verify the response includes caching headers like X-Cache-Status: HIT when using the appropriate module.

  • This approach works best when you control both server and client environments
  • For public-facing services, carefully evaluate cache invalidation needs
  • Combine with Cache-Control headers for comprehensive cache management

If you need more control, you can modify incoming headers:

location / {
    # Remove Pragma header completely
    proxy_set_header Pragma "";
    
    # Or replace with cache-friendly value
    proxy_set_header Pragma "cache";
    
    # Rest of proxy configuration...
}

Remember to reload Nginx after configuration changes: nginx -s reload