惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
爱范儿
爱范儿
量子位
Martin Fowler
Martin Fowler
V
V2EX
博客园 - 三生石上(FineUI控件)
I
InfoQ
MongoDB | Blog
MongoDB | Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
Engineering at Meta
Engineering at Meta

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
轶哥 · 2023-04-30 · via 轶哥博客

在本教程中,我们将介绍如何使用Nginx反向代理访问OpenAI API,并提供相应的测试方法。主要目标是保证Server-sent events (SSE)类型响应的流畅输出,从而提供良好的用户体验。

步骤1:安装最新版Nginx

首先,我们需要安装最新版的Nginx。在Ubuntu上,可以使用以下命令安装:

echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list
curl -o /tmp/nginx_signing.key https://nginx.org/keys/nginx_signing.key
sudo mv /tmp/nginx_signing.key /etc/apt/trusted.gpg.d/nginx_signing.asc
sudo apt update
sudo apt install nginx

对于使用的Debian系统,请按照以下步骤操作:

  1. 使用文本编辑器打开 /etc/apt/sources.list 并在底部添加以下内容:

    deb http://nginx.org/packages/mainline/debian/ stretch nginx
    
  2. 导入软件源的签名密钥,并将其添加到apt:

    sudo wget http://nginx.org/keys/nginx_signing.key
    sudo apt-key add nginx_signing.key
    
  3. 安装Nginx:

    sudo apt update
    sudo apt install nginx
    

在安装完成后,可以使用以下命令启动并设置Nginx服务为开机自启:

sudo systemctl enable nginx --now

步骤2:配置Nginx

接下来,我们配置Nginx来反向代理OpenAI API。在Nginx的配置文件(/etc/nginx/conf.d/openai.conf)中添加以下内容:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;
    ssl_certificate /home/azureuser/.acme.sh/example.com_ecc/fullchain.cer;
    ssl_certificate_key /home/azureuser/.acme.sh/example.com_ecc/example.com.key;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    add_header Strict-Transport-Security "max-age=31536000";
    charset utf-8;

    location / {
        proxy_pass https://api.openai.com;
        proxy_ssl_name api.openai.com;
        proxy_ssl_server_name on;
        proxy_set_header Host api.openai.com;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        chunked_transfer_encoding off;
        proxy_set_header X-Real-IP [your_us_server_ip];
        proxy_read_timeout 3600;
        proxy_buffering off;
        proxy_cache off;
        proxy_redirect off;
        proxy_hide_header Cache-Control;
    }

    location /v2 {
        return 404;
    }
}

请将配置中的example.com替换为您实际使用的域名,并将[your_us_server_ip]替换为您的美国服务器IP地址。如果使用Azure OpenAI接口,只需要替换api.openai.com为你的Azure OpenAI Endpoint的Host。

通过以上方法,您可以测试和验证Nginx反向代理配置是否正确。本教程向您展示了如何使用Nginx反向代理访问OpenAI API,并提供了相应的测试方法。主要目标是保证Server-sent events (SSE)类型响应的流畅输出,从而提供良好的用户体验。