此笔记是dify docker版本在服务器上部署在子路径的方法 例如 www.xxx.com/dify 基于2025年9月30日的官方master版本代码部署成功 也集成了本地的mcp服务 可以后续分享集成的过程和效果
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
修改的文件清单
/usr/dify/dify/docker/docker-compose.yaml
/usr/dify/dify/docker/nginx/conf.d/default.conf.template
/usr/dify/dify/web/Dockerfile
/usr/dify/dify/web/next.config.js
新增的文件
/usr/dify/dify/docker/.env
/usr/dify/dify/web/.env
/usr/dify/dify是我服务器上dify项目的目录
必要条件:服务器安装docker 、docker compose 由于不可抗拒原因docker的正常使用需要科学上网 并设置
/etc/docker/daemon.json
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
{
"proxies": {
"http-proxy": "http://127.0.0.1:7890",
"https-proxy": "http://127.0.0.1:7890",
"no-proxy": "localhost,127.0.0.1,::1"
}
}
1.拉取官方的仓库代码
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
https://github.com/langgenius/dify.git
2.修改docker-compose.yaml文件
2.1修改web容器为本地 web的目录(代码因为会改nextjs的配置来重新编译容器)
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
image: dify/web:local
build:
context: ../web
args:
NEXT_PUBLIC_WEB_PREFIX: ${NEXT_PUBLIC_WEB_PREFIX:-/dify}
NEXT_PUBLIC_BASE_PATH: ${NEXT_PUBLIC_BASE_PATH:-/dify}
environment:
NEXT_PUBLIC_WEB_PREFIX: ${NEXT_PUBLIC_WEB_PREFIX:-/dify} # 新添加
NEXT_PUBLIC_BASE_PATH: ${NEXT_PUBLIC_BASE_PATH:-/dify} # 新添加
3.修改default.conf.template文件
改之前:
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
# Please do not directly edit this file. Instead, modify the .env variables related to NGINX configuration.
server {
listen ${NGINX_PORT};
server_name ${NGINX_SERVER_NAME};
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 /files {
proxy_pass http://api:5001;
include proxy.conf;
}
location /explore {
proxy_pass http://web:3000;
include proxy.conf;
}
location /e/ {
proxy_pass http://plugin_daemon:5002;
proxy_set_header Dify-Hook-Url $scheme://$host$request_uri;
include proxy.conf;
}
location / {
proxy_pass http://web:3000;
include proxy.conf;
}
location /mcp {
proxy_pass http://api:5001;
include proxy.conf;
}
# placeholder for acme challenge location
${ACME_CHALLENGE_LOCATION}
# placeholder for https config defined in https.conf.template
${HTTPS_CONFIG}
}
改之后
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
server {
listen ${NGINX_PORT};
server_name ${NGINX_SERVER_NAME};
# API endpoints - 保持不变
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 /files {
proxy_pass http://api:5001;
include proxy.conf;
}
location /mcp {
proxy_pass http://api:5001;
include proxy.conf;
}
location /e/ {
proxy_pass http://plugin_daemon:5002;
proxy_set_header Dify-Hook-Url $scheme://$host$request_uri;
include proxy.conf;
}
# /dify 路径转发到 web 服务
# Next.js 会处理 basePath
location /dify {
proxy_pass http://web:3000;
include proxy.conf;
proxy_set_header X-Forwarded-Prefix /dify;
proxy_set_header Host $host:$server_port;
}
# placeholder for acme challenge location
${ACME_CHALLENGE_LOCATION}
# placeholder for https config defined in https.conf.template
${HTTPS_CONFIG}
}
4.修改/web/Dockerfile
增加构建参数
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
# 添加构建参数
ARG NEXT_PUBLIC_BASE_PATH=/dify
ARG NEXT_PUBLIC_WEB_PREFIX=/dify
ENV NEXT_PUBLIC_BASE_PATH=${NEXT_PUBLIC_BASE_PATH}
ENV NEXT_PUBLIC_WEB_PREFIX=${NEXT_PUBLIC_WEB_PREFIX}
修改前
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
# base image
FROM node:22-alpine3.21 AS base
LABEL maintainer="takatost@gmail.com"
# if you located in China, you can use aliyun mirror to speed up
# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# if you located in China, you can use taobao registry to speed up
# RUN npm config set registry https://registry.npmmirror.com
RUN apk add --no-cache tzdata
RUN corepack enable
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NEXT_PUBLIC_BASE_PATH=
# install packages
FROM base AS packages
WORKDIR /app/web
COPY package.json .
COPY pnpm-lock.yaml .
# Use packageManager from package.json
RUN corepack install
RUN pnpm install --frozen-lockfile
# build resources
FROM base AS builder
WORKDIR /app/web
COPY --from=packages /app/web/ .
COPY . .
ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN pnpm build:docker
# production stage
FROM base AS production
ENV NODE_ENV=production
ENV EDITION=SELF_HOSTED
ENV DEPLOY_ENV=PRODUCTION
ENV CONSOLE_API_URL=http://127.0.0.1:5001
ENV APP_API_URL=http://127.0.0.1:5001
ENV MARKETPLACE_API_URL=https://marketplace.dify.ai
ENV MARKETPLACE_URL=https://marketplace.dify.ai
ENV PORT=3000
ENV NEXT_TELEMETRY_DISABLED=1
ENV PM2_INSTANCES=2
# set timezone
ENV TZ=UTC
RUN ln -s /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone
WORKDIR /app/web
COPY --from=builder /app/web/public ./public
COPY --from=builder /app/web/.next/standalone ./
COPY --from=builder /app/web/.next/static ./.next/static
COPY docker/entrypoint.sh ./entrypoint.sh
# global runtime packages
RUN pnpm add -g pm2 \
&& mkdir /.pm2 \
&& chown -R 1001:0 /.pm2 /app/web \
&& chmod -R g=u /.pm2 /app/web
ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA}
USER 1001
EXPOSE 3000
ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]
修改后:
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
# base image
FROM node:22-alpine3.21 AS base
LABEL maintainer="takatost@gmail.com"
# if you located in China, you can use aliyun mirror to speed up
# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# if you located in China, you can use taobao registry to speed up
# RUN npm config set registry https://registry.npmmirror.com
RUN apk add --no-cache tzdata
RUN corepack enable
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
ENV NEXT_PUBLIC_BASE_PATH=
# install packages
FROM base AS packages
WORKDIR /app/web
COPY package.json .
COPY pnpm-lock.yaml .
# Use packageManager from package.json
RUN corepack install
RUN pnpm install --frozen-lockfile
# build resources
FROM base AS builder
WORKDIR /app/web
COPY --from=packages /app/web/ .
COPY . .
# 添加构建参数
ARG NEXT_PUBLIC_BASE_PATH=/dify
ARG NEXT_PUBLIC_WEB_PREFIX=/dify
ENV NODE_OPTIONS="--max-old-space-size=4096"
RUN pnpm build:docker
# production stage
FROM base AS production
ENV NODE_ENV=production
ENV EDITION=SELF_HOSTED
ENV DEPLOY_ENV=PRODUCTION
ENV CONSOLE_API_URL=http://127.0.0.1:5001
ENV APP_API_URL=http://127.0.0.1:5001
ENV MARKETPLACE_API_URL=https://marketplace.dify.ai
ENV MARKETPLACE_URL=https://marketplace.dify.ai
ENV PORT=3000
ENV NEXT_TELEMETRY_DISABLED=1
ENV PM2_INSTANCES=2
ENV NEXT_PUBLIC_BASE_PATH=${NEXT_PUBLIC_BASE_PATH}
ENV NEXT_PUBLIC_WEB_PREFIX=${NEXT_PUBLIC_WEB_PREFIX}
# set timezone
ENV TZ=UTC
RUN ln -s /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone
WORKDIR /app/web
COPY --from=builder /app/web/public ./public
COPY --from=builder /app/web/.next/standalone ./
COPY --from=builder /app/web/.next/static ./.next/static
COPY docker/entrypoint.sh ./entrypoint.sh
# global runtime packages
RUN pnpm add -g pm2 \
&& mkdir /.pm2 \
&& chown -R 1001:0 /.pm2 /app/web \
&& chmod -R g=u /.pm2 /app/web
ARG COMMIT_SHA
ENV COMMIT_SHA=${COMMIT_SHA}
USER 1001
EXPOSE 3000
ENTRYPOINT ["/bin/sh", "./entrypoint.sh"]
5. 修改/web/next.config.js
增加
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
publicExcludes: ['!manifest.json'],
buildExcludes: [/manifest.json$/],
去除
locImageURLs 、remoteImageURLs 这两个在构建的时候报错 因为我们增加了环境变量 url构建会有问题
直接去除 在image 的配置里应用下面的配置即可
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
images: {
remotePatterns: [
{
protocol: 'http',
hostname: 'localhost',
pathname: '/**',
},
{
protocol: 'http',
hostname: '127.0.0.1',
pathname: '/**',
},
{
protocol: 'https',
hostname: '**',
pathname: '/**',
},
],
修改前:
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
const { codeInspectorPlugin } = require('code-inspector-plugin')
const withPWA = require('next-pwa')({
dest: 'public',
register: true,
skipWaiting: true,
disable: process.env.NODE_ENV === 'development',
fallbacks: {
document: '/_offline.html',
},
runtimeCaching: [
{
urlPattern: /^https://fonts.googleapis.com/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts',
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60 // 1 year
}
}
},
{
urlPattern: /^https://fonts.gstatic.com/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60 // 1 year
}
}
},
{
urlPattern: /.(?:png|jpg|jpeg|svg|gif|webp|avif)$/i,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 64,
maxAgeSeconds: 30 * 24 * 60 * 60 // 30 days
}
}
},
{
urlPattern: /.(?:js|css)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'static-resources',
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60 // 1 day
}
}
},
{
urlPattern: /^/api/.*/i,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
networkTimeoutSeconds: 10,
expiration: {
maxEntries: 16,
maxAgeSeconds: 60 * 60 // 1 hour
}
}
}
]
})
const withMDX = require('@next/mdx')({
extension: /.mdx?$/,
options: {
// If you use remark-gfm, you'll need to use next.config.mjs
// as the package is ESM only
// https://github.com/remarkjs/remark-gfm#install
remarkPlugins: [],
rehypePlugins: [],
// If you use `MDXProvider`, uncomment the following line.
// providerImportSource: "@mdx-js/react",
},
})
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
// the default url to prevent parse url error when running jest
const hasSetWebPrefix = process.env.NEXT_PUBLIC_WEB_PREFIX
const port = process.env.PORT || 3000
const locImageURLs = !hasSetWebPrefix ? [new URL(`http://localhost:${port}/**`), new URL(`http://127.0.0.1:${port}/**`)] : []
const remoteImageURLs = [hasSetWebPrefix ? new URL(`${process.env.NEXT_PUBLIC_WEB_PREFIX}/**`) : '', ...locImageURLs].filter(item => !!item)
/** @type {import('next').NextConfig} */
const nextConfig = {
basePath: process.env.NEXT_PUBLIC_BASE_PATH || '',
transpilePackages: ['echarts', 'zrender'],
turbopack: {
rules: codeInspectorPlugin({
bundler: 'turbopack'
})
},
productionBrowserSourceMaps: false, // enable browser source map generation during the production build
// Configure pageExtensions to include md and mdx
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
// https://nextjs.org/docs/messages/next-image-unconfigured-host
images: {
remotePatterns: remoteImageURLs.map(remoteImageURL => ({
protocol: remoteImageURL.protocol.replace(':', ''),
hostname: remoteImageURL.hostname,
port: remoteImageURL.port,
pathname: remoteImageURL.pathname,
search: '',
})),
},
experimental: {
optimizePackageImports: [
'@remixicon/react',
'@heroicons/react'
],
},
// fix all before production. Now it slow the develop speed.
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
dirs: ['app', 'bin', 'config', 'context', 'hooks', 'i18n', 'models', 'service', 'test', 'types', 'utils'],
},
typescript: {
// https://nextjs.org/docs/api-reference/next.config.js/ignoring-typescript-errors
ignoreBuildErrors: true,
},
reactStrictMode: true,
async redirects() {
return [
{
source: '/',
destination: '/apps',
permanent: false,
},
]
},
output: 'standalone',
}
module.exports = withPWA(withBundleAnalyzer(withMDX(nextConfig)))
修改后
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
const { codeInspectorPlugin } = require('code-inspector-plugin')
const withPWA = require('next-pwa')({
dest: 'public',
register: true,
skipWaiting: true,
disable: process.env.NODE_ENV === 'development',
publicExcludes: ['!manifest.json'],
buildExcludes: [/manifest.json$/],
fallbacks: {
document: '/_offline.html',
},
runtimeCaching: [
{
urlPattern: /^https://fonts.googleapis.com/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts',
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60 // 1 year
}
}
},
{
urlPattern: /^https://fonts.gstatic.com/.*/i,
handler: 'CacheFirst',
options: {
cacheName: 'google-fonts-webfonts',
expiration: {
maxEntries: 4,
maxAgeSeconds: 365 * 24 * 60 * 60 // 1 year
}
}
},
{
urlPattern: /.(?:png|jpg|jpeg|svg|gif|webp|avif)$/i,
handler: 'CacheFirst',
options: {
cacheName: 'images',
expiration: {
maxEntries: 64,
maxAgeSeconds: 30 * 24 * 60 * 60 // 30 days
}
}
},
{
urlPattern: /.(?:js|css)$/i,
handler: 'StaleWhileRevalidate',
options: {
cacheName: 'static-resources',
expiration: {
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60 // 1 day
}
}
},
{
urlPattern: /^/api/.*/i,
handler: 'NetworkFirst',
options: {
cacheName: 'api-cache',
networkTimeoutSeconds: 10,
expiration: {
maxEntries: 16,
maxAgeSeconds: 60 * 60 // 1 hour
}
}
}
]
})
const withMDX = require('@next/mdx')({
extension: /.mdx?$/,
options: {
// If you use remark-gfm, you'll need to use next.config.mjs
// as the package is ESM only
// https://github.com/remarkjs/remark-gfm#install
remarkPlugins: [],
rehypePlugins: [],
// If you use `MDXProvider`, uncomment the following line.
// providerImportSource: "@mdx-js/react",
},
})
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
// the default url to prevent parse url error when running jest
const hasSetWebPrefix = process.env.NEXT_PUBLIC_WEB_PREFIX
const port = process.env.PORT || 3000
// const locImageURLs = !hasSetWebPrefix ? [new URL(`http://localhost:${port}/**`), new URL(`http://127.0.0.1:${port}/**`)] : []
// const remoteImageURLs = [hasSetWebPrefix ? new URL(`${process.env.NEXT_PUBLIC_WEB_PREFIX}/**`) : '', ...locImageURLs].filter(item => !!item)
/** @type {import('next').NextConfig} */
const nextConfig = {
basePath: process.env.NEXT_PUBLIC_BASE_PATH || '',
transpilePackages: ['echarts', 'zrender'],
turbopack: {
rules: codeInspectorPlugin({
bundler: 'turbopack'
})
},
productionBrowserSourceMaps: false, // enable browser source map generation during the production build
// Configure pageExtensions to include md and mdx
pageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],
// https://nextjs.org/docs/messages/next-image-unconfigured-host
// images: {
// remotePatterns: remoteImageURLs.map(remoteImageURL => ({
// protocol: remoteImageURL.protocol.replace(':', ''),
// hostname: remoteImageURL.hostname,
// port: remoteImageURL.port,
// pathname: remoteImageURL.pathname,
// search: '',
// })),
// },
images: {
remotePatterns: [
{
protocol: 'http',
hostname: 'localhost',
pathname: '/**',
},
{
protocol: 'http',
hostname: '127.0.0.1',
pathname: '/**',
},
{
protocol: 'https',
hostname: '**',
pathname: '/**',
},
],
},
experimental: {
optimizePackageImports: [
'@remixicon/react',
'@heroicons/react'
],
},
// fix all before production. Now it slow the develop speed.
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
dirs: ['app', 'bin', 'config', 'context', 'hooks', 'i18n', 'models', 'service', 'test', 'types', 'utils'],
},
typescript: {
// https://nextjs.org/docs/api-reference/next.config.js/ignoring-typescript-errors
ignoreBuildErrors: true,
},
reactStrictMode: true,
async redirects() {
return [
{
source: '/',
destination: '/apps',
permanent: false,
},
]
},
output: 'standalone',
}
module.exports = withPWA(withBundleAnalyzer(withMDX(nextConfig)))
6.新增 /docker/.env 文件并修改关键参数 进入docker目录
cp .env.example .env
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
FILES_URL=https://你的域名/dify
NGINX_PORT=8080
NGINX_SSL_PORT=8443
EXPOSE_NGINX_PORT=8080
EXPOSE_NGINX_SSL_PORT=8443
7.新增 /web/.env 文件并修改关键参数 进入web目录
cp .env.example .env
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
NEXT_PUBLIC_BASE_PATH=/dify # 重点 之前怎么修改docker里面的都没用
8.宿主机nginx 反向代理设置
--javascripttypescriptshellbashsqljsonhtmlcssccppjavarubypythongorustmarkdown
# /dify 路径代理到 Docker 容器
location /dify {
proxy_pass http://localhost:8080/dify;
proxy_http_version 1.1;
# WebSocket 支持
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 代理头
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 缓冲设置
proxy_buffering off;
proxy_request_buffering off;
}
# API 路径也需要代理
location /api {
proxy_pass http://localhost:8080/api;
proxy_http_version 1.1;
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;
}
location /console/api {
proxy_pass http://localhost:8080/console/api;
proxy_http_version 1.1;
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;
}
location /v1 {
proxy_pass http://localhost:8080/v1;
proxy_http_version 1.1;
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;
}
location /dify/files {
proxy_pass http://localhost:8080/files;
proxy_http_version 1.1;
# 关键:传递正确的 Host 头
proxy_set_header Host $http_host; # 或者 $host:$server_port
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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
}
dify 启动!!!! 进入docker目录
docker compose down (如果启动了先down)
docker compose up -d
可能会需要 docker compose build --no-cache web(先单独构建一下web镜像)
!!!如果修改环境变量 一定要先down 再up restart环境变量变更不会生效
!!! files比较特殊 踩过坑 关键是.env中的FILE_URL 和宿主机中的nginx配置 不然你的dify应用图片预览就会出现问题