Cache POST requests

Daniël Mostertman daniel at mostertman.org
Tue Sep 25 18:52:05 UTC 2018


Hi,

On 2018-09-25 20:32, Lahiru Prasad wrote:
>     What is the best way to cache POST requests in Nginx. I'm familiar
> with using redis module to cache GET requests. But is it possible to
> use the same for POST ?
It's possible to cache POST-requests, but it's generally not something
you want to do. POST-data is in most cases more sensitive than GET-data,
for, for instance, login-details. Use it with care.
It needs to be specifically enabled.

> Search showed that Nginx POST caching possible via disk cache. I'm
> thinking whether it would be a good idea to use a RAM disk for this.
This is certainly possible. I'm doing precisely this for my
DNS-over-HTTPS setup. It required me, however, to be a bit inventful to
get a populated $request_body to use as part of the cache key, but this
was the result (open to suggestions as to avoid "mirror" here, but
haven't really found any other proper solution):

http {
    proxy_cache_path        /dev/shm/dns
                            levels=1:2
                            keys_zone=dns:5m
                            max_size=20m
                            inactive=1d
                            use_temp_path=off; # leave this to off for
performance reasons!

    server {
        ...
                # DNS over HTTPS
                location = /dns-query {
            expires off;
                        mirror =; # Don't touch, this is necessary to
populate $request_body!

                        ## Begin - FastCGI caching.
                        proxy_cache             dns;
                        proxy_cache_methods     GET HEAD POST;
                        proxy_cache_key        
"$scheme$request_method$host$request_uri$request_body";
                        proxy_cache_valid       200 1m; # this is the
only valid response of the proxy.

                        proxy_ignore_headers    "Cache-Control"
                                                "Expires"
                                                "Set-Cookie";

                        proxy_cache_use_stale   error
                                                timeout
                                                updating
                                                http_429
                                                http_500
                                                http_503;

                        proxy_cache_background_update   on;
                        ## End - FastCGI caching

                        proxy_pass http://[::1]:8553;
                }

    }
}

Kind regards,

Daniël Mostertman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3994 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20180925/bea422f2/attachment-0001.bin>


More information about the nginx mailing list