# How to force nginx to cache HTTP POST response for few seconds

**URL:** <https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855>\
**Category:** NGINX\
**Created:** [September 15, 2011, 7:44am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855 "2011-09-15T07:44:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 15, 2011, 7:44am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/1 "2011-09-15T07:44:20Z")

</div>

Hi everybody,

I’m a newbie with nginx and would like to ask your help. On potentially  
very high loaded site I would like to cache response to the HTTP POST  
for the few seconds. Works perfectly for GETs, but…

nginx version: nginx/0.8.54

Please see below my config:

proxy\_cache\_path /tmp/nginx levels=2:2:2 keys\_zone=small:1m inactive=1m  
max\_size=10m;

server {

log\_format main ‘$remote\_addr $host $remote\_user [$time\_local]  
“$request” $status $body\_bytes\_sent “$http\_referer”  
“$http\_user\_agent” “$gzip\_ratio”’;

```
access_log /var/log/nginx/test_server.access.log main buffer=32k;
error_log /var/log/nginx/test_server.error.log info;

listen 80;

root /var/www/html/nginx;
index index.html index.htm;

location / {
    try_files $uri @apache_backend;
}

```

### location ~ .php$ {

### proxy\_pass [http://127.0.0.1:88](http://127.0.0.1:88);

### }

```
location @apache_backend {
    proxy_pass http://127.0.0.1:88;

```

proxy\_cache small;  
proxy\_cache\_valid 5s;  
proxy\_cache\_key $uri;  
proxy\_cache\_use\_stale updating;  
add\_header X-Cached $upstream\_cache\_status;  
}  
}

Any help/advices will be appreciated.

Thanks  
—\>\>\> qzpmwo \<\<\<—

Posted at Nginx Forum:

> **[How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215231#msg-215231)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 7:50am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/2 "2011-09-15T07:50:29Z")

</div>

On Thu, Sep 15, 2011 at 01:43:38AM -0400, qzpmwo wrote:

> proxy\_cache\_path /tmp/nginx levels=2:2:2 keys\_zone=small:1m inactive=1m
> 
> ### location ~ .php$ {
> 
> ```
> }
> 
> ```
> 
> }
> 
> Any help/advices will be appreciated.

You should upgrade to at least 0.8.55.  
If POST request body is small enough, then you can use:

```
location @apache_backend {
    ...
    proxy_cache_methods POST;
    proxy_cache_key "$uri|$request_body";
    client_max_body_size 1k;
}

```

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 7:52am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/3 "2011-09-15T07:52:21Z")

</div>

Hi everybody,

Sorry for not reading documentation properly. proxy\_cache\_methods is a  
saver!

Thanks a lot, case closed 🙂

—\>\>\> qzpmwo \<\<\< —

Posted at Nginx Forum:

> **[Re: How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215235#msg-215235)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 11:29am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/4 "2011-09-15T11:29:24Z")

</div>

On Thu, Sep 15, 2011 at 03:09:48AM -0400, qzpmwo wrote:

> Hi Igor,
> 
> Thanks a lot for so fast answer and for so wonderful product! I will try  
> to use your trick with request\_body, though in my case it is an AMF  
> binary.

How large these binaries ?

> I updated to 1.0.5 and have weird problem now (from error log):
> 
> http: file cache: /tmp/nginx-cache 0.000M, bsize: 4096  
> signal 17 (SIGCHLD) recieved  
> cache loader process 11737 exited with code 0  
> signal 29 (SIGIO) recieved
> 
> ☹
> 
> Any advices of how to figure out why is it happening?

This is non-erroneous messages logged at “notice” level.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 9:10am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/5 "2011-09-15T09:10:13Z")

</div>

Hi Igor,

Thanks a lot for so fast answer and for so wonderful product! I will try  
to use your trick with request\_body, though in my case it is an AMF  
binary.

I updated to 1.0.5 and have weird problem now (from error log):

http: file cache: /tmp/nginx-cache 0.000M, bsize: 4096  
signal 17 (SIGCHLD) recieved  
cache loader process 11737 exited with code 0  
signal 29 (SIGIO) recieved

☹

Any advices of how to figure out why is it happening?

Thanks again  
—\>\>\> qzpmwo \<\<\<—

Posted at Nginx Forum:

> **[Re: How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215236#msg-215236)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 11:33am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/6 "2011-09-15T11:33:45Z")

</div>

Hi Igor,

Payload size is small around 120 bytes according to my data. Response is  
empty. May be that is why? Tomorrow I will try to recompile nginx with  
debug enabled and may be get more data.

Thanks a lot for support.  
—\>\>\> qzpmwo \<\<\<—

Posted at Nginx Forum:

> **[Re: How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215244#msg-215244)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 16, 2011, 1:22am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/7 "2011-09-16T01:22:20Z")

</div>

Hi Igor,

Thanks a lot for your help. Problem was with the request body. Now  
everything is working as expected.

Sincerely  
—\>\>\> qzpmwo \<\<\<—

Posted at Nginx Forum:

> **[Re: How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215274#msg-215274)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...

---

<div class="post-metadata">

**Author:** ![qzpmwo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/ecb155/32.png) [@qzpmwo](https://www.ruby-forum.com/u/qzpmwo)\
**Post date:** [September 15, 2011, 11:44am UTC](https://www.ruby-forum.com/t/how-to-force-nginx-to-cache-http-post-response-for-few-seconds/211855/8 "2011-09-15T11:44:53Z")

</div>

Actually I was wrong. Response “Content-Length” header says 66 bytes, so  
response is not empty. If I disable caching of that request, application  
works. If it is enabled it stops after first cache HIT. As I mentioned  
in previous mail I will try to get more data (tcpdump and nginx with  
debug) tomorrow.

Thanks  
—\>\>\> qzpmwo \<\<\<—

Posted at Nginx Forum:

> **[Re: How to force nginx to cache HTTP POST response for few seconds](https://forum.nginx.org/read.php?2,215231,215246#msg-215246)**
>
> Hi everybody, I'm a newbie with nginx and would like to ask your help. On potentially very high loaded site I would like to cache response to the HTTP POST for the few seconds. Works perfectly for GETs, but... nginx version: nginx/0.8.54 Please see...
