• 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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    这里将原本的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"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    1. 修改跨域策略 (请勿应在生产)
    WEB_API_CORS_ALLOW_ORIGINS: '*'
    CONSOLE_CORS_ALLOW_ORIGINS: '*'
    COOKIE_HTTPONLY: 'true'
    COOKIE_SAMESITE: 'None'
    COOKIE_SECURE: 'true'
    
    • 1
    • 2
    • 3
    • 4
    • 5

    由于 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
    
    • 1
    • 2

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

  • 相关阅读:
    javascript算法之从会用到理解 - 数组反转
    LL(1)文法的核心原理
    大模型部署手记(2)baichuan2+Windows GPU
    自然语言处理(扩展学习1):Scheduled Sampling(计划采样)与Teacher forcing(教师强制)
    关于mac上如何U盘
    算法通关村第三关-青铜挑战数组专题
    密码学 数字签名
    Android Studio 下载地址
    Flink系列文档-(YY03)-Flink编程基础API
    淘宝商品详情API接口(H5端和APP端),淘宝详情页,商品属性接口,商品信息查询
  • 原文地址:https://blog.csdn.net/qq_21359467/article/details/133064367