Automatically cache game files and updates at LAN parties using Nginx as a reverse proxy.
Forked with thanks from Lochnair/lancache
slice
and pcre
modulessudo -i
rm -rf /etc/nginx/
git clone https://github.com/zeropingheroes/lancache.git /etc/nginx
cd /etc/nginx
cp .env.example .env
nano .env
./prepare-configs.sh
systemctl restart nginx
Simply create and remove symlinks from caches-available/
to caches-enabled
ln -s /etc/nginx/caches-available/cache-name.conf /etc/nginx/caches-enabled/cache-name.conf
rm /etc/nginx/caches-enabled/cache-name.conf
To clear the cache, run:
/etc/nginx/scripts/clear-cache.sh
It is desirable to remove items from the cache, in case of corruption, or items being cached that shouldn't have been.
Currently there is no straightforward or reliable way to do this, without paying for Nginx Plus.
The closest we can get is to update the cached item from upstream by using the proxy_cache_bypass
config option to tell Nginx to re-fetch the item from upstream when it receives the custom request header "X-Cache-Bypass"
, forcing it to update the item in cache, and serve the updated item to the client. This can be done using the following CURL snippet:
$ export ITEM_TO_RE_REQUEST="http://valve123.steamcontent.com/depot/282440/chunk/d6556e1b9f7?l=14&e=1446464&sid=103124340&h=2da3a7ad94cab11ff1e6aac28"
$ curl --verbose --header "X-Cache-Bypass: 1" "$ITEM_TO_RE_REQUEST" > /dev/null
$ITEM_TO_RE_REQUEST
must be set to the full URL, including the original query string, so that Nginx can pass the request on and get a valid response. If Nginx does not get a valid response, the item will not be updated in the cache.
In the very unlikely event that the cache stores and serves up a corrupted file, only some download clients handle this helpfully for us:
Pragma: no-cache
and Cache-control: no-cache
which causes Nginx to bypass the cache and request a fresh copy of the file from upstream Pragma: no-cache
and Cache-control: no-cache
headers. This causes the game download to never completePragma: no-cache
and Cache-control: no-cache
and we ignore these or nothing would be cached. If a file is corrupt in the cache there is no mechanism for the Origin client to request Nginx fetches a fresh copy of the file from upstreamPlease submit pull requests to useful changes and enhancements.
Recommended reading: Nginx's Caching blog post