Dify配置https协议

Dify配置https协议访问

我们再本地开发时服务启动后登录系统时无法登录,查看浏览器控制台发现访问后端接口均提示 401。这是由于前端和后的域名/网址不一致,导致前端和服务端跨域。

通过官方文档说明解决服务端与 web 客户端跨域,服务端必须为 https。

1、服务端配置https网关

由于部署dify的web、api端使用的nginx做的代理网关,因此我们只需对nginx进行配置https协议即可。在配置之前我们需要准备 server.pemserver.key 两个格式的证书文件。

进入 /dify-0.3.22/docker/nginx/conf.d 文件中,创建 cacerts 文件将 server.pemserver.key 放入该目录中。编辑 default.conf 文件:(这里我增加了logs日志的打印,可以忽略)

复制代码
server {
	listen 443 ssl;
	server_name 域名;
	ssl on;
	ssl_certificate /etc/nginx/conf.d/cacerts/server.pem;
	ssl_certificate_key /etc/nginx/conf.d/cacerts/server.key;
	access_log /etc/nginx/conf.d/logs/access.log  main;
	error_log /etc/nginx/conf.d/logs/error.log  info;

    location /console/api {
      proxy_pass http://api:5001;
      include proxy.conf;
    }

    location /api {
      proxy_pass http://api:5001;
      include proxy.conf;
    }

    location /v1 {
      proxy_pass http://api:5001;
      include proxy.conf;
    }

    location / {
      proxy_pass http://web:3000;
      include proxy.conf;
    }
}

这里将原本的80端口改为443端口,增加了证书文件的配置。同步需要将docker容器映射的端口进行修改

2、修改服务端容器端口映射配置

进入/dify-0.3.22/docker目录下,编辑docker-compose.yaml文件,

  1. 修改映射端口,将80:80替换为443:443。例如:

    nginx:
    image: nginx:latest
    volumes:
    - ./nginx/nginx.conf:/etc/nginx/nginx.conf
    - ./nginx/proxy.conf:/etc/nginx/proxy.conf
    - ./nginx/conf.d:/etc/nginx/conf.d
    depends_on:
    - api
    - web
    ports:
    - "443:443"

  2. 修改跨域策略 (请勿应在生产)

    WEB_API_CORS_ALLOW_ORIGINS: ''
    CONSOLE_CORS_ALLOW_ORIGINS: '
    '
    COOKIE_HTTPONLY: 'true'
    COOKIE_SAMESITE: 'None'
    COOKIE_SECURE: 'true'

由于 SameSite=None 必须配合 Secure=true,因此服务端必须为 https 协议才能实现跨域访问。

3、本地web服务配置服务端API

修改web跟目录下的.env文件,这里必须配置域名,不能配置IP

复制代码
NEXT_PUBLIC_API_PREFIX=https://域名/console/api
NEXT_PUBLIC_PUBLIC_API_PREFIX=https://域名/api

注意:配置域名的地方需填写你项目中实际的域名

相关推荐
FPGA_Linuxer1 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 11 天前
传输层协议UDP
网络·网络协议·udp
霍格沃兹软件测试开发1 天前
快速掌握Dify+Chrome MCP:打造网页操控AI助手
人工智能·chrome·dify·mcp
ftpeak2 天前
从零开始使用 axum-server 构建 HTTP/HTTPS 服务
网络·http·https·rust·web·web app
hsjkdhs2 天前
网络编程之UDP广播与粘包问题
网络·网络协议·udp
00后程序员张2 天前
详细解析苹果iOS应用上架到App Store的完整步骤与指南
android·ios·小程序·https·uni-app·iphone·webview
yzx9910132 天前
接口协议全解析:从HTTP到gRPC,如何选择适合你的通信方案?
网络·人工智能·网络协议·flask·pygame
板鸭〈小号〉2 天前
UDP-Server(3)chat聊天室
网络·网络协议·udp
weixin_456904272 天前
使用HTTPS 服务在浏览器端使用摄像头的方式解析
网络协议·http·https
不会叫的狼2 天前
HTTPS + 域名 + 双向证书认证(下)
https