Nginx Static Content

Beginner
5 min read
Updated: Nov 2026
3 examples
root / location

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.

Nginx
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

NamePlain 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

Example 1 — Serve an HTML Folder

Put index.html in /data/www and map location / to that root.

Nginx
server {
    listen 80;
    location / {
        root /data/www;
        index index.html;
    }
}

How It Works

  1. Create /data/www/index.html with any text.
  2. Reload Nginx after saving the config.
  3. Open http://localhost/ to see the file.

Example 2 — Separate Folder for Images

Requests under /images/ read from /data/images on disk.

Nginx
location /images/ {
    root /data;
}
# /images/logo.png → /data/images/logo.png

How It Works

  1. URI + root become the file path.
  2. /images/ is a longer prefix than / so it wins for those URLs.
  3. Missing files return 404.

Example 3 — Match Image Extensions with Regex

A ~ location can catch .gif, .jpg, and .png anywhere in the URI.

Nginx
location ~* \.(gif|jpg|jpeg|png)$ {
    root /data/images;
}

How It Works

  1. ~ means regex (case-sensitive); ~* is case-insensitive.
  2. Regex locations are checked after prefix locations.
  3. First regex match wins among regex locations.

Frequently Asked Questions

root appends the full URI under the folder. alias replaces the location prefix with another path.
Check file permissions, the path spelled in root, and whether the file exists on disk.
Yes. Point root at the build folder and often add try_files $uri /index.html for client-side routes.
No. Nginx alone can serve a static site.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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