# Rate limiting zone (Note: 10m is the size of the address pool, not time)
limit_req_zone $binary_remote_addr zone=main:10m rate=10r/s;

server {
    listen 80 default_server;

    root /usr/share/nginx/html;
    index index.html;

    charset utf-8;
    charset_types application/json;

    # Docker reverse proxy IP passthrough
    set_real_ip_from 172.16.0.0/12;
    real_ip_header X-Real-IP;

    # Log to the main (Docker stdout) log
    access_log /var/log/nginx/access.log;

    # Rate limiting: allow bursts with no delay
    limit_req zone=main burst=500 nodelay;

    location / {
        try_files $uri $uri/ /index.html;
    }

    # Force browsers to check for updated data
    location /data/ {
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin *;
        autoindex on;
    }

    location /twitter-images/ {
        add_header Cache-Control no-cache;
        add_header Access-Control-Allow-Origin *;
    }

    location ~ \.ics$ {
        add_header Content-Type text/calendar;
    }

    # CORS (for modern ES module support and third-party integrations)
    location /assets/ {
        add_header Access-Control-Allow-Origin *;
    }

    # Block access to the file used for screenshots
    location = /screenshots.html {
        internal;
    }
}

# Redirect www to non-www
server {
    listen 80;

    server_name www.splatoon2.ink;
    return 301 $scheme://splatoon2.ink$request_uri;
}
