准备
创建文件目录结构
1
2
3
4
5
6
|
mkdir nextcloud-onlyoffice
cd nextcloud-onlyoffice
touch docker-compose.yml
touch nginx.conf
touch phi.ini
mkdir rabbitmq_data
|
最终目录结构
1
2
3
4
5
6
7
8
|
nextcloud-onlyoffice/
├── docker-compose.yml
├── nginx.conf
├── phi.ini
└── rabbitmq_data/
├── mnesia/
├── schemas/
└── ... (RabbitMQ自动生成的其他目录)
|
文件存储结构示例
1
2
3
4
5
6
7
8
9
10
11
12
|
_data/
├── admin/ # 用户目录
│ ├── cache/ # 缓存文件
│ ├── files/ # 用户上传的文件
│ │ ├── Documents/ # 文档文件夹
│ │ ├── Photos/ # 照片文件夹
│ │ └── ... # 其他文件
│ ├── files_trashbin/ # 回收站文件
│ └── ... # 其他用户数据
├── appdata_*/ # 应用数据
├── files_external/ # 外部存储挂载点
└── nextcloud.log # NextCloud 日志
|
docker-compose.yml 和 nginx.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
version: '3'
services:
app:
container_name: app-server
image: nextcloud:fpm
stdin_open: true
tty: true
restart: always
expose:
- '80'
- '9000'
networks:
- onlyoffice
volumes:
- app_data:/var/www/html
- ./php.ini:/usr/local/etc/php/conf.d/opcache.ini
- /etc/localtime:/etc/localtime:ro
environment:
- TRUSTED_DOMAINS=localhost,app-server,IP,nginx-server,域名 # IP、域名
- OVERWRITEHOST=xxxxxx # 域名
- OVERWRITEPROTOCOL=http
- OVERWRITECLIURL=http://xxxxxxx # 域名
- NEXTCLOUD_DEFAULT_PHONE_REGION=CN
- REDIS_HOST=redis
- MAINTENANCE_WINDOW_START=4 # 设置维护窗口(凌晨4点)
depends_on:
- db
- redis
onlyoffice-document-server:
container_name: onlyoffice-document-server
image: onlyoffice/documentserver:latest
stdin_open: true
tty: true
restart: always
networks:
- onlyoffice
expose:
- '80'
- '443'
volumes:
- document_data:/var/www/onlyoffice/Data
- document_log:/var/log/onlyoffice
- /etc/localtime:/etc/localtime:ro
ports:
- 2280:80
- 4423:443
environment:
- JWT_ENABLED=true
- JWT_SECRET=root123456
- AMQP_URI=amqp://guest:guest@rabbitmq
depends_on:
- rabbitmq
nginx:
container_name: nginx-server
image: nginx
stdin_open: true
tty: true
restart: always
ports:
- 2380:80
- 4433:443
networks:
- onlyoffice
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- app_data:/var/www/html
- /etc/localtime:/etc/localtime:ro
depends_on:
- app
db:
container_name: mariadb
image: mariadb:11.4.2
restart: always
volumes:
- mysql_data:/var/lib/mysql
- /etc/localtime:/etc/localtime:ro
environment:
- MYSQL_ROOT_PASSWORD=Mysql123
- MYSQL_PASSWORD=Msql123
- MYSQL_DATABASE=nextcloud
- MYSQL_USER=nextcloud
networks:
- onlyoffice
rabbitmq:
image: rabbitmq:3-management
container_name: rabbitmq
networks:
- onlyoffice
volumes:
- rabbitmq_data:/var/lib/rabbitmq
- /etc/localtime:/etc/localtime:ro
redis:
image: redis:alpine
container_name: redis
networks:
- onlyoffice
command: redis-server --save 60 1 --loglevel warning
volumes:
- /etc/localtime:/etc/localtime:ro
networks:
onlyoffice:
driver: 'bridge'
volumes:
document_data:
document_log:
app_data:
mysql_data:
rabbitmq_data:
redis_data:
|
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
user www-data;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
types {
text/javascript mjs;
application/javascript mjs;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
client_max_body_size 10G;
map $http_host $this_host {
"" $host;
default $http_host;
}
map $http_x_forwarded_proto $the_scheme {
default $http_x_forwarded_proto;
"" $scheme;
}
map $http_x_forwarded_host $the_host {
default $http_x_forwarded_host;
"" $this_host;
}
upstream backend {
server app-server:9000;
}
server {
listen 80;
server_name 域名; # 域名
# 安全头设置
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;" always;
add_header X-Content-Type-Options nosniff always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Robots-Tag "noindex, nofollow" always;
add_header X-Download-Options noopen always;
add_header X-Permitted-Cross-Domain-Policies none always;
add_header Referrer-Policy "no-referrer" always;
add_header X-Frame-Options "SAMEORIGIN" always;
root /var/www/html;
fastcgi_buffers 64 4K;
gzip off;
index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
# 修复 .well-known 路由问题
location = /.well-known/carddav {
return 301 /remote.php/dav/;
}
location = /.well-known/caldav {
return 301 /remote.php/dav/;
}
location /.well-known/ {
# 核心修复:直接传递请求给index.php处理
try_files $uri $uri/ /index.php$request_uri;
}
# 修复 ocm-provider 路由
location = /ocm-provider {
return 301 /index.php$request_uri;
}
location / {
rewrite ^/remote/(.*) /remote.php last;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ /index.php$uri?$args;
}
location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS off;
fastcgi_param modHeadersAvailable true;
fastcgi_pass backend;
fastcgi_intercept_errors on;
}
# OnlyOffice 代理设置
location ~* ^/ds-vpath/ {
rewrite /ds-vpath/(.*) /$1 break;
proxy_pass http://onlyoffice-document-server;
proxy_redirect off;
proxy_http_version 1.1;
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-Host $the_host;
proxy_set_header X-Forwarded-Proto $the_scheme;
}
# 静态资源缓存
location ~* \.(?:css|js|mjs)$ {
add_header Cache-Control "public, max-age=7200" always;
access_log off;
}
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
access_log off;
}
}
}
|
1
2
3
4
5
6
|
[opcache]
opcache.enable=1
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.memory_consumption=256
opcache.revalidate_freq=60
|
cpolar 内网穿透
(图1
)
(图2
)
1
2
|
# 启动命令
cplar http 2380 -hostname=域名
|
启动
等待所有容器启动后(约2-3分钟),配置 NextCloud
:
- 访问 http://IP:2380
- 创建管理员账户
- 安装完成后,进入
头像 → 应用 → Files → 搜索 OnlyOffice → 安装启用
- 启用后,进入
头像 → 个人设置 → ONLYOFFICE
- 文档服务器地址填写:http://IP:2280
- 密钥填写:root123456
- 保存设置
(图6
)
(图3
)
(图4
)
(图5
)
维护
设置维护时段(每天凌晨4点)
1
|
docker exec -it app-server php occ config:app:set core background_job_cron --value="0 4 * * *"
|
作用:
(1) 配置 Nextcloud
在每天凌晨4点执行系统维护任务
(2) 维护内容包括:
- 清理过期文件(回收站、版本历史)
- 更新文件索引和搜索数据库
- 生成文件预览图
- 执行系统优化和完整性检查
- 发送通知和报告
为什么重要:
- 这些任务会消耗大量CPU/内存资源
- 在非高峰时段运行可避免影响用户使用体验
- 解决您看到的警告:“服务器没有配置维护时段开始时间…”
执行mimetype迁移
1
|
docker exec -it app-server php occ maintenance:repair --include-expensive
|
作用:
(1) 执行文件类型识别系统的数据库迁移
(2) 迁移内容包括:
- 更新文件类型识别规则(支持新文件格式)
- 修复错误识别的文件类型
- 优化文件处理性能
- 重建文件元数据索引
为什么重要:
- 保
Nextcloud
能正确识别各种文件类型(如 .docx
, .pptx
等)
- 决您看到的警告:
"One or more mimetype migrations are available..."
- 大型实例上可能需要较长时间(几分钟到几小时)
启用cron服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
# 1. 在宿主机添加定时任务(每5分钟执行)
sudo crontab -e
# 2. 添加以下内容(使用绝对路径)
*/5 * * * * /usr/bin/docker exec app-server /usr/local/bin/php -f /var/www/html/cron.php > /dev/null 2>&1
# crontab 文件位置
# 用户级 crontab:/usr/lib/cron/tabs/<username>
# root 级 crontab:/private/var/at/tabs/root
# 但不需要直接编辑这些文件,始终使用 crontab 命令管理
# 查看已添加的 cron 任务
sudo crontab -l
# 编辑 cron 任务
sudo crontab -e
|
作用:
(1) 启动容器内的定时任务守护进程
(2) 定时任务功能:
- 每
5
分钟执行 cron.php
(根据您的 crontab
设置)
- 处理实时后台任务:
- 文件同步
- 邮件发送
- 用户通知
- 即时文件处理
为什么重要:
- 保持
Nextcloud
响应迅速
- 确保后台任务及时执行
- 替代低效的
AJAX
轮询方式
类比
想象 Nextcloud
是一个办公大楼:
(1) 维护时段 = 夜间清洁/维护
- 凌晨4点进行大扫除、设备检修
- 避免白天影响员工工作
(2) mimetype 迁移
= 文件归档系统升级
- 重新整理所有文件柜和标签系统
- 确保能快速准确找到任何文件类型
(3) cron 任务
= 快递/邮件分拣员
- 每5分钟处理一次新到的"包裹"(用户请求)
- 确保文件同步、通知发送等及时处理