nginx.ccWeb server troubleshooting desk
Home / Generator / Rewrites / Nextcloud

Nextcloud nginx rewrite rules

nginx rewrite rules for Nextcloud, generated for your domain, root, PHP-FPM and HTTPS as a full server block or snippet, with .htaccess versions and cited sources.

Checked against official docs Source: docs.nextcloud.com · NGINX configuration

Nginx · Nextcloud

Save as /etc/nginx/conf.d/example.com.conf · Apply: 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;
    # No OCSP stapling: Let's Encrypt shut down its OCSP service in 2025.

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

    # Security headers (server level only; a location with its own add_header must repeat the whole set)
    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 must be tailored per site; enable it once you are sure it does not block your own scripts:
    # add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'" always;
    add_header Strict-Transport-Security "max-age=63072000" always;

    # Core of the official “webroot install” config; see the source link for the full version (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;
    }

    # Hidden files (.git, .env, …)
    location ~ /\.(?!well-known) {
        deny all;
    }
}

Notes

  • The official config assumes TLS terminates here (HTTPS on). Set 'htaccess.RewriteBase' => '/' in config.php and run occ maintenance:update:htaccess to drop index.php from URLs.
  • To keep headers consistent, the asset block here omits the official per-location add_header lines; see the source for the full version.

Apache .htaccess

# Nextcloud ships its own .htaccess; refresh it with sudo -u www-data php occ maintenance:update:htaccess

Caddy equivalent

No concise Caddy equivalent; use nginx.

Every generated config passes the Linter; still run nginx -t on the server. Inner pages 404? See 404 & try_files.

FAQ

Where do the Nextcloud nginx rewrite rules go?

Inside the site's server {} block (usually /etc/nginx/conf.d/<domain>.conf, or the panel's rewrite box). Run nginx -t, then systemctl reload nginx.

Nextcloud inner pages still 404 after adding rules?

Check: 1) the rules are in the right server block (server_name matches); 2) reload succeeded; 3) root points at the entry directory; 4) pretty URLs are enabled in the app; 5) no control-panel rewrite file overrides it.

What about Apache for Nextcloud?

This page also shows the .htaccess version; Apache needs mod_rewrite and AllowOverride All.

Anything else to watch for with Nextcloud?

The official config assumes TLS terminates here (HTTPS on). Set 'htaccess.RewriteBase' => '/' in config.php and run occ maintenance:update:htaccess to drop index.php from URLs.

Sponsored slot (empty)
VPS / CDN recommendations may appear here later, labelled as affiliate links. There are no referral links today.