Skip to content

serve static files with Nginx X-Accel-Redirect - #10440

Open
vortigont wants to merge 1 commit into
roundcube:masterfrom
vortigont:x_accel
Open

vortigont wants to merge 1 commit into
roundcube:masterfrom
vortigont:x_accel

Conversation

@vortigont

@vortigont vortigont commented Nov 25, 2026 •

Copy link
Copy Markdown

this PR introduces using (optional) Nginx's X-Accel-Redirect feature to static.php to offload static file's content delivery to webserver and reduce php execution footprint. This would drastically reduce statics loading times and adds possibility to use static gzip compression on server side for js/css.

static.php part:

  • check requested file/versioning/validity/security, etc...
  • request nginx to handle file content delivery

nginx part:

  • file read and data transfer part
  • handling for cache/etag headers
  • ranged downloading
  • content-encoding, i.e. gzip, br, etc...

Change is transparent for existing setups, it detects Nginx's configuration via supplied server vars and falls back to usual path for non-accelerated setups.

an example config for NGINX

    location ~ ^/static\.php(/.*)$ {
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $document_root/static.php;
        fastcgi_param SCRIPT_NAME /static.php;
        fastcgi_param PATH_INFO $1;
        fastcgi_param X-ACCEL_PREFIX /static/;      # var that enables X-Accel in static.php, value must match with X-Accel internal location's path
        fastcgi_pass unix:/tmp/your-fpm.sock;
    }

    # Nginx X-Accel location for static files
    location ^~ /static/ {
        internal;
        alias /usr/local/www/roundcube/;     # your RC root setup path

        # another safety block (optional)
        location ~ \.php$ { return 403; }

        gzip on;
        #gzip_static      on;                                  # opt-in, if you have pre-compressed files next to regular ones
        add_header X-Accel-Nginx "static";        # for debugging purposes, could be removed for prod setup's
        add_header Cache-Control "public";
        add_header X-Content-Type-Options "nosniff";
        expires 30d;
    }

Comment thread public_html/static.php Resolved
Comment thread public_html/static.php Resolved
`static.php` could (optionally) use Nginx's X-Accel-Redirect feature to
offload static file's content delivery to webserver and reduce php execution footprint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants