Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Configure Nginx to Serve Static HTML Pages

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Install Nginx (if not already installed):
    sudo apt update && sudo apt install nginx (Debian/Ubuntu) or sudo yum install nginx (RHEL/CentOS).
  2. 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.
  3. Create a directory for your site:
    sudo mkdir -p /var/www/mysite
    Place your HTML files here. For example, copy index.html into this folder.
  4. Set permissions:
    sudo chown -R $USER:$USER /var/www/mysite (or use www-data if you prefer). Ensure the web server can read the files: sudo chmod -R 755 /var/www/mysite.
  5. Create a new Nginx server block:
    sudo nano /etc/nginx/sites-available/mysite
    Add the following configuration (replace example.com with your domain or IP):
    server {
        listen 80;
        server_name example.com;
        root /var/www/mysite;
        index index.html;
    }
  6. Enable the site:
    Create a symbolic link to the sites-enabled directory:
    sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
  7. Remove the default site (optional but recommended):
    sudo rm /etc/nginx/sites-enabled/default (or unlink it) to avoid conflicts.
  8. Test the configuration:
    sudo nginx -t – this checks for syntax errors. You should see “syntax is ok” and “test is successful”.
  9. Reload Nginx:
    sudo systemctl reload nginx (or sudo service nginx reload) to apply changes without downtime.
  10. Access your site:
    Open a browser and go to http://your-server-ip or http://example.com. You should see your index.html page.

Common Mistakes

  • Wrong file permissions: If Nginx runs as user www-data and files are owned by root with restrictive permissions, you’ll get a 403 error.
  • Missing index file: If index.html is 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 -t to 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 80 on Ubuntu).

Tips

  • Use try_files to handle clean URLs: add try_files $uri $uri/ =404; inside the server block to avoid exposing internal paths.
  • For HTTPS, use Let’s Encrypt with certbot after 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 -T to dump the full configuration for debugging.

Troubleshooting

  • 403 Forbidden: Check file permissions and that the root directory exists. Also ensure the index directive points to an existing file.
  • 404 Not Found: Verify the root path 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_pass directive.
  • Changes not taking effect: After editing config, always run sudo nginx -t and then sudo 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 status to 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.