Apache · Configuration and operations

How do you serve static files efficiently with Apache?

Easystatic filesperformancecachingUpdated Oct 2026
Quiz this track

Short answer

Serve from DocumentRoot with tight Directory permissions, enable sendfile, set long cache headers on versioned assets and disable .htaccess. Offload high-volume assets to a CDN or edge cache.

Static serving should avoid per-request configuration work and unnecessary copies between kernel and user space. Apache can use sendfile and memory mapping, but the bigger wins are cache headers, compression policy and keeping the document root free of dynamic handlers.

apache
DocumentRoot "/var/www/static"

<Directory "/var/www/static">
  Options -Indexes +FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>

EnableSendfile On
EnableMMAP On
FileETag MTime Size

<Location "/assets">
  ExpiresActive On
  ExpiresDefault "access plus 1 year"
  Header set Cache-Control "public, immutable, max-age=31536000"
</Location>

What actually helps

  • Content-hashed filenames allow long max-age without stale content
  • Disable .htaccess and merge rules into the virtual host
  • Serve precompressed Brotli or gzip variants when available
  • Keep ETag and Last-Modified validators consistent across servers
  • Use a CDN for global traffic instead of scaling Apache for bytes
  • Avoid ExecCGI, Includes and other handlers in static directories
  • Verify that sendfile works on the filesystem; some network filesystems disable it

If several Apache nodes serve the same files, ETag values based on inode can differ between nodes and break conditional requests behind a load balancer. FileETag MTime Size is more portable than the default inode-based tag for replicated content.

Do not enable directory listings on a static root. Options -Indexes plus explicit denial of dotfiles prevents accidental exposure of backups and configuration files.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why use content-hashed filenames for static assets?
  2. 2What does EnableSendfile do?
  3. 3Why can ETag values differ across servers?
  4. 4When should static files move to a CDN?

Say it in the interview

I serve static content from a dedicated document root with AllowOverride None, no directory listing, and long cache headers on versioned assets. I enable sendfile where the filesystem supports it and use portable ETags across replicated nodes. For scale I move bytes to a CDN rather than adding Apache workers. Compression is precomputed where possible.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Apache mock interview and get scored feedback on depth, clarity and confidence.