Home Nginx Static content Nginx Static Content Definition Static content means files on disk — HTML, CSS, JS, images — that Nginx can send without calling your app. You point a location at a folder with root (or alias).
For a request /about.html with root /data/www;, Nginx looks for /data/www/about.html.
Config Start with this shape. Adjust paths for your server.
server {
listen 80;
server_name localhost;
location / {
root /data/www;
index index.html;
}
location /images/ {
root /data;
}
} Longer prefix locations win over shorter ones. /images/ beats / for URLs that start with /images/.
Key Directives Name Plain meaning rootFolder prefix joined with the request URI to find the file. aliasReplace the location prefix with a folder path (different from root). indexDefault file names when the URI ends with /. try_filesTry a list of files/URIs; useful for SPA fallbacks.
Examples 1. Serve an HTML Folder 2. Separate Folder for Images 3. Match Image Extensions with Regex Example 1 — Serve an HTML Folder Put index.html in /data/www and map location / to that root.
server {
listen 80;
location / {
root /data/www;
index index.html;
}
} How It Works Create /data/www/index.html with any text.Reload Nginx after saving the config.Open http://localhost/ to see the file.Example 2 — Separate Folder for Images Requests under /images/ read from /data/images on disk.
location /images/ {
root /data;
}
# /images/logo.png → /data/images/logo.png How It Works URI + root become the file path./images/ is a longer prefix than / so it wins for those URLs.Missing files return 404.Example 3 — Match Image Extensions with Regex A ~ location can catch .gif, .jpg, and .png anywhere in the URI.
location ~* \.(gif|jpg|jpeg|png)$ {
root /data/images;
} How It Works ~ means regex (case-sensitive); ~* is case-insensitive.Regex locations are checked after prefix locations.First regex match wins among regex locations.Frequently Asked Questions What is the difference between root and alias? root appends the full URI under the folder. alias replaces the location prefix with another path.
Why do I get 403 or 404? Check file permissions, the path spelled in root, and whether the file exists on disk.
Can Nginx host a React build? Yes. Point root at the build folder and often add try_files $uri /index.html for client-side routes.
Do I need Node for static files? No. Nginx alone can serve a static site.
How do I apply changes? Run nginx -t, then reload.
Next: Reverse proxy Keep going through the beginner Nginx path.
Reverse proxy → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion