问答社区
Openai设置(Nginx)反向代理配置,用于将请求转发到https://api.openai.com
在反向代理的配置中填写以下的代码:(复制双横线以内)
======================================================
#PROXY-START/
location ^~ /
{
proxy_pass https://api.openai.com;
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 REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# proxy_hide_header Upgrade;
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
set $static_filer4u4DiVj 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
{
set $static_filer4u4DiVj 1;
expires 1m;
}
if ( $static_filer4u4DiVj = 0 )
{
add_header Cache-Control no-cache;
}
}
#PROXY-END/
=================================================
这段代码是一个Nginx反向代理配置,用于将请求转发到https://api.openai.com。以下是代码的注释版本:
```nginx
#PROXY-START - 代理服务器开始
location ^~ / {
# 代理转发到OpenAI API
proxy_pass https://api.openai.com;
# 设置Host头为api.openai.com
proxy_set_header Host api.openai.com;
# 设置X-Real-IP为远程地址
proxy_set_header X-Real-IP $remote_addr;
# 设置X-Forwarded-For为代理添加的X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 设置REMOTE-HOST为远程地址
proxy_set_header REMOTE-HOST $remote_addr;
# 设置Upgrade头为HTTP升级
proxy_set_header Upgrade $http_upgrade;
# 设置Connection头为连接升级
proxy_set_header Connection $connection_upgrade;
# 设置代理HTTP版本为1.1
proxy_http_version 1.1;
# 启用代理SSL服务器名称
proxy_ssl_server_name on;
# 设置代理SSL协议为TLSv1、TLSv1.1和TLSv1.2
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# 隐藏Upgrade头(未启用)
# proxy_hide_header Upgrade;
# 添加X-Cache头,值为上游缓存状态
add_header X-Cache $upstream_cache_status;
# 设置Nginx缓存
# 设置静态文件缓存标志为0
set $static_filer4u4DiVj 0;
# 如果URI匹配特定类型的静态文件(如图片、CSS、JavaScript等),则设置静态文件缓存标志为1,并设置过期时间为1分钟
if ($uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$") {
set $static_filer4u4DiVj 1;
expires 1m;
}
# 如果静态文件缓存标志为0,则添加Cache-Control头,值为no-cache
if ($static_filer4u4DiVj = 0) {
add_header Cache-Control no-cache;
}
}
#PROXY-END - 代理服务器结束
```
这段代码的主要功能是将客户端的请求代理到OpenAI API,同时处理静态文件的缓存和HTTP头信息。