Skip to content

Instantly share code, notes, and snippets.

@Duncaen
Last active September 27, 2023 15:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Duncaen/645f119544a65ad0fd32e0e0400e22a3 to your computer and use it in GitHub Desktop.
Save Duncaen/645f119544a65ad0fd32e0e0400e22a3 to your computer and use it in GitHub Desktop.
voidlinux nginx caching repository proxy
proxy_cache_path /repo_cache use_temp_path=off keys_zone=repo_cache:5m max_size=20g inactive=7d;
server {
server_name _;
listen 80;
proxy_cache repo_cache;
# if the backend is not reachable use cache
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
# wait 5s to finish privious requests to the backend and try to use the cache
proxy_cache_lock on;
proxy_cache_lock_age 5s;
proxy_cache_lock_timeout 5s;
# timeouts if the backend is not reachable
proxy_connect_timeout 30s;
proxy_read_timeout 30s;
proxy_send_timeout 30s;
# ignore if the client aborts, to still fill the cache with the requested file
#proxy_ignore_client_abort on;
location / {
proxy_cache_revalidate on;
proxy_cache_valid 200 1m;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Mirror repo-fi.voidlinux.org;
proxy_pass https://repo-fi.voidlinux.org;
}
location ~ /(distfiles|live|logos|static) {
# stuff that can be cached for longer periods
proxy_cache_revalidate off;
proxy_cache_valid 200 7d;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Mirror repo-fi.voidlinux.org;
proxy_pass https://repo-fi.voidlinux.org;
}
location ~ \.xbps$ {
proxy_cache_revalidate off;
proxy_cache_valid 200 7d;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Mirror repo-fi.voidlinux.org;
proxy_pass https://repo-fi.voidlinux.org;
}
location ~ /(void-updates|xlocate) {
# this is updated daily, maybe increase cache time
proxy_cache_revalidate off;
proxy_cache_valid 200 10m;
add_header X-Cache-Status $upstream_cache_status;
add_header X-Mirror repo-fi.voidlinux.org;
proxy_pass https://repo-fi.voidlinux.org;
}
}
@erwin
Copy link

erwin commented Sep 30, 2022

When I use your mirror, repo-fi.voidlinux.org - verbatim as you have here, it works fine.

But when I change to a locally much faster mirror, for example in Vi run:

:%s/repo-fi.voidlinux/void.webconverger/g

And then restart nginx, on each proxied request I get:

502 Bad Gateway

Sending the X-Mirror header along on a curl request it still works fine

curl -H "X-Mirror repo-fi.voidlinux.org" https://void.webconverger.org/current/x86_64-repodata -o test

Yet when I do the same thing via nginx the request fails.


I also tried to get this to work with https://mirrors.bfsu.edu.cn/voidlinux/ but it seems this kind of proxy_pass directive only works on a hostname:

[emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except"

For right now I'm kind of working around this by using the Warsaw, PL mirror: void.sakamoto.pl.

Sure would like to know how to get the webconverger mirror to work with this, the Warsaw, PL mirror is about half the speed...

@Duncaen
Copy link
Author

Duncaen commented Sep 30, 2022

X-Mirror is a header sent to the client to show which mirror was used, the idea behind this was if you use multiple upstream mirrors this would help to debug issues. Sending that header from a client does nothing.

I think you would have to rewrite the request to include the target folder or use the same structure to make it work with different folder structures.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment