baton/nginx/baton.conf
2026-03-20 23:44:58 +02:00

99 lines
4.2 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# nginx/baton.conf — шаблон конфигурации для деплоя Baton
#
# Замените <YOUR_DOMAIN> на реальный домен перед использованием.
# Токен бота НЕ включается в этот файл — он передаётся только через
# переменную окружения BOT_TOKEN в самом приложении.
#
# Деплой:
# sudo cp nginx/baton.conf /etc/nginx/sites-available/baton
# sudo ln -s /etc/nginx/sites-available/baton /etc/nginx/sites-enabled/baton
# sudo nginx -t && sudo systemctl reload nginx
# ---------------------------------------------------------------------------
# Маскировка BOT_TOKEN в access_log (defence in depth)
#
# Хотя текущий webhook-эндпоинт (/api/webhook/telegram) не содержит токен
# в URL, этот map-блок защищает на случай, если в будущем появится маршрут
# вида /bot<TOKEN>/... (например, при смене архитектуры или временном дебаге).
# ---------------------------------------------------------------------------
map $request_uri $masked_uri {
default $request_uri;
"~^(/bot)[^/]+(/.*)$" "$1[REDACTED]$2";
}
log_format baton_secure '$remote_addr - $remote_user [$time_local] '
'"$request_method $masked_uri $server_protocol" '
'$status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
# ---------------------------------------------------------------------------
# HTTP → HTTPS redirect
# ---------------------------------------------------------------------------
server {
listen 80;
server_name baton.itafrika.com;
return 301 https://$server_name$request_uri;
}
# ---------------------------------------------------------------------------
# HTTPS: проксирование к FastAPI (uvicorn на порту 8000)
# ---------------------------------------------------------------------------
server {
listen 443 ssl;
server_name baton.itafrika.com;
ssl_certificate /etc/letsencrypt/live/baton.itafrika.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/baton.itafrika.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# Все запросы логируются с маскированным URI
access_log /var/log/nginx/baton_access.log baton_secure;
error_log /var/log/nginx/baton_error.log warn;
# Заголовки X-Telegram-Bot-Api-Secret-Token НЕ логируются —
# они передаются только в proxy_pass и не попадают в access_log.
# API → FastAPI
location /api/ {
proxy_pass http://127.0.0.1:8000;
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 30s;
proxy_send_timeout 30s;
proxy_connect_timeout 5s;
}
# Health → FastAPI
location /health {
proxy_pass http://127.0.0.1:8000;
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;
}
# Admin API → FastAPI (UI-страница /admin.html раздаётся статикой ниже)
location /admin/users {
proxy_pass http://127.0.0.1:8000;
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 30s;
proxy_send_timeout 30s;
proxy_connect_timeout 5s;
}
# Статика фронтенда (SPA)
location / {
root /opt/baton/frontend;
try_files $uri /index.html;
}
}