nginx-autoinstall icon indicating copy to clipboard operation
nginx-autoinstall copied to clipboard

Enhancement suggestion: for PageSpeed, automatically create cache directory

Open GwynethLlewelyn opened this issue 5 years ago • 3 comments

Hi there! First of all: awesome script, I found it by mere chance, and it works so flawlessly that it's actually scary...

Now for a suggestion — I've mostly used this script to add pagespeed & brotli support on nginx. pagespeed in particular has a gazillion options, but one that pretty much everyone will be using is, obviously, adding the cache directory.

By convention — and your script follows this convention! — /var/ngx_pagespeed_cache is the default. However, a common configuration change (at least on Ubuntu/Debian) is to use a tmpfs-mounted directory for the cache, say, /run/ngx_pagespeed_cache (and just use a symlink to keep the rest of the default configuration working).

This has a problem: when rebooting, as expected, the content of /run will be lost, and when nginx starts, it expects /run/ngx_pagespeed_cache to exist, and because it doesn't exist any more (just a broken symlink pointing to it), nginx will fail to run.

On systems using systemd, one simple solution is to add a small file under /usr/lib/tmpfiles.d, which will tell the system what to create when starting up. In fact, a lot of packages (mysql, php-fpm, among so many others...) use this method to set up sockets, for example, which are conveniently created on a tmpfs volume for added speed.

It would be nice if a future version of nginx-autoinstall would be able to create something like that automatically, e.g.:

echo "d /run/ngx_pagespeed_cache 0755 www-data www-data - -" > /usr/lib/tmpfiles.d/nginx.conf
ln -s /run/ngx_pagespeed_cache /var/ngx_pagespeed_cache

This could be one of the (many) extra options for those interested in having a tmpfs-based cache for pagespeed...

Keep updating nginx-autoinstall, great job so far, and thanks again!

GwynethLlewelyn avatar Jun 04 '19 22:06 GwynethLlewelyn

Interesting suggestion, thanks

angristan avatar Jun 04 '19 22:06 angristan

EDIT: Foot in mouth. Create a directory. Insert this into fstab for nginx or other uses in your stack: tmpfs /var/cache/stack_cache tmpfs size=256m,mode=0775,uid=www-data,gid=www-data 0 0 Creating a tmpfs would be a headache because it's only usable by root, and it is, well, temp. I've been down this rabbit hole.

Most flavors of Linux offer tmpfs device which any user can use called /dev/shm. It is not mounted to a specific directory by default, but you can still use it as one. Just create a directory in /dev/shm and then symlink it to wherever you want. You can give the created directory any permissions you choose, so that www-data and PHP users can use it. This is a RAM backed device, so what's there is in memory by default. You can create any directories you need inside /dev/shm Naturally, files placed here will not survive a reboot, and if your machine starts swapping, /dev/shm won't help you.

adamaayala avatar Jun 30 '19 13:06 adamaayala

Whoops. Sorry. I sort of get lost with all threads waiting for my input... and this one has been around for two years. Ugh!

Note that I originally suggested this as an option — not as a default setting — which, as you so well pointed out, could be a really bad idea.

However, there are many other weird and potentially dangerous options on the script... if you don't know what they're for. That's why they are options...

GwynethLlewelyn avatar Jul 06 '21 19:07 GwynethLlewelyn