# Replace domain, certificate paths, PHP socket and release directory before enabling.
server {
    listen 80;
    server_name house.example.com;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl;
    server_name house.example.com;
    root /srv/house/current/public;
    index index.html index.php;
    ssl_certificate /etc/letsencrypt/live/house.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/house.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    client_max_body_size 6m;
    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 Strict-Transport-Security "max-age=31536000" always;
    # Inline styles are required by the existing React component library; scripts remain same-origin.
    add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob:; font-src 'self' data:; connect-src 'self' wss://house.example.com; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'" always;

    location ^~ /admin/assets/ { try_files $uri =404; expires 1y; access_log off; }
    location ^~ /tenant/assets/ { try_files $uri =404; expires 1y; access_log off; }
    location = /admin/index.html { expires -1; }
    location = /tenant/index.html { expires -1; }
    location / { try_files $uri $uri/ /index.php?$query_string; }

    location /app/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 90s;
    }
    # Server-side signed broadcast requests use /apps; they must not be publicly proxied.
    location = /index.php {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root/index.php;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass unix:/run/php/php8.5-fpm.sock;
        fastcgi_hide_header X-Powered-By;
    }
    location ~ \.php$ { return 404; }
    location ~ /\. { deny all; }
}
