# Perga default Nginx config for self-hosted installation # This file is mounted into the nginx container at /etc/nginx/conf.d/default.conf # empty keys exempt ordinary API requests and CORS preflight from auth limits map "$request_method:$uri" $perga_login_limit_key { default ""; ~^POST:/api/v1/auth/(access_token|access_token_json|google)/?$ $binary_remote_addr; } map "$request_method:$uri" $perga_signup_limit_key { default ""; ~^POST:/api/v1/auth/signup/?$ $binary_remote_addr; } limit_req_zone $perga_login_limit_key zone=perga_login:1m rate=10r/m; limit_req_zone $perga_signup_limit_key zone=perga_signup:1m rate=5r/m; limit_req_status 429; client_max_body_size 50m; # Upstreams for internal services upstream perga_api { server api:8000; keepalive 64; } upstream perga_web { server web:80; keepalive 64; } map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; limit_req zone=perga_login burst=10 nodelay; limit_req zone=perga_signup burst=5 nodelay; error_page 429 = @auth_rate_limited; location @auth_rate_limited { # use the API CORS allowlist without executing the original auth request rewrite ^ /_internal/rate-limited? break; proxy_pass http://perga_api; proxy_method GET; proxy_pass_request_body off; proxy_set_header Content-Length ""; proxy_set_header Origin $http_origin; proxy_intercept_errors off; } # Proxy API under /api/ location /api/ { proxy_pass http://perga_api/api/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 300; } # Serve the web UI for all other routes location / { proxy_pass http://perga_web/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_read_timeout 300; } gzip on; gzip_types text/plain text/css application/json application/javascript application/xml+rss text/javascript image/svg+xml; gzip_min_length 1024; }