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(orsudo dnf install nginxon 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.comand modify your local hosts file. - A text editor (like
vi,nano, orvim).
Step-by-Step
- Create the directory structure – For each site, create a directory under
/var/www. For example, forexample.com:
sudo mkdir -p /var/www/example.com/html
This creates a parent directory for the domain and anhtmlsubdirectory to hold the site files. - Assign ownership – Set the directory owner to the Nginx user (usually
nginx) and the group tonginxto avoid permission issues:
sudo chown -R $USER:$USER /var/www/example.com/html
Then set the correct permissions:sudo chmod -R 755 /var/www - Create a sample page – Add a simple
index.htmlto test later:
echo '<h1>Hello from example.com!</h1>' | sudo tee /var/www/example.com/html/index.html - 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 useconf.dfor simplicity). Create a new file:
sudo vi /etc/nginx/conf.d/example.com.conf - Write the server block configuration – Paste the following, adjusting the
server_nameandrootpaths 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; } } - Test the configuration – Run
sudo nginx -tto check for syntax errors. You should seesyntax is okandtest is successful. - Reload Nginx – Apply the changes without dropping connections:
sudo systemctl reload nginx - Repeat for additional sites – For a second domain, create another directory, another
.conffile, and reload Nginx again. - Test the site – Visit
http://example.comin a browser (or usecurl). If you’re using a dummy domain, add an entry to your local/etc/hostsfile 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
nginxuser 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
serverblock in/etc/nginx/nginx.confcan 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;anderror_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_filesfor clean URLs – The linetry_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.comand ensure the directory owner is correct. - 404 Not Found – The
rootdirective may be wrong, or theindex.htmlfile 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 -tand 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.dhas a.confextension and that theserver_namematches 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
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It