ANALYSIS
- Docker
- HTTP
- Kubernetes
- PostgreSQL
- Posts
- Python
Dify 应用发布和部署
概述
Dify 提供多种发布和部署方式,让你可以将创建的 AI 应用快速推向用户。本文将详细介绍如何将应用发布为 Web 站点、API 工具,以及如何进行私有化部署。
发布方式概览
| 发布方式 | 复杂度 | 适用场景 | 特点 |
|---|---|---|---|
| Web 应用 | 低 | 快速分享、用户体验 | 几分钟即可发布 |
| API 工具 | 中 | 集成到现有系统 | 灵活的 API 接口 |
| MCP 服务器 | 高 | Claude Desktop 集成 | 新兴标准协议 |
| 私有化部署 | 高 | 企业级应用 | 数据隐私、完全控制 |
发布为 Web 应用
快速发布
步骤:
- 进入应用详情页
- 点击 发布
- 选择 发布更新
- 等待发布完成
访问地址:
PRTCL // PLAINTEXT
https://cloud.dify.ai/chatbot/xxxWeb 应用设置
基础设置:
PRTCL // YAML
Web 应用配置: enabled: true site_name: "我的应用" site_title: "AI 助手" description: "这是一个智能 AI 助手" icon: "https://example.com/icon.png"主题配置:
PRTCL // YAML
主题配置: primary_color: "#007AFF" background_color: "#F5F5F5" text_color: "#333333" font_family: "Inter, sans-serif" border_radius: 8高级设置:
PRTCL // YAML
高级配置: custom_domain: "app.example.com" ssl_enabled: true analytics: enabled: true provider: "google_analytics" tracking_id: "UA-XXXXXXXXX"功能配置
对话设置:
PRTCL // YAML
对话配置: opening_statement: "您好!有什么可以帮您的吗?" suggested_questions: - "如何使用产品?" - "产品有哪些功能?" - "常见问题有哪些?" voice_input: enabled: true language: "zh-CN" voice_output: enabled: true voice: "female"显示设置:
PRTCL // YAML
显示配置: show_branding: false # 隐藏 Dify 品牌信息 show_footer: true show_header: true show_share_button: true访问控制
公开访问:
PRTCL // YAML
访问控制: type: "public" authentication: false私有访问:
PRTCL // YAML
访问控制: type: "private" authentication: true allowed_users: - "user@example.com" - "user2@example.com"密码保护:
PRTCL // YAML
访问控制: type: "password" password: "your_password"API 集成
生成 API 凭据
步骤:
- 进入应用详情页
- 点击 API Access
- 点击 创建凭据
- 输入凭据名称
- 点击 创建
- 保存 API Key
API 文档
Dify 会自动生成完整的 API 文档,包括:
- 端点 URL
- 请求方法
- 请求参数
- 响应格式
- 示例代码
查看文档:
- 进入 API Access 页面
- 点击 查看文档
- 查看详细的 API 说明
文本生成 API
端点:POST https://api.dify.ai/v1/completion-messages
请求示例:
PRTCL // BASH
curl --location --request POST 'https://api.dify.ai/v1/completion-messages' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data-raw '{ "inputs": { "text": "Hello, how are you?" }, "response_mode": "streaming", "user": "abc-123" }'Python 示例:
PRTCL // PYTHON
import requestsimport json
url = "https://api.dify.ai/v1/completion-messages"headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json',}data = { "inputs": { "text": "Hello, how are you?" }, "response_mode": "streaming", "user": "abc-123"}response = requests.post(url, headers=headers, json=data)print(response.text)对话 API
端点:POST https://api.dify.ai/v1/chat-messages
请求示例:
PRTCL // BASH
curl --location --request POST 'https://api.dify.ai/v1/chat-messages' \ --header 'Authorization: Bearer YOUR_API_KEY' \ --header 'Content-Type: application/json' \ --data-raw '{ "inputs": {}, "query": "Hello", "response_mode": "streaming", "conversation_id": "", "user": "abc-123" }'Python 示例:
PRTCL // PYTHON
import requestsimport json
url = 'https://api.dify.ai/v1/chat-messages'headers = { 'Authorization': 'Bearer YOUR_API_KEY', 'Content-Type': 'application/json',}data = { "inputs": {}, "query": "Hello", "response_mode": "streaming", "conversation_id": "", "user": "abc-123"}response = requests.post(url, headers=headers, json=data)print(response.text)API 安全
最佳实践:
PRTCL // YAML
安全配置: credentials: development: api_key: "${DIFY_DEV_API_KEY}" testing: api_key: "${DIFY_TEST_API_KEY}" production: api_key: "${DIFY_PROD_API_KEY}"
security: rate_limit: 100 # 每小时 100 次 ip_whitelist: true allowed_ips: - "192.168.1.0/24" - "10.0.0.0/8"发布为 MCP 服务器
什么是 MCP
MCP(Model Context Protocol)是一种让 AI 工具之间相互通信的标准协议。通过 MCP,你可以将 Dify 应用集成到 Claude Desktop 等 AI 工具中。
发布步骤
步骤:
- 进入应用详情页
- 点击 发布
- 选择 发布为 MCP 服务器
- 配置 MCP 参数
- 点击 发布
MCP 配置
基础配置:
PRTCL // YAML
MCP 配置: enabled: true server_name: "my-dify-app" description: "我的 Dify 应用" version: "1.0.0"工具配置:
PRTCL // YAML
工具配置: tools: - name: "generate_content" description: "生成内容" input_schema: type: "object" properties: topic: type: "string" description: "主题" style: type: "string" description: "风格" required: - "topic"使用 MCP
在 Claude Desktop 中使用:
- 打开 Claude Desktop
- 进入设置
- 添加 MCP 服务器
- 输入 Dify 提供的 MCP URL
- 开始使用
私有化部署
部署方式
Dify 支持多种私有化部署方式:
| 方式 | 复杂度 | 适用场景 |
|---|---|---|
| Docker | 低 | 小规模部署 |
| Docker Compose | 中 | 中等规模部署 |
| Kubernetes | 高 | 大规模部署 |
| 源码部署 | 高 | 自定义部署 |
Docker 部署
步骤:
PRTCL // BASH
# 拉取镜像docker pull langgenius/dify:latest
# 运行容器docker run -d \ --name dify \ -p 80:80 \ -e DATABASE_URL="postgresql://user:password@host:5432/dify" \ -e REDIS_URL="redis://host:6379" \ langgenius/dify:latestDocker Compose 部署
docker-compose.yml:
PRTCL // YAML
version: '3.8'
services: dify-web: image: langgenius/dify-web:latest ports: - "3000:3000" depends_on: - dify-api environment: - API_URL=http://dify-api:5001
dify-api: image: langgenius/dify-api:latest ports: - "5001:5001" depends_on: - db - redis environment: - DATABASE_URL=postgresql://postgres:postgres@db:5432/dify - REDIS_URL=redis://redis:6379/0
db: image: postgres:15-alpine volumes: - ./data/postgres:/var/lib/postgresql/data environment: - POSTGRES_DB=dify - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres
redis: image: redis:7-alpine volumes: - ./data/redis:/data启动服务:
PRTCL // BASH
docker-compose up -dKubernetes 部署
deployment.yaml:
PRTCL // YAML
apiVersion: apps/v1kind: Deploymentmetadata: name: dify-apispec: replicas: 3 selector: matchLabels: app: dify-api template: metadata: labels: app: dify-api spec: containers: - name: dify-api image: langgenius/dify-api:latest ports: - containerPort: 5001 env: - name: DATABASE_URL value: "postgresql://postgres:postgres@db:5432/dify" - name: REDIS_URL value: "redis://redis:6379/0"启动服务:
PRTCL // BASH
kubectl apply -f deployment.yaml监控和维护
监控指标
关键指标:
PRTCL // YAML
监控指标: - name: "API 响应时间" target: "< 1s" - name: "成功率" target: "> 99%" - name: "并发用户数" target: "< 1000" - name: "错误率" target: "< 1%"日志管理
日志配置:
PRTCL // YAML
日志配置: level: "info" # debug, info, warn, error format: "json" outputs: - type: "console" enabled: true - type: "file" enabled: true path: "/var/log/dify" max_size: "100M" max_files: 10备份策略
备份配置:
PRTCL // YAML
备份配置: enabled: true schedule: "0 2 * * *" # 每天 2:00 retention: daily: 7 weekly: 4 monthly: 12 storage: type: "s3" bucket: "dify-backups" region: "us-east-1"安全配置
认证和授权
认证配置:
PRTCL // YAML
认证配置: enabled: true method: "jwt" # jwt, oauth2, saml jwt: secret: "${JWT_SECRET}" expiration: "24h" refresh_expiration: "7d"授权配置:
PRTCL // YAML
授权配置: enabled: true strategy: "rbac" # rbac, abac roles: - name: "admin" permissions: - "app:*" - "user:*" - name: "user" permissions: - "app:read" - "app:execute"数据加密
加密配置:
PRTCL // YAML
加密配置: enabled: true at_rest: algorithm: "AES-256" key_source: "env" in_transit: tls: enabled: true min_version: "1.2"访问控制
IP 白名单:
PRTCL // YAML
IP 白名单: enabled: true allowed_ips: - "192.168.1.0/24" - "10.0.0.0/8"性能优化
缓存策略
缓存配置:
PRTCL // YAML
缓存配置: enabled: true provider: "redis" # redis, memory ttl: 3600 # 1 小时 max_size: 10000负载均衡
负载均衡配置:
PRTCL // YAML
负载均衡: enabled: true strategy: "round_robin" # round_robin, least_connections health_check: enabled: true interval: 30 timeout: 10数据库优化
数据库配置:
PRTCL // YAML
数据库配置: pool: min: 5 max: 20 idle_timeout: 300 query_cache: enabled: true size: 1000故障排查
常见问题
发布失败:
PRTCL // BASH
# 检查日志docker logs dify-api
# 检查配置docker exec dify-api env | grep DATABASE_URLAPI 调用失败:
PRTCL // BASH
# 测试连接curl -I https://api.dify.ai/v1
# 检查 API 密钥echo $DIFY_API_KEY性能问题:
PRTCL // BASH
# 检查资源使用docker stats dify-api
# 检查数据库连接docker exec dify-api pg_isready调试工具
日志查看:
PRTCL // BASH
# 查看应用日志docker logs -f dify-api
# 查看错误日志docker logs dify-api | grep ERROR性能分析:
PRTCL // BASH
# 分析 API 响应时间curl -w "@curl-format.txt" -o /dev/null -s "https://api.dify.ai/v1/completion-messages"最佳实践
发布建议
- 分阶段发布:先发布到测试环境,再发布到生产环境
- 版本控制:使用版本号管理发布
- 灰度发布:逐步放量,监控效果
- 回滚准备:准备回滚方案
安全建议
- 保护密钥:使用环境变量存储密钥
- 定期轮换:定期更换 API 密钥
- 监控异常:监控异常访问和行为
- 权限最小化:最小权限原则
性能建议
- 使用缓存:合理使用缓存减少请求
- 优化数据库:优化查询和索引
- 负载均衡:使用负载均衡分散流量
- 监控指标:持续监控性能指标
资源链接
- 发布文档: https://docs.dify.ai/zh/use-dify/publish
- 部署文档: https://docs.dify.ai/zh/use-dify/deploy
- MCP 文档: https://docs.dify.ai/zh/use-dify/publish/mcp
最后更新: 2026-03-12 作者: EchoHaoRan
R P
Rhine Lab Pioneer Division
Auth_Verified: 2026.04.08
Auth_Verified: 2026.04.08
