agneevX / nginxcache-for-traefik

How do you use something as good as Traefik and make stuff faster? This is how.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NGINX cache for Traefik

Nginx and Traefik logos

While Traefik is a really great and modern load-balancer, it does not support caching at the moment.

This project aims to solve that by putting Nginx, a caching load-balancer and reverse proxy in front of it.

This is written for Docker, but the nginx config can be applied to any configuration.

Features

  • Cache is stored and served entirely from memory
  • Returns stale items (and refreshes it), so cached content is always returned
  • Only static assets are cached... does not affect changing content like API queries
  • HTTP errors like 404 are not cached.

Nginx config can be found in the ./nginx folder.

Usage

Using Docker Compose:

docker compose up -d

Or alternatively, apply nginx.conf to your configuration.

Persistent caching

To persist the cache across nginx container restarts or system reboots:

  1. Remove the shm_size line from docker-compose.yml
  2. Mount a folder to store caching data, for example:
mkdir nginx-cache
...
  volumes:
    - ./nginx:/etc/nginx/conf.d:ro
    - ./nginx-cache:/cache
  1. Change /dev/shm in the first line in nginx.conf to /cache, per the example above.
  2. Run docker compose up -d or restart nginx

Test caching

To check whether an item is cached or not, check the X-Cache-Status header in response headers.

Here's an example with curl:

~> curl -s -I "http://10.0.0.99/login.html" | grep Cache
X-Cache-Status: MISS

~> curl -s -I "http://10.0.0.99/login.html" | grep Cache
X-Cache-Status: HIT

To-do

  • add HTTPS support

About

How do you use something as good as Traefik and make stuff faster? This is how.

License:MIT License