File size: 3,165 Bytes
2f4849d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Sentinel-AI reverse proxy example (nginx)
#
# Terminates TLS, forwards to uvicorn on 127.0.0.1:8000, adds a second layer of
# rate limiting, and enforces an upload cap.  Replace the certificate paths and
# domain before use.
#
# Backend:
#   cd backend
#   python -m uvicorn main:app --host 127.0.0.1 --port 8000
#
# Then set in .env:
#   SENTINEL_API_KEY=<demo access token>
#   CORS_ORIGINS=https://sentinel.example.com
#   TRUST_PROXY_HEADERS=1

limit_req_zone $binary_remote_addr zone=sentinel_api:10m rate=10r/s;

upstream sentinel_backend {
    server 127.0.0.1:8000;
    keepalive 32;
}

# Redirect HTTP to HTTPS.
server {
    listen 80;
    listen [::]:80;
    server_name sentinel.example.com;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name sentinel.example.com;

    ssl_certificate     /etc/letsencrypt/live/sentinel.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/sentinel.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;

    # Upload cap must be a little larger than MAX_UPLOAD_BYTES (10 MiB default).
    client_max_body_size 12m;
    client_body_timeout 30s;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "DENY" always;
    add_header Referrer-Policy "no-referrer" always;

    # Optional: serve the built dashboard from the same origin.  TanStack Start
    # may require its own server if you use SSR; a static build works with this
    # block.
    # root /var/www/sentinel/frontend/dist;
    # index index.html;
    # location / {
    #     try_files $uri $uri/ /index.html;
    # }

    location /api/ {
        limit_req zone=sentinel_api burst=20 nodelay;

        proxy_pass http://sentinel_backend;
        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;
        proxy_set_header Connection "";
        proxy_connect_timeout 5s;
        proxy_send_timeout 90s;
        proxy_read_timeout 90s;
    }

    location = /health {
        proxy_pass http://sentinel_backend;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        access_log off;
    }

    location /docs {
        # Optional: protect the API docs behind the same token or an IP allow-list.
        # allow 10.0.0.0/8;
        # deny all;
        proxy_pass http://sentinel_backend;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    location /redoc {
        proxy_pass http://sentinel_backend;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
    }

    location / {
        return 404;
    }
}