Serve static files with NGINX: configure paths, verify, and reload safely
NGINX selects a server block using the request's listen port and Host name, then selects a matching location to serve a file. For a static site, root maps a URL path to a filesystem path and index chooses the default file for a directory request. This guide gives a minimal setup and checks for serving static files on a Linux package installation. Reverse proxy configuration is a separate topic.
NGINX selects a server block using the request's listen port and Host name, then selects a matching location to serve a file. For a static site, root maps a URL path to a filesystem path and index chooses the default file for a directory request. This guide gives a minimal setup and checks for serving static files on a Linux package installation. Reverse proxy configuration is a separate topic.
How configuration maps a request to a file
NGINX configuration consists of directives. http contains HTTP settings, server defines a port and virtual host, and location defines behavior for URI paths. A server belongs inside http; a location belongs inside server. root appends the request URI to a directory to construct a file path. For example, root /srv/www; plus /assets/logo.svg maps to /srv/www/assets/logo.svg. A request for / looks for an index file such as index.html by default.
With multiple virtual hosts, NGINX first finds servers whose listen address and port match the connection, then compares the HTTP Host value with server_name. If no name matches or Host is missing, the port's default server handles the request. This is why a domain test must use a hostname that matches server_name. For URI matching, the longest prefix location wins; regular-expression locations can add ordering rules. Avoid complex regex locations for a beginner's single static site.
Minimal static-site configuration
This illustrative example serves files from /srv/www on localhost:8080. Do not overwrite a production configuration with it. Find where the distribution's nginx.conf includes snippets inside the http context, and add the server block there. Adjust paths and ports for your installation.
server {
listen 8080;
server_name localhost;
root /srv/www;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Create the document root and a file. NGINX worker processes need read access to files and traversal permission on parent directories. If a path contains symbolic links, check their targets and permissions too.
sudo install -d -o root -g root -m 0755 /srv/www
printf '%s\n' '<h1>It works</h1>' | sudo tee /srv/www/index.html >/dev/null
sudo nginx -ttry_files checks $uri, then $uri/, in order and returns 404 if neither exists. A single-page app (SPA) may need a fallback to /index.html for client-side routes, but a broad fallback can hide missing static assets by returning a success page. Decide the app's route policy and 404 behavior first; do not apply a fallback indiscriminately to API or download paths. Directory listing is not enabled by default, so do not rely on a public listing to conceal filenames.
Apply and verify
Confirm `nginx -t` passes without syntax errors. It also checks whether referenced files can be opened.
If NGINX is running, load the configuration with `sudo nginx -s reload`. NGINX starts new workers and gracefully shuts down old workers after they finish existing requests. If the command fails because the master PID or privileges differ, use the service manager and package instructions for the installation.
Run `curl -i http://localhost:8080/` and confirm a 200 response with `It works` in the body.
Run `curl -i http://localhost:8080/not-found` and confirm 404. Add a file and confirm its URL is then served.
When testing another virtual host, set the request Host header, for example: `curl -i -H 'Host: localhost' http://127.0.0.1:8080/`.
Port 8080 is used for local checks instead of privileged port 80. Before exposing the site, separately review the firewall, TLS certificate, public domain, distribution-specific service setup, and directory permissions. This configuration was not applied to or tested on an NGINX instance.
Diagnose and recover
If `nginx -t` reports a directive-context or semicolon error, inspect the file and context: `location` belongs inside `server`, and `server` inside `http`.
For `permission denied` or missing-file errors, check the actual path formed by `root` and the URI, worker read permissions, and traversal permissions on every parent directory.
If the wrong server responds, verify `listen` for the port, `server_name`, default-server selection, and Host header.
If an unexpected URL mapping occurs, compare the request URI with prefix and regex location priority. Temporarily simplify complex locations to reproduce with a plain prefix.
If behavior stays unchanged after reload, rerun `nginx -t` and inspect `nginx -T` to confirm which configuration is tested. `-T` prints the full configuration to standard output; review it for sensitive values before sharing.
Use error logs to find the cause and keep a copy of the previous configuration. If the new config fails its test, do not reload it; retain or restore the last known-good version. After a successful reload command, verify the new file and 404 behavior through HTTP responses.
Sources and scope
NGINX, [Beginner’s Guide: Serving Static Content](https://nginx.org/en/docs/beginners_guide.html) — contexts, `server`/`location`, `root`, path mapping, and static-file example
NGINX, [HTTP core module](https://nginx.org/en/docs/http/ngx_http_core_module.html) — `root` and `try_files`
NGINX, [Index module](https://nginx.org/en/docs/http/ngx_http_index_module.html) — default index file for directory requests
NGINX, [How nginx processes a request](https://nginx.org/en/docs/http/request_processing.html) — virtual and default server selection
NGINX, [Command-line parameters](https://nginx.org/en/docs/switches.html) — `-t`, `-T`, and `-s reload`
Scope: HTTP static-file serving with a typical Linux NGINX package. Configuration paths, service commands, and permissions vary by distribution. Docs were read directly on 2026-11-19. The example is illustrative and was not run or tested on a server.