ClawCloud的Nginx反向代理做OpenAI API转发

in STEEM CN/中文last month

做OpenAI API开发的小伙伴可以看看此篇。

做本地使用或是某些服务器上有限制,那么,用一台不受限的服务器做转发就是必备的。一直用的是伦敦的IP、Nginx反向代理做了转发。虽然还有些最新的大模型用不了,但基础模型如o1-mini,4o这些可以使用。

ClawCloud有免费的资源,能迁过来免费用么?能想到,那基本上就可以做到!试了几个参数就顺利开通了。

claw1.jpg
ClawCloud Run界面

新起一个 App lanchpad, 以下是参数

nginx.jpg

  • image name: nginx
  • 1 cpu 512M
  • 打开 80 端口
  • Configmaps添加两个Nginx设置
    a. /etc/nginx/nginx.conf
    b. /etc/nginx/conf.d/aiwork.conf
    具体配置参考稍后的文本
  • localstorage:
    /etc/letsencrypt/

两个Nginx设置

a. /etc/nginx/nginx.conf

user  nginx;
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format upstreamlog '[$time_local] $remote_addr passed to: $upstream_addr: $request Upstream Response Time: $upstream_response_time Request time: $request_time';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

b. /etc/nginx/conf.d/aiwork.conf

server { 
    location / {
        proxy_pass  https://api.openai.com/;
        proxy_ssl_server_name on;
        proxy_set_header Host api.openai.com;
        proxy_set_header Connection '';
        proxy_http_version 1.1;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

这样就可以布署了!成功后,80端口对应的网址就是转发的地址, 点击打开后会有一行文本,如下

{
    "message": "Welcome to the OpenAI API! Documentation ..."
}

有上面的信息就是成功转发啰!

开发时多设置一行参数即可:

const Openai = new OpenAI({
  apiKey: apiKey,
  baseURL: "https://yufepxxxxx.clawcloudrun.com/v1"  //多加这行
})