# Caching issues with nginx as reverse-proxy

**URL:** <https://www.ruby-forum.com/t/caching-issues-with-nginx-as-reverse-proxy/244870>\
**Category:** NGINX\
**Created:** [June 30, 2016, 12:03am UTC](https://www.ruby-forum.com/t/caching-issues-with-nginx-as-reverse-proxy/244870 "2016-06-30T00:03:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [June 30, 2016, 12:03am UTC](https://www.ruby-forum.com/t/caching-issues-with-nginx-as-reverse-proxy/244870/1 "2016-06-30T00:03:19Z")

</div>

Hi community,

currently I am serving files with a size about 1,5G (static without  
dynamic content) using a hand full of nodes and nginx in reverse proxy  
setup.  
Caching works, …but not as expected. During the requests nginx creates  
a lot of temp caching files that grow up to the size of the origin file  
delivered from backend server. It’s kinda weird that even if the  
inactive caching time out is not reached, the file is randomly  
downloaded again from backend.

I guess that I just simply got blind by the configure that affects that.

## /etc/nginx/nginx.conf

proxy\_cache\_path /var/tmp/nginx-cache/rproxy levels=1:2  
keys\_zone=rproxy:260m max\_size=5g inactive=260m use\_temp\_path=off;  
proxy\_cache\_key “$scheme$request\_method$host$request\_uri$is\_args$args”;  
proxy\_cache\_valid 200 302 45m;  
proxy\_cache\_valid 404 1m;

## /etc/nginx/sites-available/domain.tld

server {  
listen 80;# default\_server;  
listen [::]:80;# default\_server;

```
 server_name domain.tld;

 location / {
 return 301 https://$host$request_uri;
 }

```

}

```
 server {

 listen 443 ssl http2; #default_server;
 listen [::]:443 ssl http2; #default_server;

 ssl on;
 ssl_certificate /etc/letsencrypt/live/domain.tld/fullchain.pem;
 ssl_certificate_key 

```

/etc/letsencrypt/live/domain.tld/privkey.pem;

```
 server_name domain.tld;

 location / {

```

# proxy\_buffering on;

proxy\_cache\_revalidate on;  
proxy\_cache\_lock on;  
proxy\_cache rproxy;  
proxy\_cache\_use\_stale error timeout http\_500 http\_502 http\_503  
http\_504;  
proxy\_pass [https://rproxy](https://rproxy);  
}

}

#### 

Nginx 1.11.1 is used. And the frontends got only 1gb ram and 10 gb of  
disk space.

I am looking forward for any advice that could bring me some steps  
forward to the right configuration 🙂

Thanks in advance!

Best regards,  
Daniel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [June 30, 2016, 2:51pm UTC](https://www.ruby-forum.com/t/caching-issues-with-nginx-as-reverse-proxy/244870/2 "2016-06-30T14:51:50Z")

</div>

Hello!

On Thu, Jun 30, 2016 at 12:02:29AM +0200, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> currently I am serving files with a size about 1,5G (static without dynamic  
> content) using a hand full of nodes and nginx in reverse proxy setup.  
> Caching works, ..but not as expected. During the requests nginx creates a  
> lot of temp caching files that grow up to the size of the origin file  
> delivered from backend server. It’s kinda weird that even if the inactive  
> caching time out is not reached, the file is randomly downloaded again from  
> backend.

Multiple temporary files can be created when the response is not  
yet cached and there are multiple requests to the resource in  
question. Once the response is cached it will be returned from  
cache to subsequent requests, and no additional temporary files  
will appear.

To mininize parallel caching by multiple simultaneous requests  
there is the “proxy\_cache\_lock” directive. Though for 1.5G files  
default timeouts it uses may not be enough, consider tuning  
proxy\_cache\_lock\_age and proxy\_cache\_lock\_timeout:

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock_age)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_lock_timeout)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

Additionally, consider using the slice module. It was specially  
designed to improve caching of large files, see here:

> **[Module ngx\_http\_slice\_module](http://nginx.org/en/docs/http/ngx_http_slice_module.html)**

[…]

> proxy\_cache\_path /var/tmp/nginx-cache/rproxy levels=1:2  
> keys\_zone=rproxy:260m max\_size=5g inactive=260m use\_temp\_path=off;

Note well: with “max\_size” set to 5g the cache in question can  
hold about 3 1.5G files. It may not be what you want for  
effective caching.

[…]

–  
Maxim D.

> **[nginx](http://nginx.org/)**
