proxy_cache_path /tmp/nginx-uc-apt-cache levels=1:2 keys_zone=uc-apt-cache:10m max_size=4g inactive=300s;

server {
	listen 80;
	server_name {{ server_name }};

	rewrite ^ https://$host$request_uri? permanent;
}

server {
	listen 443 ssl;
	server_name {{ server_name }};
	root /home/skyreach/htdocs/skyreach_site/static;

	access_log /var/log/nginx/skyreach_access.log;
	error_log /var/log/nginx/skyreach_error.log;

	location /media {
		alias /home/skyreach/htdocs/skyreach_site/media;
		expires 30d;
		add_header Pragma public;
		add_header Cache-Control "public";
	}
	location /static {
		alias /home/skyreach/htdocs/skyreach_site/static;
		expires 30d;
		add_header Pragma public;
		add_header Cache-Control "public";
	}

	location /remote-event/ {
		proxy_pass http://localhost:6586/;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_read_timeout 12h;
	}

	location / {
		uwsgi_pass unix:///home/skyreach/htdocs/skyreach_site/uwsgi/uwsgi.sock;
		include /etc/nginx/uwsgi_params;
	}

	# Old APT proxy urls
	location /fr.archive.ubuntu.com/ubuntu {
		rewrite ^/(.*)$ /apt/http/$1?;
	}
	location /security.ubuntu.com/ubuntu {
		rewrite ^/(.*)$ /apt/http/$1?;
	}
	location /old-releases.ubuntu.com/ubuntu {
		rewrite ^/(.*)$ /apt/http/$1?;
	}
	# APT proxy
	location /apt {
		location ~ ^/apt/(http|https)/([\w\.\-\_]+.ubuntu.com/ubuntu|ftp.debian.org/debian|mozilla.debian.net|panel.ubicast.eu/packaging/apt)/(.*)$ {
			resolver 127.0.0.1;
			proxy_pass $1://$2/$3;
			proxy_cache uc-apt-cache;
			# do not consider secure urls as new files
			proxy_cache_key $scheme$proxy_host$uri;
			# only one request at a time will be allowed to populate a new cache element
			proxy_cache_lock on;
			# how long should the data be kept in the cache
			proxy_cache_valid 200 7d;
			# instruct browser to cache this
			expires 7d;
			# headers
			proxy_ignore_headers "Cache-Control" "X-Accel-Expires" "Expires";
			add_header X-Cache $upstream_cache_status;
		}
	}
}
