Static Files
Static files are files that are sent as-is to the client, without backend processing.
Examples: HTML (.html), CSS (.css), JavaScript (.js), Images (.png, .jpg), Downloads (.zip, .pdf)
NGINX is extremely efficient at serving static content due to:
- Event-driven architecture
- Zero-copy (
sendfile) - Kernel-level optimizations
Basic Request Flow for Static Files
Client → NGINX → File System → Client
No application server is involved.
Core Directives Used for Static Files
| Directive | Purpose |
|---|---|
root | Defines base directory |
alias | Maps URI to different path |
index | Default file |
try_files | File existence check |
autoindex | Directory listing |
sendfile | Efficient file transfer |
expires | Client-side caching |
gzip | Compression |
Simple Static File Server Example
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.html;
}
- Request:
http://example.com/about.html - NGINX looks for:
/var/www/html/about.html - If found → served directly
- If not → 404
root Directive (Most Common)
Syntax
root path;
How Path Is Built
Full path = root + URI
location /images/ {
root /var/www;
}
Request:
/images/logo.png
File served:
/var/www/images/logo.png
alias Directive (Important Difference)
Syntax
alias path;
Example
location /images/ {
alias /data/pictures/;
}
Request:
/images/logo.png
File served:
/data/pictures/logo.png
Key Difference
| root | alias |
|---|---|
| Appends URI | Replaces URI |
| More common | Needs care |
Default Files with index
index index.html index.htm;
Example
Request: http://example.com/
NGINX tries:
/var/www/html/index.html/var/www/html/index.htm
File Existence Control with try_files
location / {
try_files $uri $uri/ =404;
}
- Checks if file exists
- Prevents unwanted proxying
- Improves security
Serving Assets from a Separate Directory
location /assets/ {
root /var/www;
expires 30d;
}
Request:
/assets/style.css
File:
/var/www/assets/style.css
Adds caching:
Cache-Control: max-age=2592000
Auto Directory Listing (autoindex)
location /downloads/ {
root /var/www;
autoindex on;
}
- Lists files if no index file exists
- Use carefully (security risk)
MIME Types & Content-Type
http {
include mime.types;
default_type application/octet-stream;
}
- Correct
Content-Typeheaders - Browser renders files properly
Performance Optimizations
sendfile:sendfile on;- Uses zero-copy file transfer.
- TCP Optimizations
tcp_nopush on;
tcp_nodelay on;
- Gzip Compression
gzip on;
gzip_types text/css application/javascript;
Static Files + Reverse Proxy (Hybrid Setup)
server {
listen 80;
location / {
root /var/www/html;
try_files $uri $uri/ @backend;
}
location @backend {
proxy_pass http://app_backend;
}
}
- Static files served directly
- Dynamic requests proxied
- Best performance pattern
Security Best Practices
Disable access to hidden files:
location ~ /\. {
deny all;
}
Prevent directory traversal:
disable_symlinks on;
Restrict file types:
location ~ \.(php|sh)$ {
deny all;
}
Real-World Production Example
server {
listen 80;
server_name example.com;
root /var/www/app/public;
index index.html;
location / {
try_files $uri $uri/ =404;
}
location /static/ {
expires 7d;
access_log off;
}
}
Common Mistakes
- Mixing
rootandaliasincorrectly - Forgetting trailing slash with
alias - Leaving
autoindexenabled - Not setting cache headers