resolver 8.8.8.8 8.8.4.4;

# Pacman Cache
server
{
	listen      8000;
	server_name pkg.actcur.com;
	root        /srv/http/pacman-cache;
	autoindex   on;

	# Requests for aur.db and sig files should stay here
	location ~ aur-local\.(db|sig){
		try_files $uri @pkg_mirror;
	}
	#TEMPORARY. Requests for teampass.db and sig files should stay here
	location ~ teampass-temp\.(db|sig){
		try_files $uri @pkg_mirror;
	}

	# Requests for package db and signature files should redirect upstream without caching
	location ~ \.(db|sig)$ {
		proxy_pass http://mirrors$request_uri;
	}

	# Requests for actual packages should be served directly from cache if available.
	#   If not available, retrieve and save the package from an upstream mirror.
	location ~ \.tar\.(xz|zst)$ {
		try_files $uri @pkg_mirror;
	}

	# Retrieve package from upstream mirrors and cache for future requests
	location @pkg_mirror {
		proxy_store    on;
		proxy_redirect off;
		proxy_store_access  user:rw group:rw all:r;
		proxy_next_upstream error timeout http_404;
		proxy_pass          http://mirrors$request_uri;
	}
}

# Upstream Arch Linux Mirrors
# - Configure as many backend mirrors as you want in the blocks below
# - Servers are used in a round-robin fashion by nginx
# - Add "backup" if you want to only use the mirror upon failure of the other mirrors
# - Separate "server" configurations are required for each upstream mirror so we can set the "Host" header appropriately
upstream mirrors {
	server localhost:8001;
	server localhost:8002 backup;
	server localhost:8003 backup;
        server localhost:8004 backup;
        server localhost:8005 backup;
        server localhost:8006 backup;
        server localhost:8007 backup;
        server localhost:8008 backup;
        server localhost:8009 backup;
        server localhost:8010 backup;
}

{% from 'mirrors.jinja' import mirrors %}

{%- set port=8000 -%}
{%- if mirrors is defined -%}
  {%- for mirror in mirrors -%}
    {%- set port=port+1 %}
    {%- set lst=mirror.split('/') %}

# Arch Mirror {{ lst[2] }} Proxy Configuration
server
{
	listen      {{ port + loop.index }};
	server_name localhost;

	location / {
		proxy_pass       {{ mirror }}$request_uri;
		proxy_set_header Host {{ lst[2] }};
	}
}
  {% endfor -%}
{%- endif -%}