Nginx Cache filling up please help!

Hi, I recently setup a web server with Easyengine Nginx Cache + W3TC. It is performing very well, but the problem is Nginx cache seems to fill up very fast (about in a day).

This web server hosts a magazine website, which aggreagates content from various sources, and there are currently about 25.000 posts. Here is the error message I get from /var/log/nginx/error.log:

[alert] 6553#6553: *198911 write() "/var/run/nginx-cache/e/f1/5e175b9fb58aa01c23a706e4f33aaf1e.0000128511" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /en-iyi-arkadasina-asik-olanlar/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:21:45 [alert] 6553#6553: *198917 write() "/var/run/nginx-cache/1/60/474354f7a87e990c681fc80be71ab601.0000128513" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /beyonceden-civileme-atlayis/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:21:47 [alert] 6553#6553: *198919 write() "/var/run/nginx-cache/1/60/474354f7a87e990c681fc80be71ab601.0000128515" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /beyonceden-civileme-atlayis/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:22:22 [alert] 6553#6553: *198925 write() "/var/run/nginx-cache/e/5f/476191d79467b79490937cba63d1c5fe.0000128517" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /yuzu-olmayan-kadin-canli-tablo-oldu/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com", referrer: "http://m.facebook.com/" 2016/08/19 08:22:36 [alert] 6553#6553: *198886 write() "/var/run/nginx-cache/c/cd/329081ce2cbc1b91a96e79c877094cdc.0000128519" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:22:45 [alert] 6553#6553: *198930 write() "/var/run/nginx-cache/9/66/77c743612c916b9016e612a7f27e7669.0000128521" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /7-milyon-dolarlik-bosanma/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com", referrer: "http://m.facebook.com/" 2016/08/19 08:22:49 [alert] 6553#6553: *198886 write() "/var/run/nginx-cache/1/9e/d50b248396f2967c1ad45031d8ad09e1.0000128523" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /tag/magaz/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:22:49 [alert] 6553#6553: *198933 write() "/var/run/nginx-cache/7/4e/bf5b692acbd5b2319aa9fe8c85e804e7.0000128525" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /ebru-gundes-ten-duygusal-huseyin-utku-paylasimi/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com", referrer: "https://www.google.com.tr/" 2016/08/19 08:23:16 [alert] 6553#6553: *198886 write() "/var/run/nginx-cache/b/f2/a58951386c935ba9a76b8256d6b19f2b.0000128527" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /tag/fitoterapi-uzmani-umit-aktas/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com" 2016/08/19 08:23:17 [alert] 6553#6553: *198953 write() "/var/run/nginx-cache/4/bd/06b1a3ab18cc0620f1b31eb2964bdbd4.0000128529" failed (28: No space left on device) while reading upstream, client: 10.135.13.187, server: magarazzi.com, request: "GET /anil-can-magazin-d-29-temmuz-2016-burc-yorumlari/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.magarazzi.com", referrer: "http://www.google.com.tr/"

How can I increase the Nginx cache size, or anybody know a way to purge Nginx cache automatically, with a CRON for example? Thanks a lot for your support!!

Anyone?

By default fastcgi cache is stored in this location

/var/run/nginx-cache

which is mounted on tmps file system /run and it usually has around 10% allocated size. How much RAM does your server has? 10% is setup for a reason so if you don’t have enough RAM increasing /tmpfs may not be a good idea. Anyway, you can check yours this way:

df -h and look for /run mount point. Depending on your server OS you can edit /etc/fstab file

sudo nano /etc/fstab

look for line similar to this:

tmpfs /run tmpfs noexec,nosuid,size=10%,mode=0755 0 0

edit size part to allocate more space, then close Ctrl-X and save. Then run:

mount -o remount /run and check to see if /run now has more size allocated to it.

Meanwhile, if you need to automatically clear NGINX/FASTCGI cache every day, you can add this to your crontab:

sudo crontab -e

and add the following line to it:

0 0 * * * ee clean --fastcgi

One other way to increase your allocated memory for FASTCGI is edit this file:

/etc/nginx/conf.d/fastcgi.conf

Find line:

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m

and adjust WORDPRESS:100m - where 100m is 100Mb according to your needs. But first, as I suggested above check how much memory to have allocated to /run.

Don’t forget to restart nginx and php-fpm services after this change.

sudo service php5.6-fpm restart && sudo service nginx restart

or

sudo service php7.0-fpm restart && sudo service nginx restart

if you use php 7.

Hi Tyrro,

Thanks for the exceptionally detailed answer, it really covers everything I need to know! I’d be very happy if you care to evaluate how I solved this problem;

Since our server is on DO and they use SSD’s, I decided to move nginx cache files from /var/run to /usr/share/nginx/cache/fcgi, and allocated 250MB for cache files.

So, my fastcgi configuration file starts with;

fastcgi_cache_path /usr/share/nginx/cache/fcgi levels=1:2 keys_zone=WORDPRESS:250m inactive=60m; Just wanted to make sure I am not doing anything wrong, just drop me a line if you stop by, cheers!

Have you tested if clear cache is working and cache files are being removed from this custom location?