Skip to content

Instantly share code, notes, and snippets.

@carlio
Created December 9, 2012 11:00
Show Gist options
  • Save carlio/4244267 to your computer and use it in GitHub Desktop.
Save carlio/4244267 to your computer and use it in GitHub Desktop.
nginx config to act as cheeseshop
# the inactive time is set really high so that we can still serve stale entries if pypi is down
proxy_cache_path /srv/pypi/cache/html levels=1:2 keys_zone=pypi-html-cache:8m max_size=100m inactive=2000h;
proxy_cache_path /srv/pypi/cache/package levels=1:2 keys_zone=pypi-package-cache:100m max_size=1000m inactive=2000h;
proxy_temp_path /srv/pypi/cache/tmp;
server {
listen 80;
server_name pypi.yourdomain.com;
access_log /var/log/nginx/pypi_access.log;
error_log /var/log/nginx/pypi_error.log;
root /path/to/your/pypi/packages/;
rewrite_log on;
location /packages/ {
# a request to /packages/ means the client wants a tarball of a package.
# first we see if we have the file in our package directory (ie, it is a
# private package) and if not, we fall back on the proxy to the main pypi
try_files $uri $uri/ @pypi_packages;
}
location / {
# redirect an empty path to /simple to match the behaviour of pypi.python.org
rewrite ^ http://pypi.yourdomain.com/simple$uri;
}
location /simple/ {
# similar to packages - we'll try to serve our own index files if we have them
# and if not, fall back on the main pypi
alias /path/to/your/pypi/packages/;
try_files $uri $uri/ @pypi_html;
}
location @pypi_html {
# we declare separate proxies for HTML and packages to give different TTLs
# this is because we want the HTML to refresh much quicker (to get information
# about new releases) but keep packages much longer (to protect against pypi
# downtime)
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# cache everything!
proxy_cache_min_uses 0;
proxy_cache pypi-html-cache;
# cache HTML for a maximum of 1 hour
proxy_cache_valid 200 302 1h;
proxy_cache_valid 404 1m;
proxy_pass http://pypi.python.org;
# serve what we have if pypi is down
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
}
location @pypi_packages {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# cache everything!
proxy_cache_min_uses 0;
proxy_cache pypi-package-cache;
proxy_ignore_headers "Cache-Control";
# cache packages for a day
proxy_cache_valid 200 302 24h;
proxy_cache_valid 404 1m;
proxy_pass http://pypi.python.org;
# serve what we have if pypi is down
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment