OpenAI 反向代理API接口 Nginx配置
话不多说,直接上nginx配置:
server {
listen 80;
server_name openai.xxx.com;
# 自动重定向到 HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name openai.xxx.com;
# SSL 证书配置
ssl_certificate /root/cert/openai.xxx.com.pem;
ssl_certificate_key /root/cert/openai.xxx.com.key;
# 推荐的安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# 反向代理设置
location / {
proxy_pass https://api.openai.com/;
proxy_http_version 1.1;
proxy_set_header Host api.openai.com;
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_ssl_server_name on;
proxy_ssl_name api.openai.com;
# 防止 WebSocket 断连(如有需要)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# 可选:限制上传大小
client_max_body_size 20M;
# 可选:开启压缩
gzip on;
gzip_types application/json text/plain text/css application/javascript;
gzip_min_length 1000;
# 可选:访问日志
access_log /var/log/nginx/api.access.log;
error_log /var/log/nginx/api.error.log;
}