limit_req_zone $binary_remote_addr zone=public_otp_send:10m rate=6r/m;
limit_req_zone $binary_remote_addr zone=public_otp_verify:10m rate=24r/m;
limit_req_zone $binary_remote_addr zone=public_request_create:10m rate=10r/m;
limit_req_zone $binary_remote_addr zone=public_upload_init:10m rate=20r/m;
limit_conn_zone $binary_remote_addr zone=public_per_ip_conn:10m;

server {
    listen 80;
    server_name ruakb.ru www.ruakb.ru ruakb.online www.ruakb.online;
    server_tokens off;
    client_max_body_size 30m;
    client_body_timeout 15s;
    keepalive_timeout 20s;
    send_timeout 20s;
    limit_req_status 429;
    limit_conn public_per_ip_conn 50;

    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "DENY" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Permissions-Policy "geolocation=(), microphone=(), camera=(), payment=(), usb=()" always;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
        try_files $uri =404;
    }

    location = /api/public/otp/send {
        limit_req zone=public_otp_send burst=8 nodelay;
        proxy_pass http://frontend:80;
        proxy_http_version 1.1;
        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;
    }

    location = /api/public/otp/verify {
        limit_req zone=public_otp_verify burst=20 nodelay;
        proxy_pass http://frontend:80;
        proxy_http_version 1.1;
        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;
    }

    location = /api/public/requests {
        limit_req zone=public_request_create burst=10 nodelay;
        proxy_pass http://frontend:80;
        proxy_http_version 1.1;
        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;
    }

    location = /api/public/uploads/init {
        limit_req zone=public_upload_init burst=20 nodelay;
        proxy_pass http://frontend:80;
        proxy_http_version 1.1;
        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;
    }

    location / {
        proxy_pass http://frontend:80;
        proxy_http_version 1.1;
        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;
    }
}

server {
    listen 80 default_server;
    server_name _;
    return 308 http://ruakb.ru$request_uri;
}
