nginx.ccWeb 服务器排障台
首页 / 配置生成 / 伪静态 / Nextcloud

Nextcloud Nginx 伪静态规则

Nextcloud 的 Nginx 伪静态(URL 重写)规则,按你的域名、目录、PHP-FPM 和 HTTPS 生成完整 server 配置或片段,附 Apache .htaccess 写法,并注明规则来源。

已对照官方文档 来源: docs.nextcloud.com · NGINX configuration

Nginx · Nextcloud

建议保存为 /etc/nginx/conf.d/example.com.conf · 应用:nginx -t && systemctl reload nginx
server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    location ^~ /.well-known/acme-challenge/ {
        root /var/www/_letsencrypt;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    http2 on;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    # Mozilla Intermediate(Mozilla Server Side TLS 6.0)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_prefer_server_ciphers off;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off;
    # 不开 OCSP stapling:Let's Encrypt 已于 2025 年停止 OCSP 服务。

    root /var/www/example.com;
    server_tokens off;
    client_max_body_size 512M;

    # 安全头(只写在 server 层;location 里一旦写 add_header 就要整组重复)
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;
    add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
    # CSP 需要按站点调整,确认不会挡掉自己的脚本后再启用:
    # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
    add_header Strict-Transport-Security "max-age=63072000" always;

    # 摘自官方「webroot 安装」配置的核心部分;完整版(gzip、map、MIME)见来源链接
    client_body_timeout 300s;
    fastcgi_buffers 64 4K;
    fastcgi_hide_header X-Powered-By;
    index index.php index.html /index.php$request_uri;

    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 {
        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; }
        return 301 /index.php$request_uri;
    }

    location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)(?:$|/)  { return 404; }
    location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console)                { return 404; }

    location ~ \.php(?:$|/) {
        rewrite ^/(?!index|remote|public|cron|status|ocs\/v[12]|ocs-provider\/.+|core\/ajax\/update|updater\/.+|.+\/richdocumentscode(_arm64)?\/proxy) /index.php$request_uri;
        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;
        fastcgi_param front_controller_active true;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_intercept_errors on;
        fastcgi_request_buffering on;
        fastcgi_max_temp_file_size 0;
    }

    location ~ \.(?:css|js|mjs|svg|gif|ico|jpg|png|webp|wasm|tflite|map|ogg|flac|mp4|webm)$ {
        try_files $uri /index.php$request_uri;
        expires 6M;
        access_log off;
    }

    location ~ \.(otf|woff2?)$ {
        try_files $uri /index.php$request_uri;
        expires 7d;
        access_log off;
    }

    location /remote {
        return 301 /remote.php$request_uri;
    }

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

    # 隐藏文件(.git、.env 等)
    location ~ /\.(?!well-known) {
        deny all;
    }
}

说明

  • 官方配置假设 TLS 在本机终止(fastcgi_param HTTPS on)。config.php 里设置 'htaccess.RewriteBase' => '/' 后运行 occ maintenance:update:htaccess 才能去掉 index.php。
  • 这里为保持安全头统一,静态资源段没有重复官方的 add_header;需要官方完整写法请看来源链接。

Apache .htaccess

# Nextcloud 自带 .htaccess,运行 sudo -u www-data php occ maintenance:update:htaccess 更新

Caddy 写法

这个程序没有简洁的 Caddy 等价写法,建议用 Nginx。

生成的 Nginx 配置都通过了本站 配置体检;上线前请在服务器上 nginx -t。只有首页能开、内页 404 见 404 & try_files.

常见问题

Nextcloud 的 Nginx 伪静态规则放在哪里?

放进这个站点的 server {} 块里(通常是 /etc/nginx/conf.d/域名.conf 或宝塔面板「网站 → 设置 → 伪静态」)。改完执行 nginx -t,通过后 systemctl reload nginx。

配置了伪静态,Nextcloud 内页还是 404 怎么办?

依次检查:1) 规则是否在正确的 server 块里(server_name 匹配);2) 是否 reload 成功;3) root 是否指向程序入口目录;4) 程序后台是否已开启伪静态/固定链接;5) 宝塔等面板是否有另一份伪静态文件覆盖。

Nextcloud 用 Apache 怎么配?

本页同时给出了 .htaccess 写法,需要 Apache 启用 mod_rewrite 并允许 AllowOverride All。

Nextcloud 配置还有什么要注意的?

官方配置假设 TLS 在本机终止(fastcgi_param HTTPS on)。config.php 里设置 'htaccess.RewriteBase' => '/' 后运行 occ maintenance:update:htaccess 才能去掉 index.php。

推广位(待接入)
这里以后会放 VPS / CDN 推荐,并标明「推广链接」。目前没有任何返佣链接。