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

推荐订阅源

V
V2EX
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
Help Net Security
Help Net Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
P
Palo Alto Networks Blog
Spread Privacy
Spread Privacy
S
Securelist
V2EX - 技术
V2EX - 技术
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
Cyberwarzone
Cyberwarzone
C
Cisco Blogs
Cloudbric
Cloudbric
GbyAI
GbyAI
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
P
Proofpoint News Feed
SecWiki News
SecWiki News
T
Tailwind CSS Blog
腾讯CDC
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
T
Troy Hunt's Blog
G
Google Developers Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cisco Talos Blog
Cisco Talos Blog
D
DataBreaches.Net
V
Vulnerabilities – Threatpost
博客园 - 叶小钗
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Know Your Adversary
Know Your Adversary
T
Tor Project blog
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
Y
Y Combinator Blog
H
Help Net Security
Latest news
Latest news

轶哥博客

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 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 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 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)类型响应的流畅输出,从而提供良好的用户体验。