keepcalculator

06 Numbers and Powers

Nginx Server Blocks on CentOS: Setup Guide

Card No. 06·55309 · Updated · Standard formula, shown below

This guide walks you through setting up Nginx server blocks (also known as virtual hosts) on CentOS. Server blocks allow you to host multiple websites on a single server, each with its own domain name, document root, and configuration. By the end, you’ll have a working multi-site setup, complete with proper directory permissions and a test to confirm everything is running.

What You’ll Need

  • A CentOS server (7, 8, or 9) with Nginx installed. If Nginx isn’t installed, run sudo yum install nginx (or sudo dnf install nginx on CentOS 8+).
  • Root or sudo access to the server.
  • At least one domain name (or subdomain) pointing to your server’s public IP address. For testing, you can use a dummy domain like example.com and modify your local hosts file.
  • A text editor (like vi, nano, or vim).

Step-by-Step

  1. Create the directory structure – For each site, create a directory under /var/www. For example, for example.com:
    sudo mkdir -p /var/www/example.com/html
    This creates a parent directory for the domain and an html subdirectory to hold the site files.
  2. Assign ownership – Set the directory owner to the Nginx user (usually nginx) and the group to nginx to avoid permission issues:
    sudo chown -R $USER:$USER /var/www/example.com/html
    Then set the correct permissions: sudo chmod -R 755 /var/www
  3. Create a sample page – Add a simple index.html to test later:
    echo '<h1>Hello from example.com!</h1>' | sudo tee /var/www/example.com/html/index.html
  4. Create the server block file – Nginx server blocks are stored in /etc/nginx/conf.d/ (or /etc/nginx/sites-available/ on some setups; we’ll use conf.d for simplicity). Create a new file:
    sudo vi /etc/nginx/conf.d/example.com.conf
  5. Write the server block configuration – Paste the following, adjusting the server_name and root paths to match your domain:
    server {
        listen 80;
        server_name example.com www.example.com;
    
        root /var/www/example.com/html;
        index index.html index.htm;
    
        location / {
            try_files $uri $uri/ =404;
        }
    }
  6. Test the configuration – Run sudo nginx -t to check for syntax errors. You should see syntax is ok and test is successful.
  7. Reload Nginx – Apply the changes without dropping connections:
    sudo systemctl reload nginx
  8. Repeat for additional sites – For a second domain, create another directory, another .conf file, and reload Nginx again.
  9. Test the site – Visit http://example.com in a browser (or use curl). If you’re using a dummy domain, add an entry to your local /etc/hosts file pointing the domain to your server’s IP.

Common Mistakes

  • Forgetting to reload Nginx – After creating or editing a server block, you must reload or restart Nginx for changes to take effect.
  • Wrong file permissions – If the nginx user can’t read the site files, you’ll get a 403 Forbidden error. Ensure the directory is executable and files are readable.
  • Duplicate server_name – If two server blocks have the same server_name, Nginx will use the first one, causing the second site to be unreachable.
  • Missing trailing semicolons – Nginx configuration is strict; a missing semicolon will cause a syntax error.
  • Using the default server block – The default server block in /etc/nginx/nginx.conf can interfere. It’s usually fine, but if you want to disable it, comment it out or remove it.

Tips

  • Use a consistent naming convention – Name your server block files after the domain (e.g., example.com.conf) to keep them organized.
  • Enable logging per site – Add access_log /var/log/nginx/example.com.access.log; and error_log /var/log/nginx/example.com.error.log; inside the server block to separate logs.
  • Set up SSL later – Once the basic server block works, you can add SSL with Certbot or manually. For now, HTTP is fine for testing.
  • Use try_files for clean URLs – The line try_files $uri $uri/ =404; is standard for static sites and helps with pretty URLs.
  • Backup your config – Before making major changes, copy your config file: sudo cp /etc/nginx/conf.d/example.com.conf /etc/nginx/conf.d/example.com.conf.bak

Troubleshooting

  • 403 Forbidden – This usually means the Nginx user can’t access the directory. Check permissions: sudo chmod -R 755 /var/www/example.com and ensure the directory owner is correct.
  • 404 Not Found – The root directive may be wrong, or the index.html file doesn’t exist. Verify the file path and that the file is present.
  • Server not responding – Check if Nginx is running: sudo systemctl status nginx. Also ensure the firewall allows HTTP (port 80): sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload.
  • Configuration test fails – Run sudo nginx -t and read the error message carefully. It will point to the exact line and file.
  • Wrong site showing – If you see the default Nginx page, your server block might not be loaded. Check that the file in conf.d has a .conf extension and that the server_name matches the domain you’re using.

Frequently Asked Questions

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

They are essentially the same concept. Apache calls them virtual hosts, while Nginx calls them server blocks. Both allow multiple websites on one server.

Can I use IP addresses instead of domain names?

Yes, but you can only have one server block per IP address unless you use different ports. For multiple sites on the same IP, you need domain names.

How do I set up a server block for a subdomain?

Just create a new server block with the subdomain as the server_name (e.g., blog.example.com) and point it to a different document root.

Do I need to restart Nginx or reload?

Reload (sudo systemctl reload nginx) is sufficient for configuration changes. Restart is only needed for major changes or if Nginx is not running.

Why is my site showing the default Nginx page?

This usually means your server block isn’t being used. Check that the server_name matches the domain exactly, and that the configuration file is in the correct directory and has the .conf extension.

Can I use a server block to redirect HTTP to HTTPS?

Yes, later when you set up SSL, you can create a server block that listens on port 80 and returns a 301 redirect to the HTTPS version. For now, focus on the basic setup.

Related

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

More cards in this drawer

The whole Numbers and Powers drawer →