前言
本文是关于如何将部署的dify开放给公网使用的小教程,由于作者水平有限,可能并不是最佳方案,欢迎您在评论区批评指正!
写在部署前
如果想要在个人家庭环境部署(自己电脑,使用wifi或者手机流量)的dify向公网开放端口访问,比较难实现,因为连接自家的wifi获得的ip地址,是经过层层nat转换的,不是真正的公网ip,所以笔者选择的方式是使用云服务器(自带公网ip)进行部署
正式部署
-
购买云服务器,可以选择阿里云,现在注册个人账号,有300块钱3个月的免费额度,且免费给的服务器性能足够强大,阿里云试用地址
具体可以选下面这个
-
进去之后可以选择2核4G的EC2实例,预装应用选docker
-
等待实例创建完成,然后远程连接到实例,登陆的时候选密码登录,第一次可能登录不成功,会提示你到重置密码界面,重置完后在进行登录
-
接下来按照dify官方手册中docker安装教程, 进行dify安装, dify手册地址
开放端口给公网访问
Dify的docker镜像中包含了nginx,所以我们它作为我们的HTTP的代理
dify的docker-compose.yml文件中,nginx的具体配置如下:
nginx:image: nginx:latestrestart: alwaysvolumes:- ./nginx/nginx.conf.template:/etc/nginx/nginx.conf.template- ./nginx/proxy.conf.template:/etc/nginx/proxy.conf.template- ./nginx/https.conf.template:/etc/nginx/https.conf.template- ./nginx/conf.d:/etc/nginx/conf.d- ./nginx/docker-entrypoint.sh:/docker-entrypoint-mount.sh- ./nginx/ssl:/etc/ssl # cert dir (legacy)- ./volumes/certbot/conf/live:/etc/letsencrypt/live # cert dir (with certbot container)- ./volumes/certbot/conf:/etc/letsencrypt- ./volumes/certbot/www:/var/www/htmlentrypoint: [ 'sh', '-c', "cp /docker-entrypoint-mount.sh /docker-entrypoint.sh && sed -i 's/\r$$//' /docker-entrypoint.sh && chmod +x /docker-entrypoint.sh && /docker-entrypoint.sh" ]environment:NGINX_SERVER_NAME: ${NGINX_SERVER_NAME:-_}NGINX_HTTPS_ENABLED: ${NGINX_HTTPS_ENABLED:-false}NGINX_SSL_PORT: ${NGINX_SSL_PORT:-443}NGINX_PORT: ${NGINX_PORT:-80}# You're required to add your own SSL certificates/keys to the `./nginx/ssl` directory# and modify the env vars below in .env if HTTPS_ENABLED is true.NGINX_SSL_CERT_FILENAME: ${NGINX_SSL_CERT_FILENAME:-dify.crt}NGINX_SSL_CERT_KEY_FILENAME: ${NGINX_SSL_CERT_KEY_FILENAME:-dify.key}NGINX_SSL_PROTOCOLS: ${NGINX_SSL_PROTOCOLS:-TLSv1.1 TLSv1.2 TLSv1.3}NGINX_WORKER_PROCESSES: ${NGINX_WORKER_PROCESSES:-auto}NGINX_CLIENT_MAX_BODY_SIZE: ${NGINX_CLIENT_MAX_BODY_SIZE:-15M}NGINX_KEEPALIVE_TIMEOUT: ${NGINX_KEEPALIVE_TIMEOUT:-65}NGINX_PROXY_READ_TIMEOUT: ${NGINX_PROXY_READ_TIMEOUT:-3600s}NGINX_PROXY_SEND_TIMEOUT: ${NGINX_PROXY_SEND_TIMEOUT:-3600s}NGINX_ENABLE_CERTBOT_CHALLENGE: ${NGINX_ENABLE_CERTBOT_CHALLENGE:-false}CERTBOT_DOMAIN: ${CERTBOT_DOMAIN:-}depends_on:- api- webports:- '${EXPOSE_NGINX_PORT:-80}:${NGINX_PORT:-80}'- '${EXPOSE_NGINX_SSL_PORT:-443}:${NGINX_SSL_PORT:-443}'
想要简单使用 NGINX 作为我们的 HTTP反向代理服务器, 我们只要将NGINX_SERVER_NAME: ${NGINX_SERVER_NAME:-_}
修改为我们服务器的公网ip即可,又注意到它的值是由环境变量$NGINX_SERVER_NAME决定的(默认值为_, 而_表示匹配任何地址),所以我们只要修改.ENV文件中的对应值就可以了,即修改.ENV 文件中的NGINX_SERVER_NAME=<我们具体的公网IP>,例如:NGINX_SERVER_NAME=201.0.113.9
然后,我们直接运行下面的命令重启 Docker 容器,即可从浏览器访问我们的dify页面
docker compose down
docker compose up -d
这样就完成了简单的部署,当然,根据您的需求,还可以进一步调整服务端口、配置自定义域名以及启用 TLS 等高级功能
部署中遇到的疑问
笔者当时有疑问,为什么不直接修改NGINX的配置文件, 而是要修改docker-compose.yml和.env文件
我们从docker-compose.yml文件中,可以看到,NGINX的文件映射(宿主机到容器内)主要包括:
volumes:- ./nginx/nginx.conf.template:/etc/nginx/nginx.conf.template- ./nginx/proxy.conf.template:/etc/nginx/proxy.conf.template- ./nginx/https.conf.template:/etc/nginx/https.conf.template- ./nginx/conf.d:/etc/nginx/conf.d- ./nginx/docker-entrypoint.sh:/docker-entrypoint-mount.sh- ./nginx/ssl:/etc/ssl # cert dir (legacy)- ./volumes/certbot/conf/live:/etc/letsencrypt/live # cert dir (with certbot container)- ./volumes/certbot/conf:/etc/letsencrypt- ./volumes/certbot/www:/
我们此时打开宿主机中的./nginx/nginx.conf.template文件,发现文件的顶部写着
#Please do not directly edit this file. Instead, modify the .env variables related to NGINX configuration.