[Guide] Running a local package cache using nginx

Inspired by apalrd’s Caching Linux Package Repositories article and since I’ve got multiple installations of aerynOS, I set up a local caching server on my NAS to speed up installations/upgrades.

Here is the config file I use. Put it in your nginx’s conf.d or sites-enabled (or equivalent for your distro) directory:

# Global Cache settings
proxy_cache_path /path/to/cache/files levels=2:2 keys_zone=generic:500m inactive=3650d max_size=100g min_free=100g loader_files=1000 loader_sleep=50ms loader_threshold=300ms use_temp_path=off;
# Log with cache status
log_format cachelog '$remote_addr [$time_local] "$request" $status "$http_user_agent" "$upstream_cache_status"';

# URI paths to avoid cache
# These paths will change to indicate new release contents
# All other .stone files can be cached nearly indefinitely, as the
# version number is coded into the file name.
map $request_uri $nocache {
    ~stone.index 1;
    ~moss-root-index.json 1;
}

server {
    listen 8000 reuseport;
    listen [::]:8000 reuseport;

    #Log settings
    access_log /var/log/nginx/access.log cachelog;
    error_log /var/log/nginx/error.log;

    # Cache Location
    slice 1m;
    proxy_cache generic;
    proxy_ignore_headers Expires Cache-Control;
    proxy_cache_valid 200 206 3650d;
    proxy_cache_valid 301 302 0;
    proxy_set_header  Range $slice_range;
    proxy_cache_lock on;
    proxy_cache_lock_age 2m;
    proxy_cache_lock_timeout 1h;
    proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
    proxy_cache_revalidate on;
    #Nocache for those entries
    proxy_cache_bypass $nocache;
    proxy_no_cache $nocache;
    # 1G max file
    proxy_max_temp_file_size 1024m;
    # Cache key
    proxy_cache_key      $http_host$uri$slice_range;
    # Upstream Configuration
    proxy_next_upstream error timeout http_404;
    # Cache status
    add_header X-Cache-Status $upstream_cache_status;
    proxy_redirect off;
    proxy_ignore_client_abort on;
    # Upstream request headers
    proxy_ssl_server_name on;

    # aerynOS
    location / {
        proxy_pass https://cdn.aerynos.dev/;
        proxy_set_header Host "cdn.aerynos.dev";
    }

    # Stats endpoint
    location = /nginx_status {
        stub_status;
    }
}

Of note: I use http on a non-standard port so it doesn’t interfere with my NAS’s web interface and so I don’t have to worry about valid SSL certificates. So adjust that as desired for your setup. Also, change /path/to/cache/files to the directory you want to use to hold the cache.

Then in your aerynOS install, edit the base-uri setting in the .kdl files in /etc/moss/repo.d/ to point to your server (eg http://192.168.1.10:8000/) rather than https://cdn.aerynos.dev/. Run moss sync and as long as you don’t get any errors, you’re good to go.

1 Like