# SearXNG reverse proxy via Nginx
# Place in /etc/nginx/sites-available/searxng and symlink to sites-enabled
# Then access at http://search.local (or your hostname)

upstream searxng_backend {
    server 127.0.0.1:8080;
    keepalive 64;
}

server {
    listen 80;
    server_name search.local;  # Change to your hostname or IP

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "no-referrer" always;

    # Increase body size for POST queries
    client_max_body_size 50m;

    location / {
        proxy_pass http://searxng_backend;
        proxy_http_version 1.1;

        # Pass real client info
        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_set_header Connection "";

        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        # Buffering — keep on for performance
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
    }

    # Metrics endpoint (optional, password-protected)
    location /metrics {
        proxy_pass http://searxng_backend/metrics;
        proxy_set_header Host $host;
        auth_basic "Metrics";
        auth_basic_user_file /etc/nginx/.searxng_metrics_htpasswd;
    }

    # Logging
    access_log /var/log/nginx/searxng_access.log;
    error_log /var/log/nginx/searxng_error.log;
}
