Configure Nginx to Serve Static HTML Pages
In this article
This guide walks you through setting up Nginx to serve static HTML files—ideal for personal sites, landing pages, or simple web apps. You’ll learn how to create a basic configuration, place your files correctly, and avoid common pitfalls. No prior Nginx experience is needed, but basic comfort with the command line is assumed.
What You'll Need
- A Linux server (Ubuntu/Debian used in examples) or a local machine with Nginx installed.
- Root or sudo access to edit configuration files.
- A text editor (nano, vim, or similar).
- Your HTML files ready (e.g.,
index.html,about.html).
Step-by-Step
- Install Nginx (if not already installed):
sudo apt update && sudo apt install nginx(Debian/Ubuntu) orsudo yum install nginx(RHEL/CentOS). - Check the default configuration:
After installation, Nginx creates a default site at/etc/nginx/sites-available/default. You can modify it or create a new one. For a clean setup, create a new config file. - Create a directory for your site:
sudo mkdir -p /var/www/mysite
Place your HTML files here. For example, copyindex.htmlinto this folder. - Set permissions:
sudo chown -R $USER:$USER /var/www/mysite(or usewww-dataif you prefer). Ensure the web server can read the files:sudo chmod -R 755 /var/www/mysite. - Create a new Nginx server block:
sudo nano /etc/nginx/sites-available/mysite
Add the following configuration (replaceexample.comwith your domain or IP):
server { listen 80; server_name example.com; root /var/www/mysite; index index.html; } - Enable the site:
Create a symbolic link to the sites-enabled directory:
sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/ - Remove the default site (optional but recommended):
sudo rm /etc/nginx/sites-enabled/default(or unlink it) to avoid conflicts. - Test the configuration:
sudo nginx -t– this checks for syntax errors. You should see “syntax is ok” and “test is successful”. - Reload Nginx:
sudo systemctl reload nginx(orsudo service nginx reload) to apply changes without downtime. - Access your site:
Open a browser and go tohttp://your-server-iporhttp://example.com. You should see yourindex.htmlpage.
Common Mistakes
- Wrong file permissions: If Nginx runs as user
www-dataand files are owned by root with restrictive permissions, you’ll get a 403 error. - Missing index file: If
index.htmlis not present, Nginx will return a 403 or directory listing (if enabled). Ensure the file exists and is named correctly. - Syntax errors in config: Missing semicolons or braces will cause
nginx -tto fail. Always run the test before reloading. - Not removing default site: The default site may catch requests and show its own page, making it seem your changes didn’t apply.
- Firewall blocking port 80: If you can’t access the site externally, check that port 80 is open (e.g.,
sudo ufw allow 80on Ubuntu).
Tips
- Use
try_filesto handle clean URLs: addtry_files $uri $uri/ =404;inside the server block to avoid exposing internal paths. - For HTTPS, use Let’s Encrypt with
certbotafter the site is working over HTTP. - Keep your HTML files organized in subdirectories (e.g.,
/css,/js) and reference them with relative paths. - If you have multiple sites, create a separate server block for each and enable them via symlinks.
- Use
sudo nginx -Tto dump the full configuration for debugging.
Troubleshooting
- 403 Forbidden: Check file permissions and that the root directory exists. Also ensure the
indexdirective points to an existing file. - 404 Not Found: Verify the
rootpath is correct and that the requested file exists relative to that root. Check for typos in file names. - 502 Bad Gateway: This usually means Nginx can’t connect to a backend (like PHP-FPM). Since you’re serving static files, this is unlikely—but if you see it, check that you didn’t accidentally include a
proxy_passdirective. - Changes not taking effect: After editing config, always run
sudo nginx -tand thensudo systemctl reload nginx. If you still see old content, clear your browser cache or use incognito mode. - Port 80 already in use: If another service (like Apache) is using port 80, stop it or change Nginx to listen on another port (e.g.,
listen 8080;) for testing. - Can’t access from outside: Check your cloud provider’s security group or local firewall. On Ubuntu,
sudo ufw statusto see if Nginx is allowed.
Frequently Asked Questions
Do I need a domain name to use Nginx?
No, you can use your server’s IP address as the server_name. Just replace example.com with your IP.
Can I serve multiple HTML sites on one server?
Yes, create multiple server blocks with different server_name values (domain names) and point each to its own root directory. Ensure each has a unique listen directive if using different ports.
How do I add a custom 404 page?
Add error_page 404 /404.html; inside the server block, and place a 404.html file in your root directory.
What’s the difference between root and alias?
root appends the requested URI to the root path, while alias maps a specific location to a different path. For static sites, root is simpler and recommended.
How do I enable gzip compression?
Add gzip on; and gzip_types text/html text/css application/javascript; in the http block (usually in /etc/nginx/nginx.conf).
Can I use Nginx on Windows?
Yes, Nginx runs on Windows, but configuration paths differ (e.g., conf/nginx.conf). The steps are similar, but file permissions and symlinks work differently; it’s recommended to use Linux for production.