Qwen3-4B-Instruct部署教程:HTTPS反向代理+Nginx负载均衡配置
Qwen3-4B-Instruct部署教程:HTTPS反向代理+Nginx负载均衡配置
1. 模型介绍与部署准备
Qwen3-4B-Instruct-2507是Qwen3系列的端侧/轻量旗舰模型,原生支持256K token(约50万字)上下文窗口,并可扩展至1M token,能够轻松处理整本书、大型PDF、长代码库等长文本任务。
1.1 基础环境要求
在开始部署前,请确保您的服务器满足以下要求:
- 操作系统:Ubuntu 20.04/22.04或CentOS 7/8
- GPU:NVIDIA显卡,显存≥8GB(推荐RTX 3090/A10G及以上)
- CUDA:11.8或12.x
- Python:3.9或3.10
- 内存:≥32GB
- 存储空间:≥20GB(模型文件约8GB)
1.2 项目基础信息
| 项目 | 值 |
|---|---|
| 模型 | Qwen3-4B-Instruct-2507 |
| 模型路径 | /root/ai-models/Qwen/Qwen3-4B-Instruct-2507 |
| 访问地址 | http://localhost:7860 |
| WebUI | Gradio |
| 推理引擎 | Transformers |
| Conda 环境 | torch29 |
2. 基础服务部署
2.1 安装Conda环境
# 下载Miniconda安装包 wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh # 安装Miniconda bash Miniconda3-latest-Linux-x86_64.sh -b -p /opt/miniconda3 # 初始化Conda /opt/miniconda3/bin/conda init source ~/.bashrc # 创建torch29环境 conda create -n torch29 python=3.10 -y conda activate torch29 # 安装基础依赖 pip install torch==2.9.0+cu121 transformers==5.5.0 gradio accelerate2.2 配置Supervisor服务
创建Supervisor配置文件/etc/supervisor/conf.d/qwen3-4b-instruct.conf:
[program:qwen3-4b-instruct] command=/opt/miniconda3/envs/torch29/bin/python /root/Qwen3-4B-Instruct/webui.py directory=/root/Qwen3-4B-Instruct user=root autostart=true autorestart=true stopasgroup=true killasgroup=true stderr_logfile=/root/Qwen3-4B-Instruct/logs/webui.log stdout_logfile=/root/Qwen3-4B-Instruct/logs/webui.log environment=PYTHONUNBUFFERED="1"重载Supervisor配置:
supervisorctl reread supervisorctl update2.3 服务管理命令
# 查看服务状态 supervisorctl status qwen3-4b-instruct # 重启服务 supervisorctl restart qwen3-4b-instruct # 停止服务 supervisorctl stop qwen3-4b-instruct # 启动服务 supervisorctl start qwen3-4b-instruct # 查看实时日志 tail -f /root/Qwen3-4B-Instruct/logs/webui.log3. HTTPS反向代理配置
3.1 安装Nginx和SSL证书
# Ubuntu/Debian sudo apt update sudo apt install -y nginx certbot python3-certbot-nginx # CentOS/RHEL sudo yum install -y epel-release sudo yum install -y nginx certbot python3-certbot-nginx3.2 获取SSL证书
sudo certbot --nginx -d your-domain.com3.3 配置Nginx反向代理
编辑/etc/nginx/sites-available/qwen3-4b-instruct:
server { listen 443 ssl; server_name your-domain.com; ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem; location / { proxy_pass http://localhost:7860; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }启用配置并重启Nginx:
sudo ln -s /etc/nginx/sites-available/qwen3-4b-instruct /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl restart nginx4. 负载均衡配置
4.1 多实例部署
在多台服务器上重复上述部署步骤,确保每个实例都能独立运行。
4.2 Nginx负载均衡配置
编辑/etc/nginx/nginx.conf,在http块中添加:
upstream qwen3_backend { server server1_ip:7860; server server2_ip:7860; server server3_ip:7860; # 负载均衡策略 least_conn; # 最少连接数策略 } server { listen 443 ssl; server_name your-domain.com; ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem; location / { proxy_pass http://qwen3_backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # WebSocket支持 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }4.3 健康检查配置
upstream qwen3_backend { server server1_ip:7860 max_fails=3 fail_timeout=30s; server server2_ip:7860 max_fails=3 fail_timeout=30s; server server3_ip:7860 max_fails=3 fail_timeout=30s; # 健康检查 check interval=5000 rise=2 fall=3 timeout=3000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; }5. 常见问题解决
5.1 服务启动失败排查
- 检查日志:
cat /root/Qwen3-4B-Instruct/logs/webui.log- 常见错误及解决方案:
- GPU内存不足:
nvidia-smi --query-gpu=memory.used --format=csv关闭其他占用GPU的进程或升级显卡
- 端口冲突:
ss -tlnp | grep 7860修改webui.py中的端口号或终止占用进程
5.2 防火墙配置
# CentOS/RHEL firewall-cmd --add-port=7860/tcp --permanent firewall-cmd --reload # Ubuntu/Debian ufw allow 7860/tcp5.3 GPU监控
# 实时监控GPU使用情况 watch -n 1 nvidia-smi # 查看显存占用 nvidia-smi --query-gpu=memory.used --format=csv6. 总结
通过本教程,您已经完成了Qwen3-4B-Instruct模型的完整部署,包括:
- 基础环境配置和模型服务部署
- HTTPS反向代理设置,确保通信安全
- Nginx负载均衡配置,提高服务可用性和性能
- 常见问题排查方法,保障服务稳定运行
这套架构可以支持高并发访问,同时通过负载均衡实现故障自动转移,适合生产环境部署。
获取更多AI镜像
想探索更多AI镜像和应用场景?访问 CSDN星图镜像广场,提供丰富的预置镜像,覆盖大模型推理、图像生成、视频生成、模型微调等多个领域,支持一键部署。
