uwsgi_cache

The uwsgi_cache directive specifies a shared cache for responses from uWSGI.

Syntaxuwsgi_cache zone;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The uwsgi_cache directive is used within the NGINX configuration to define a cache zone that stores responses from uWSGI applications, improving the performance of applications by decreasing their load times. Each time NGINX makes a request to an upstream uWSGI server and receives a response, that response can be stored in the specified cache for subsequent requests. This facilitates faster responses for repeated requests that would typically invoke processing by the uWSGI application, allowing cached responses to be served directly from NGINX instead of repeatedly invoking the application itself.

The directive requires one argument, which specifies the cache zone name previously defined using the directive uwsgi_cache_path. The behavior of the cache can also be controlled using other directives such as uwsgi_cache_key, uwsgi_cache_valid, and uwsgi_cache_bypass, among others. Properly configuring these additional directives is essential to effectively manage the entries in the cache and control cache expiry times based on the type of response or request conditions.

It is important to note that the cache features rely on response headers returned by the uWSGI application to determine whether content should be cached, and how long responses should be stored. Caching strategies can significantly enhance performance in heavily loaded web applications, especially under high traffic.

Config Example

http {
    uwsgi_cache_path /tmp/uwsgi_cache levels=1:2 keys_zone=uwsgi_cache:10m inactive=60m;
    server {
        location /api {
            include uwsgi_params;
            uwsgi_pass uWSGI_backend;
            uwsgi_cache uwsgi_cache;
            uwsgi_cache_valid 200 30m;
        }
    }
}

Ensure the cache path is accessible and has appropriate permissions set.

Monitor cache size and usage to avoid excessive disk usage.

Be cautious about caching sensitive data that shouldn't be shared.

Remember to handle cache invalidation properly to ensure updated content.

← Back to all directives