File size: 4,961 Bytes
dcedadb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Nginx reverse proxy configuration example for Z.AI2API
# This example shows how to deploy the service behind Nginx with a custom path prefix

# Example 1: Deploy at http://your-domain.com/ai2api
server {
    listen 80;
    server_name your-domain.com;

    # Forward requests with /ai2api prefix to the backend service
    location /ai2api {
        # Remove trailing slash redirect (optional, but recommended)
        rewrite ^(/ai2api)$ $1/ permanent;

        # Proxy to the backend service
        proxy_pass http://127.0.0.1:7860;

        # Pass original host and IP information
        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;

        # IMPORTANT: Tell the backend about the path prefix
        # This ensures all generated URLs include the prefix
        proxy_set_header X-Forwarded-Prefix /ai2api;

        # WebSocket and SSE support (for streaming responses)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        # Disable buffering for streaming responses
        proxy_buffering off;
        proxy_cache off;

        # Timeout settings (adjust as needed)
        proxy_connect_timeout 60s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
    }
}

# Example 2: Deploy at http://your-domain.com/api/chat
server {
    listen 80;
    server_name example.com;

    location /api/chat {
        # Proxy configuration
        proxy_pass http://127.0.0.1:7860;

        # Headers
        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-Prefix /api/chat;

        # SSE/WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_buffering off;
        proxy_cache off;
    }
}

# Example 3: Deploy with SSL (HTTPS)
server {
    listen 443 ssl http2;
    server_name secure.example.com;

    # SSL configuration
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

    location /ai2api {
        proxy_pass http://127.0.0.1:7860;

        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 https;
        proxy_set_header X-Forwarded-Prefix /ai2api;

        # SSE/WebSocket support
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_buffering off;
        proxy_cache off;

        # Security headers (optional)
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options DENY;
        add_header X-XSS-Protection "1; mode=block";
    }
}

# Example 4: Load balancing with multiple backend instances
upstream ai2api_backend {
    # Round-robin by default
    server 127.0.0.1:7860;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;

    # Or use least connections
    # least_conn;

    # Or use IP hash for session persistence
    # ip_hash;
}

server {
    listen 80;
    server_name loadbalanced.example.com;

    location /ai2api {
        proxy_pass http://ai2api_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_set_header X-Forwarded-Prefix /ai2api;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_buffering off;
        proxy_cache off;
    }
}

# Important Notes:
#
# 1. Set ROOT_PATH in your .env file to match the Nginx location path:
#    ROOT_PATH=/ai2api
#
# 2. Restart both Nginx and the application after configuration changes:
#    sudo systemctl reload nginx
#    docker-compose restart (or restart your application)
#
# 3. Access URLs will include the prefix:
#    - Admin panel: http://your-domain.com/ai2api/admin/login
#    - API endpoint: http://your-domain.com/ai2api/v1/chat/completions
#    - Health check: http://your-domain.com/ai2api/v1/models
#
# 4. For Docker deployments, make sure to:
#    - Add ROOT_PATH to docker-compose.yml environment variables
#    - Expose the container port (7860 by default)
#
# 5. Common issues:
#    - 404 errors: Check that ROOT_PATH matches the Nginx location path exactly
#    - CORS errors: Verify proxy headers are set correctly
#    - Streaming not working: Ensure proxy_buffering is off
#    - Admin panel CSS/JS not loading: Confirm static files are served with the prefix