ThisCalculator

Everyday

Nginx Server Blocks on CentOS: Setup Guide

Updated · Standard formula, shown below

This guide walks you through setting up Nginx server blocks (also called virtual hosts) on CentOS. Server blocks let you host multiple websites on a single server, each with its own domain name, document root, and configuration. By the end, you will have two fully functional sites running on the same Nginx instance.

What You’ll Need

  • A CentOS server (7, 8, or 9) with a non-root sudo user.
  • Nginx installed. If not, run sudo yum install nginx (or sudo dnf install nginx on CentOS 8+).
  • Two domain names (or test domains like example.com and test.com) pointing to your server’s public IP. For local testing, you can edit your hosts file.
  • Basic comfort with the command line and a text editor (nano or vi).

Step-by-Step

  1. Start Nginx and enable it on boot
    sudo systemctl start nginx
    sudo systemctl enable nginx
    Check it’s running: sudo systemctl status nginx
  2. Create the directory structure for each site
    For each domain, create a separate document root. For example:
    sudo mkdir -p /var/www/example.com/html
    sudo mkdir -p /var/www/test.com/html
    The -p flag creates parent directories as needed.
  3. Assign ownership and permissions
    sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chown -R $USER:$USER /var/www/test.com/html
    Then set read permissions for everyone: sudo chmod -R 755 /var/www
  4. Create a sample index page for each site
    nano /var/www/example.com/html/index.html
    Add a simple HTML page, e.g., <h1>Welcome to Example.com</h1>. Repeat for test.com with its own content.
  5. Create the server block configuration file
    Nginx on CentOS loads configs from /etc/nginx/conf.d/ by default. Create a file for each site:
    sudo nano /etc/nginx/conf.d/example.com.conf
    Paste this minimal configuration:

    server {
        listen 80;
        server_name example.com www.example.com;
    
        root /var/www/example.com/html;
        index index.html;
    }

    Save and exit. Repeat for test.com with its own server_name and root.

  6. Test the configuration
    sudo nginx -t
    You should see syntax is ok and test is successful. If not, fix the errors before continuing.
  7. Reload Nginx
    sudo systemctl reload nginx
  8. Verify the sites are working
    In a browser, visit http://example.com and http://test.com. You should see the respective index pages. If you’re testing locally, ensure your hosts file has entries like 127.0.0.1 example.com.

Common Mistakes

  • Forgetting to include the listen directive – Without it, Nginx may default to port 80 but with an unexpected server_name.
  • Using the same server_name in two blocks – Nginx will use the first one it finds, and the second site will never receive traffic.
  • Not setting the correct document root – If the root path doesn’t exist or is wrong, you’ll get a 404.
  • Missing the trailing slash in the root path – Always use /var/www/example.com/html not /var/www/example.com/html/ (though the trailing slash usually works, it’s inconsistent).
  • Forgetting to reload Nginx – Changes to config files only take effect after reload or restart.

Tips

  • Use descriptive filenames for your config files, like example.com.conf, so you know which site they belong to.
  • Keep a backup of your config before editing: sudo cp /etc/nginx/conf.d/example.com.conf /etc/nginx/conf.d/example.com.conf.bak
  • For SSL later, you can add a listen 443 ssl; block and include your certificate paths. Use Certbot to automate this.
  • If you want a default site for requests that don’t match any server_name, create a config file with server_name _;.
  • Test with curl -H "Host: example.com" http://localhost to see which site is being served without changing your hosts file.

Troubleshooting

Site shows the default Nginx page instead of your content
This usually means your server block isn’t being picked up. Check that the file in conf.d has a .conf extension and that the server_name matches the domain you’re visiting. Also ensure the root path contains an index file.

Configuration test fails
Run sudo nginx -t and read the error message. Common issues: missing semicolons, misspelled directives, or duplicate server_name. Fix the specific line mentioned.

Permission denied when accessing the site
Check that the directories and files are readable by Nginx. The user nginx needs at least r-x on directories and r-- on files. Run sudo chmod -R 755 /var/www to fix.

Site works locally but not externally
Ensure your firewall allows HTTP traffic: sudo firewall-cmd --permanent --add-service=http then sudo firewall-cmd --reload. Also verify your DNS points to the server’s public IP.

After adding SSL, the site stops working
If you added a 443 block, make sure you also have a listen 80 block for redirects, and that your certificate files exist and are readable. Test with sudo nginx -t and check the error log: sudo tail -f /var/log/nginx/error.log.

Frequently Asked Questions

What is the difference between a server block and a virtual host?

They are the same thing. Nginx calls them server blocks, while Apache uses the term virtual host. Both allow multiple websites on one server.

Can I use IP addresses instead of domain names?

Yes, you can set server_name to an IP address, but then you can only have one site per IP unless you use different ports or SNI with SSL.

How do I set up a default server block?

Create a config file with server_name _; and place it in conf.d. Nginx will use it for any request that doesn’t match another server_name.

Why does my second site show the first site’s content?

This usually means the second server block is missing or has the same server_name as the first. Check that the file exists and that the server_name is unique.

How can I test my server blocks without changing DNS?

Use curl with the Host header: curl -H "Host: example.com" http://127.0.0.1. This sends the hostname as if you were visiting that domain.

Do I need to restart Nginx or just reload?

Reload is sufficient for configuration changes. It gracefully restarts workers, avoiding downtime. Restart is only needed for system-level changes like new modules.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.