- Posted on
- • Web Development
Creating virtual hosts (server blocks) in Nginx
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Comprehensive Guide to Creating Virtual Hosts (Server Blocks) in Nginx for Web Developers
Whether you're a seasoned system administrator, a web developer, or just dipping your toes into the ocean of server management, understanding how to set up virtual hosts (also known as server blocks) in Nginx can greatly enhance your ability to host multiple websites on a single server. This guide aims to provide an in-depth look at creating and managing virtual hosts in Nginx, tailored specifically for Linux environments.
What is Nginx?
Nginx (pronounced “Engine-X”) is a high-performance HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server, known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
What are Virtual Hosts?
Virtual hosts or server blocks in Nginx allow you to run more than one website on a single machine. With virtual hosts, you can specify server settings for each site individually, which can be extremely useful for a host managing multiple domains, or even for developers who want to test their applications on their local machines before taking them live.
Prerequisites
Before you start setting up virtual hosts, you’ll need the following:
A running Linux server (Ubuntu, CentOS, etc.)
Nginx installed on your server (most Linux distributions have Nginx in their package repositories)
Root or sudo privileges
Domain names pointed at your server (DNS settings configured to point to your server’s IP)
Step 1: Install Nginx
If Nginx is not installed on your server, you can install it using your package manager. For Ubuntu and Debian systems, use:
sudo apt update
sudo apt install nginx
For CentOS, RHEL, and Fedora (using dnf
):
sudo dnf install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
For openSUSE (using zypper
):
sudo zypper install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Once installed, you can verify that Nginx is running by accessing your server’s IP address in a web browser. You should see a default Nginx landing page.
Step 2: Create Directory Structure
For each virtual host, you should have a separate directory that contains the website’s files. A common structure looks something like this:
sudo mkdir -p /var/www/example.com/html
sudo chmod -R 755 /var/www
Replace example.com
with your domain name.
Step 3: Create Index Page
Create a sample index.html
file in the directory you just created to test the virtual host:
sudo nano /var/www/example.com/html/index.html
Enter the following HTML code or something similar:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The Example.com server block is working!</h1>
</body>
</html>
Step 4: Configure Nginx Server Block
Next, you need to configure the virtual host (server block). Start by copying the default server block as a template:
sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com
Edit the new configuration file:
sudo nano /etc/nginx/sites-available/example.com
Adjust the file to match the following configuration, replacing example.com
with your domain name:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 5: Enable the Server Block
To enable the new server block, create a symlink in the sites-enabled
directory:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Step 6: Test Configuration
Before reloading Nginx, it’s important to test the configuration for syntax errors:
sudo nginx -t
If everything is OK, proceed to reload Nginx to apply the changes:
sudo systemctl reload nginx
Step 7: Final Testing
Now navigate to http://example.com
in your browser. You should see the welcome message you created earlier, indicating that your Nginx server block is configured and working correctly.
Conclusion
Setting up Nginx with multiple server blocks (virtual hosts) enables you to host multiple websites efficiently and securely on a single server. With the basic configuration complete, you can extend each server block's configurations to include SSL/TLS encryption, caching strategies, and more, depending on your specific requirements. Now that you understand the fundamentals, you’re well on your way to becoming proficient in managing web applications with Nginx. Happy hosting!
Further Reading
For further reading on Nginx and virtual hosts, please consider the following resources:
Nginx Official Documentation on Server Blocks: A comprehensive resource directly from the official Nginx documentation. Nginx Official Documentation
DigitalOcean's Guide to Nginx Server Blocks: A step-by-step tutorial on how to set up Nginx server blocks on Ubuntu. DigitalOcean Nginx Server Blocks
Linode's Nginx Configuration Guide: Details on configuring Nginx, including server blocks on a Linode server. Linode Guide to Nginx Configuration
Nginx Beginner’s Guide by MDN Web Docs: Offers a beginner-friendly explanation of Nginx, including how to manage virtual hosts. MDN Web Docs - Nginx Beginner’s Guide
Nginx Blog on Advanced Configuration: For those ready to dive deeper into customization and optimization of Nginx server blocks. Nginx Blog