File size: 3,020 Bytes
8caf0a0
 
0fbe9df
8caf0a0
 
 
 
 
 
 
 
 
0fbe9df
 
8caf0a0
 
 
 
 
 
 
 
 
 
edcfb9a
8caf0a0
 
 
edcfb9a
8caf0a0
 
b4e6edc
 
 
 
8caf0a0
 
 
 
0fbe9df
 
 
 
 
 
cc2ed29
 
 
8caf0a0
 
 
 
 
 
cc2ed29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4e6edc
 
 
 
 
 
 
 
 
cc2ed29
8caf0a0
 
 
 
 
 
859da78
8caf0a0
0fbe9df
8caf0a0
 
 
 
859da78
 
 
 
8caf0a0
 
 
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
worker_processes 1;
pid /tmp/nginx.pid;
error_log stderr info; # Log to stderr to see errors in HF Space Logs

events {
    worker_connections 1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    # HF Space runs as non-root, use /tmp for everything
    access_log /dev/stdout;
    client_body_temp_path /tmp/client_temp;
    proxy_temp_path       /tmp/proxy_temp;
    fastcgi_temp_path     /tmp/fastcgi_temp;
    uwsgi_temp_path       /tmp/uwsgi_temp;
    scgi_temp_path        /tmp/scgi_temp;

    sendfile        on;
    keepalive_timeout  65;

    upstream streamlit {
        server 127.0.0.1:8501;
    }

    upstream fastapi {
        server 127.0.0.1:8000;
    }

    upstream prometheus {
        server 127.0.0.1:9090;
    }

    server {
        listen 7860;
        server_name localhost;

        # Health endpoint for HF readiness check
        location /health {
            proxy_pass http://fastapi/health;
            proxy_set_header Host $host;
        }

        # FastAPI Documentation
        location /docs {
            proxy_pass http://fastapi/docs;
            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;
        }

        location /redoc {
            proxy_pass http://fastapi/redoc;
            proxy_set_header Host $host;
        }

        location /openapi.json {
            proxy_pass http://fastapi/openapi.json;
            proxy_set_header Host $host;
        }

        # FastAPI API Endpoints
        location /predict {
            proxy_pass http://fastapi/predict;
            proxy_set_header Host $host;
        }

        location /predictions {
            proxy_pass http://fastapi/predictions;
            proxy_set_header Host $host;
        }

        # Prometheus UI
        location /prometheus/ {
            proxy_pass http://prometheus/prometheus/;
            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;
        }

        # Streamlit (Catch-all)
        location / {
            proxy_pass http://streamlit;
            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 X-Forwarded-Host $host;
            
            # WebSocket support for Streamlit
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 86400;
            
            # Prevent 502 if Streamlit is slow
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
        }
    }
}