forked from BadgerHobbs/OpenAI-API-Gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway-nginx.conf
More file actions
53 lines (40 loc) · 2.06 KB
/
Copy pathgateway-nginx.conf
File metadata and controls
53 lines (40 loc) · 2.06 KB
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
# Define a shared memory zone "api" to store states of limiting requests. The maximum size for this zone is 10MB. RATE_LIMIT sets the rate of requests
limit_req_zone $binary_remote_addr zone=api:10m rate=${RATE_LIMIT};
# Set response body size (OpenAI API supports 25MB)
client_max_body_size ${CLIENT_MAX_BODY_SIZE};
# Start server configuration
server {
# Configure Nginx to listen on port 80
listen 80;
# "location /" defines how to respond to requests for URLs beginning with /
location / {
# Pre-flight requests (OPTIONS)
if ($request_method = "OPTIONS") {
add_header "Access-Control-Allow-Origin" "${CORS_ORIGIN}";
add_header "Access-Control-Allow-Methods" "${CORS_METHODS}";
add_header "Access-Control-Allow-Headers" "${CORS_HEADERS}";
add_header "Access-Control-Allow-Credentials" "${CORS_CREDENTIALS}";
add_header "Access-Control-Max-Age" ${CORS_MAX_AGE};
add_header "Content-Length" ${CONTENT_LENGTH};
add_header "Content-Type" "${CONTENT_TYPE}";
return 204;
}
# If the client is not authorized ($http_authorization not equals to "Bearer ${GATEWAY_API_KEY}"), return an HTTP 401 Unauthorized status code
if ($http_authorization != "Bearer ${GATEWAY_API_KEY}") {
return 401;
}
# Set the proxy server to be used for requests
proxy_pass ${OPENAI_API_HOST};
# Set the header fields for requests being proxied
proxy_set_header Authorization "Bearer ${OPENAI_API_KEY}";
proxy_set_header api-key "${OPENAI_API_KEY}";
# Turn off buffering of responses from the proxy server
proxy_buffering off;
# Transfer the host name from the request line to the SSL handshake
proxy_ssl_server_name on;
# Enable limiting of requests by defining its parameters with this directive
limit_req zone=api;
# Return client status 429 (Too Many Requests) when the rate of requests exceeds the defined limit
limit_req_status 429;
}
}