handwriting-ocr / nginx.conf
Z User
fix(hf): Gradio 6.0 + Dockerfile + v4.3.0 — clean HF Spaces deploy
08b497f
Raw History Blame Contribute Delete
4.6 kB
# =============================================================================
# OmniFile AI Processor - Nginx Configuration
# =============================================================================
# Load Balancer + Reverse Proxy + Rate Limiting + SSL (ready)
# =============================================================================
upstream fastapi_backend {
least_conn;
server backend:5001 max_fails=3 fail_timeout=30s;
keepalive 32;
}
# Rate limiting zones
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=30r/m;
limit_req_zone $binary_remote_addr zone=ocr_limit:10m rate=10r/m;
limit_req_zone $binary_remote_addr zone=upload_limit:10m rate=5r/m;
# Connection limits
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
server {
listen 80;
server_name _;
# 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 "strict-origin-when-cross-origin" always;
# Upload size limit (for large PDFs/images)
client_max_body_size 200M;
# Timeouts
proxy_connect_timeout 75s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
keepalive_timeout 65s;
# General API rate limit
location /api/ {
limit_req zone=api_limit burst=20 nodelay;
limit_conn conn_limit 10;
proxy_pass http://fastapi_backend;
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;
}
# OCR processing - stricter rate limit
location /api/ocr/ {
limit_req zone=ocr_limit burst=5 nodelay;
limit_conn conn_limit 5;
proxy_pass http://fastapi_backend;
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_read_timeout 600s;
}
# File upload - strictest rate limit
location /api/batch/ {
limit_req zone=upload_limit burst=3 nodelay;
limit_conn conn_limit 3;
proxy_pass http://fastapi_backend;
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_read_timeout 900s;
}
# Health check - no rate limit
location /health {
proxy_pass http://fastapi_backend/health;
proxy_set_header Host $host;
access_log off;
}
# API documentation - no rate limit
location /docs {
proxy_pass http://fastapi_backend/docs;
proxy_set_header Host $host;
}
location /redoc {
proxy_pass http://fastapi_backend/redoc;
proxy_set_header Host $host;
}
location /openapi.json {
proxy_pass http://fastapi_backend/openapi.json;
proxy_set_header Host $host;
}
# Default
location / {
limit_req zone=api_limit burst=20 nodelay;
proxy_pass http://fastapi_backend;
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;
}
# Error pages
error_page 502 503 504 /50x.html;
location = /50x.html {
default_type application/json;
return 503 '{"error": "Service temporarily unavailable", "status": 503}';
}
error_page 429 = @rate_limited;
location @rate_limited {
default_type application/json;
return 429 '{"error": "Too many requests. Please try again later.", "status": 429}';
}
}
# =============================================================================
# SSL Configuration (uncomment and configure when SSL is needed)
# =============================================================================
# server {
# listen 443 ssl http2;
# server_name your-domain.com;
#
# ssl_certificate /etc/nginx/ssl/fullchain.pem;
# ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# ssl_protocols TLSv1.2 TLSv1.3;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # ... same location blocks as above ...
# }
#
# # Redirect HTTP to HTTPS
# server {
# listen 80;
# server_name your-domain.com;
# return 301 https://$host$request_uri;
# }