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

Drupal nginx rewrite rules

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

Checked against official repo/sample Source: NGINX wiki recipe · Drupal · drupal.org · Web server requirements

Nginx · Drupal

Save as /etc/nginx/conf.d/example.com.conf · Apply: nginx -t && systemctl reload nginx
limit_req_zone $binary_remote_addr zone=example_com_login:10m rate=10r/m;

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;
    index index.php index.html;
    server_tokens off;
    client_max_body_size 50m;

    # 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;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 256;
    gzip_types text/plain text/css text/xml application/json application/javascript application/xml application/rss+xml application/atom+xml image/svg+xml;

    # Drupal hardening (adapted from nginxconfig.io)
    # Keep config, template and Composer files private
    location ~* \.(?:engine|inc|install|make|module|profile|po|sh|sql|theme|twig|tpl(?:\.php)?|xtmpl|yml|yaml)(?:~|\.sw[op]|\.bak|\.orig|\.save)?$|^/(?:composer\.(?:json|lock)|web\.config|package\.json|yarn\.lock)$ {
        deny all;
    }

    location ~* ^/sites/[^/]+/settings.*\.php$ {
        deny all;
    }

    # Private files go through Drupal's access checks
    location ^~ /system/files/ {
        try_files $uri /index.php?$query_string;
    }

    location ~ \..*/.*\.php$ {
        return 403;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

    location ~ ^/sites/[^/]+/files/.*\.php$ {
        deny all;
    }

    location ~ (^|/)\.(?!well-known) {
        return 403;
    }

    location / {
        try_files $uri /index.php?$query_string;   # Drupal >= 7
    }

    location ~ /vendor/.*\.php$ {
        deny all;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        try_files $fastcgi_script_name =404;    # CVE-2019-11043
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
    }

    location ~ ^/sites/.*/files/styles/ {
        try_files $uri @rewrite;
    }

    location @rewrite {
        rewrite ^ /index.php;
    }

    location = /user/login {
        # Login page: 10 per minute per IP, burst 5
        limit_req zone=example_com_login burst=5 nodelay;
        limit_req_status 429;
        try_files $uri /index.php?$query_string;
    }
}

Notes

  • root must point at the Drupal web dir (often /var/www/project/web with Composer). Security rules ship only as Apache .htaccess; the main ones are recreated above.

Apache .htaccess

# Drupal ships its own .htaccess (clean URLs plus security rules); keep the original file

Caddy equivalent

example.com {
    root * /var/www/example.com
    encode zstd gzip
    @blocked path_regexp ^/(sites/[^/]+/files/.*\.php|vendor/.*\.php|sites/.*/private/)
    respond @blocked 403
    php_fastcgi unix//run/php/php8.3-fpm.sock
    file_server
}

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

FAQ

Where do the Drupal 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.

Drupal 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 Drupal?

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

Anything else to watch for with Drupal?

root must point at the Drupal web dir (often /var/www/project/web with Composer). Security rules ship only as Apache .htaccess; the main ones are recreated above.

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