OneAPI在实现大模型访问的过程中提供了接近商业化的API生成服务,在商业化运用过程中,使用https加密访问可以提高访问的安全性。那么如何为OneAPI设置https访问呢?接下来,我们就使用Nginx的反向代理实现这一目标。
Nginx是一款高性能的http和反向代理服务器软件,可用于负载均衡、缓存、网页内容展示,应用应用程序反向代理等。本文介绍了如何使用Nginx实现OneAPI的反向代理。服务器配置情况如下:
主机租用,有固定外网地址
服务器硬件配置:2C2G
操作系统:ubuntu24.04
Nginx:1.26.2
一:安装Nginx
1、清除旧版的Nginx
#停止 Nginx 服务:
sudo systemctl stop nginx
#清除系统中所有旧的 Nginx 软件包:
sudo apt autoremove nginx* --purge 
#删除旧文件(如有)
sudo rm -rf /etc/nginx/2、安装相关依赖
sudo apt update && apt upgrade -y
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring dirmngr software-properties-common apt-transport-https3、安装Nginx
#导入 GPG 密钥
curl -fSsL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
#添加 Nginx 官方软件源
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
#安装 Nginx
sudo apt update
sudo apt install nginx
验证安装结果
nginx -v二:Nginx基本操作
1、检查状态
systemctl status nginx2、启动、停止和重启服务
sudo systemctl start nginx    # 启动服务
sudo systemctl stop nginx     # 停止服务
sudo systemctl reload nginx   # 重新加载配置文件,不中断服务
sudo systemctl restart nginx  # 重启服务3、设置开机自启动
sudo systemctl enable nginx   # 启用开机自启动
sudo systemctl disable nginx  # 禁止开机自启动三:生成签名证书
ssl证书的申请有多种方式,可以向你的主机提供商申请,有提供免费的,也有收费的。
免费的有certbot和acme等,另外还有自签名证书openssl。如果商用不建议使用自签名证书。
四:配置Nginx SSL和反向代理
nginx v1.26与早前的版本在配置上稍异,配置文件:
vim /etc/nginx/conf.d/default.conf全部修改如下:
server {
    listen       443 ssl;
    server_name  localhost;
    #SSL配置块
    ssl_certificate /etc/nginx/cert/server.crt;
    ssl_certificate_key /etc/nginx/cert/server.key;
    ssl_session_cache  shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    add_header Strict-Transport-Security "max-age=631139040; includeSubDomains; preload";
    #access_log  /var/log/nginx/host.access.log  main;
    # location / {
    #    root   /usr/share/nginx/html;
    #    index  index.html index.htm;
    # }
    #反向代理块
    location / {
        client_max_body_size  64m;
        proxy_pass http://localhost:3000;  #OneAPI的端口
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header Accept-Encoding gzip;
        proxy_read_timeout 300s;  # 设置访问延时时间。
    }
    #error_page  404              /404.html;
    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
1、第一块是设置SSL
    ssl_certificate /etc/nginx/cert/server.crt;   #证书存放位置
     ssl_certificate_key /etc/nginx/cert/server.key;  #密钥存放位置
     ssl_session_cache  shared:SSL:1m;
     ssl_session_timeout 5m;
     ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
     ssl_prefer_server_ciphers on;
     add_header Strict-Transport-Security "max-age=631139040; includeSubDomains; preload";
将证书和密钥存到你指定的位置,这里使用的路径:/etc/nginx/cert,文件名自行修改。
mkdir -p /etc/nginx/cert拷贝证书和密钥到此目录。
2、第二块是反向代理块
location / {
         client_max_body_size  64m;
         proxy_pass http://localhost:3000;  #OneAPI的端口
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
         proxy_cache_bypass $http_upgrade;
         proxy_set_header Accept-Encoding gzip;
         proxy_read_timeout 300s;  # 设置访问延时时间。
     }
注意:proxy_pass http://localhost:3000;就是OneAPI的后端地址,无论是源码安装,还是docker安装指定访问地址就可以了。
3、验证与使用
#验证
nginx -t
启动nginx
sudo systemctl restart nginx  # 重启服务
sudo systemctl reload nginx   # 重新加载配置文件,不中断服务好了,下面你用https://IP就可以访问了。当然为了安全的要求,你可以在防火墙那里把3000端口关掉。










