diff --git a/etc/gunicorn.conf.py b/etc/gunicorn.conf.py index 2d0c238..ae4ff32 100644 --- a/etc/gunicorn.conf.py +++ b/etc/gunicorn.conf.py @@ -6,5 +6,5 @@ max_requests = 1200 max_requests_jitter = 50 forwarded_allow_ips = "*" -# Run an additional thread so the GIL isn't sitting completely idle -threads = 2 +# Run additional threads so the GIL isn't sitting completely idle +threads = 4 diff --git a/etc/nginx.conf b/etc/nginx.conf index afd2897..05b56de 100644 --- a/etc/nginx.conf +++ b/etc/nginx.conf @@ -1,7 +1,10 @@ +proxy_cache_path /tmp/nginx_cache levels=1:2 keys_zone=nginxcache:10m max_size=150m; + server { listen 8000; access_log /dev/stdout; + error_log /dev/stderr; gzip_static on; @@ -12,30 +15,36 @@ server { more_set_headers "Server: Wouldn't you like to know"; server_tokens off; + proxy_buffers 32 4k; + proxy_connect_timeout 240; + proxy_headers_hash_bucket_size 128; + proxy_headers_hash_max_size 1024; + proxy_http_version 1.1; + proxy_read_timeout 240; + proxy_send_timeout 240; + + proxy_set_header Host $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header Proxy ""; + + proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; + proxy_cache_lock on; + proxy_cache_valid 404 1m; + location / { - proxy_buffers 32 4k; - proxy_connect_timeout 240; - proxy_headers_hash_bucket_size 128; - proxy_headers_hash_max_size 1024; - proxy_http_version 1.1; - proxy_read_timeout 240; - proxy_send_timeout 240; - - proxy_set_header Host $host; - proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; - proxy_set_header X-Forwarded-Host $host; - proxy_set_header Proxy ""; - proxy_pass http://localhost:8080; } location /static { - add_header Cache-Control "public, immutable, max-age=31536000"; - alias /app/collected-static; + proxy_cache nginxcache; + add_header X-Cache-Status $upstream_cache_status; + proxy_pass http://localhost:8080; } location /media { - add_header Cache-Control "public, immutable, max-age=3600"; - alias /app/media; + proxy_cache nginxcache; + add_header X-Cache-Status $upstream_cache_status; + proxy_pass http://localhost:8080; } } diff --git a/website/urls.py b/website/urls.py index 1614885..31f3e60 100644 --- a/website/urls.py +++ b/website/urls.py @@ -58,6 +58,11 @@ urlpatterns = [ ), path("favicon.ico", FaviconView.as_view()), path("", include(favicon_urls)), + re_path( + r"^%s(?P.*)$" % re.escape(settings.MEDIA_URL.lstrip("/")), + cache_control(max_age=60 * 60)(serve), + {"document_root": settings.MEDIA_ROOT}, + ), ] @@ -72,15 +77,6 @@ if settings.DEBUG: # Add django-debug-toolbar urlpatterns.append(path("__debug__/", include("debug_toolbar.urls"))) - urlpatterns.append( - # Media is served by nginx in production - re_path( - r"^%s(?P.*)$" % re.escape(settings.MEDIA_URL.lstrip("/")), - cache_control(max_age=60 * 60)(serve), - {"document_root": settings.MEDIA_ROOT}, - ) - ) - if settings.DEBUG or settings.TEST: urlpatterns.extend(