优惠发布
商家八卦

Nextcloud更新索引、伪静态、性能优化

一、直接上传或同步文件到服务器后,更新文件索引

#扫描所有用户的所有文件,并更新索引。

sudo -u www php occ files:scan --all

注意 PHP 和 OCC 文件路径,也可以直接写绝对路径:

sudo -u www /usr/local/php8.0/bin/php /home/wwwroot/www.example.com/occ files:scan --all

#只扫描指定用户或者指定文件夹,并更新索引。

列出所有用户:

sudo -u www php occ user:list

只扫描 test 用户的文件:

sudo -u www php occ files:scan test

扫描用户的指定目录:

sudo -u www php occ files:scan --path="/test/files/download"

二、Nextcloud 伪静态规则

gzip on;
gzip_vary on;
gzip_comp_level 4;
gzip_min_length 256;
gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;

# HTTP response headers borrowed from Nextcloud `.htaccess`
add_header Referrer-Policy                      "no-referrer"   always;
add_header X-Content-Type-Options               "nosniff"       always;
add_header X-Download-Options                   "noopen"        always;
add_header X-Frame-Options                      "SAMEORIGIN"    always;
add_header X-Permitted-Cross-Domain-Policies    "none"          always;
add_header X-Robots-Tag                         "none"          always;
add_header X-XSS-Protection                     "1; mode=block" always;

# Remove X-Powered-By, which is an information leak
fastcgi_hide_header X-Powered-By;

#原有的基础上添加 /index.php$request_uri;
index index.php index.html /index.php$request_uri;

# Rule borrowed from `.htaccess` to handle Microsoft DAV clients
location = / {
    if ( $http_user_agent ~ ^DavClnt ) {
        return 302 /remote.php/webdav/$is_args$args;
    }
}

location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
}

location ^~ /.well-known {
    # The rules in this block are an adaptation of the rules
    # in `.htaccess` that concern `/.well-known`.

    location = /.well-known/carddav { return 301 /remote.php/dav/; }
    location = /.well-known/caldav  { return 301 /remote.php/dav/; }

    location /.well-known/acme-challenge    { try_files $uri $uri/ =404; }
    location /.well-known/pki-validation    { try_files $uri $uri/ =404; }

    # Let Nextcloud's API for `/.well-known` URIs handle all other
    # requests by passing them to the front-end controller.
    return 301 /index.php$request_uri;
}

# Rules borrowed from `.htaccess` to hide certain paths from clients
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)                { return 404; }


location ~ \.php(?:$|/) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    set $path_info $fastcgi_path_info;

    try_files $fastcgi_script_name =404;

    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param HTTPS on;

    fastcgi_param modHeadersAvailable true;         # Avoid sending the security headers twice

    fastcgi_param front_controller_active true;     # Enable pretty urls
    fastcgi_pass unix:/tmp/php-cgi.sock;   #自行修改为对应路径
    fastcgi_intercept_errors on;
    fastcgi_request_buffering off;
}

location ~ \.(?:css|js|svg|gif|png|jpg|ico)$ {
    try_files $uri /index.php$request_uri;
    expires 6M;         # Cache-Control policy borrowed from `.htaccess`
    access_log off;     # Optional: Don't log access to assets
}

location ~ \.woff2?$ {
    try_files $uri /index.php$request_uri;
    expires 7d;         # Cache-Control policy borrowed from `.htaccess`
    access_log off;     # Optional: Don't log access to assets
}

# Rule borrowed from `.htaccess`
location /remote {
    return 301 /remote.php$request_uri;
}

location / {
    try_files $uri $uri/ /index.php$request_uri;
}

三:解决423 locked

#临时解决方法

进入维护模式:

sudo -u www php occ maintenance:mode --on

清空 oc_file_locks 表:

delete * from oc_file_locks;
或者
truncate table oc_file_locks;

退出维护模式:

sudo -u www php occ maintenance:mode --off

#彻底解决方法

参考官网:https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/caching_configuration.html?highlight=cache

为 Nextcloud 配置 Redis 缓存,在 config/config.php 文件中添加以下配置:

'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
  'host'      => '127.0.0.1',
  'port'      => 6379,
  'user'          => 'Redis用户名',
  'password'      => 'Redis密码',
  'dbindex'       => 0,
  'timeout'       => 1.5,
  'read_timeout'  => 1.5,
]

四:性能优化

#内存缓存优化

APCu(官方推荐):

安装PHP的APCu扩展,在 config/config.php 文件中添加以下配置:

'memcache.local' => '\OC\Memcache\APCu',

Redis(官方说多服务器可能导致问题,单机速度不如APCu):

'memcache.local' => '\OC\Memcache\Redis',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
  'host'      => '127.0.0.1',
  'port'      => 6379,
  'user'          => 'Redis用户名',
  'password'      => 'Redis密码',
  'dbindex'       => 0,
  'timeout'       => 1.5,
  'read_timeout'  => 1.5,
]

#PHP优化

upload_max_filesize 5G
post_max_size 5G
max_input_time 3600
max_execution_time 3600
memory_limit = 512M
output_buffering = 0
upload_tmp_dir = /dev/shm/client_body_temp

#NGINX优化

client_max_body_size 5G
fastcgi_read_timeout 3600
client_body_temp_path /dev/shm/client_body_temp

临时文件目录设置为内存时,推荐大内存服务器,务必配置 SWAP。

#如果 Nextcloud 位于 CDN 或者负载均衡后

有前端权限:建议关闭proxy_buffering或增加proxy_max_temp_file_size的值。

无前端权限:add_header X-Accel-Buffering no;

#高上传带宽环境中提高上传性能,调整 Nextcloud 端的块大小(默认为10M)

sudo -u www php occ config:app:set files max_chunk_size --value 20971520
未经允许不得转载:主机资讯-VPS商家前沿资讯 » Nextcloud更新索引、伪静态、性能优化