How do you configure Nginx to serve static files efficiently?
Learn to serve static files fast in Nginx using sendfile, gzip, caching headers and open_file_cache, with config examples and interview questions.
Expected Interview Answer
You serve static files efficiently in Nginx by pointing a location at the files with root or alias, enabling sendfile, tcp_nopush and gzip compression, and setting long expires/Cache-Control headers so browsers and proxies cache aggressively.
Nginx is extremely fast at static delivery because it can hand files straight to the kernel via sendfile, avoiding user-space copies, and batch packets with tcp_nopush. You match asset paths in a location block, compress text assets with gzip (or pre-compressed gzip_static), add far-future expires headers for fingerprinted files, and use open_file_cache to cache file descriptors and metadata. This keeps CPU and I/O low while maximizing throughput.
- Very high throughput with low CPU via sendfile
- Smaller transfers through gzip compression
- Fewer repeat requests with long-lived caching
- Reduced latency using open_file_cache
- Offloads static delivery from the app server
AI Mentor Explanation
Serving static files well is like a boundary rider fielding routine balls instantly instead of routing every ball to the captain. Nginx grabs the file and sends it straight from the shelf (sendfile), stamps it 'good for the whole season' so viewers cache it, and shrinks it before throwing (gzip) — keeping the main bowlers, your app servers, free for the tricky deliveries.
Step-by-Step Explanation
Step 1
Define a static location
Match your assets path with a location block and set root or alias to the files' directory.
Step 2
Enable sendfile and tcp_nopush
Turn on sendfile for zero-copy delivery and tcp_nopush to send file headers and data in full packets.
Step 3
Turn on compression
Enable gzip for text assets or serve pre-compressed files with gzip_static to save CPU.
Step 4
Set caching headers
Add long expires and Cache-Control for fingerprinted assets so browsers and CDNs cache them.
Step 5
Cache file metadata
Configure open_file_cache to cache file descriptors and stat results, reducing disk syscalls.
What Interviewer Expects
- Difference between root and alias
- Knowing what sendfile does
- Using gzip vs gzip_static
- Correct cache-control and expires usage
- Awareness of open_file_cache for performance
Common Mistakes
- Confusing root and alias path handling
- Serving static files through the app instead of Nginx
- Setting long cache on non-fingerprinted files
- Enabling gzip on already-compressed images
- Forgetting to enable sendfile
Best Answer (HR Friendly)
“You tell Nginx where the files live and let it send them straight from disk, compress text files to shrink downloads, and set headers so browsers keep copies. This makes the site fast and takes load off the main application.”
Code Example
http {
sendfile on;
tcp_nopush on;
gzip on;
gzip_types text/css application/javascript application/json;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
server {
listen 80;
root /var/www/html;
location /assets/ {
# fingerprinted files can cache for a year
expires 1y;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
}
}Follow-up Questions
- What is the difference between root and alias?
- How does sendfile improve performance?
- When should you use gzip_static over gzip?
- Why avoid gzipping JPEG or PNG files?
- How does open_file_cache help under high load?
MCQ Practice
1. Which directive enables zero-copy file transfer from disk to network in Nginx?
sendfile on lets the kernel copy file data directly to the socket, avoiding user-space buffers and boosting throughput.
2. For fingerprinted static assets, which caching approach is best?
Content-hashed assets never change under the same URL, so a far-future expires with immutable is ideal.
3. Which file type should generally NOT be gzipped?
JPEGs are already compressed, so gzip wastes CPU and yields little to no size reduction.
Flash Cards
What does sendfile do? — Enables zero-copy transfer of file data from disk straight to the socket, bypassing user-space buffers.
root vs alias? — root appends the full URI to the path; alias replaces the matched location prefix with the given path.
gzip vs gzip_static? — gzip compresses on the fly; gzip_static serves pre-compressed .gz files, saving CPU at request time.
Why open_file_cache? — It caches file descriptors and metadata so Nginx avoids repeated open/stat syscalls under load.