Problem: Hosting Multiple Domains on One IP
Hosting several websites with different domain names on a single server IP address can be difficult. Nginx, a popular web server, offers a solution to this issue, but it needs the right setup to handle multiple domains.
Solution: Correct Nginx Configuration for Multiple Domains
Setting Up Server Blocks
Server blocks in Nginx help host multiple domains on one server. They let you set different configurations for each domain, including document roots and logging.
To create server blocks for each domain, define them in your Nginx configuration file. Each server block starts with the server
directive and contains settings for that domain.
Tip: Organize Your Configuration
Keep your Nginx configuration clean by creating separate files for each domain in the /etc/nginx/sites-available/ directory. Then, create symbolic links to these files in the /etc/nginx/sites-enabled/ directory to activate them.
Configuring the Listen Directive
The 'listen' directive in Nginx tells the server which IP address and port to use for incoming connections. When setting up multiple domains, use the 'listen' directive correctly.
The proper way to use the 'listen' directive is to specify only the port number, usually 80 for HTTP or 443 for HTTPS. For example:
listen 80;
Do not include the domain in the 'listen' directive. This is incorrect and can cause problems.
Using the Server_Name Directive
The 'server_name' directive is important for domain routing in Nginx. It tells Nginx which domain names to link with a server block.
To set up 'server_name' for each domain, include it in the server block. For example:
server_name www.domain1.example domain1.example;
You can add multiple domain names, with and without the 'www' prefix, to cover all versions of your domain name.
Example: Wildcard Subdomains
If you want to match all subdomains of a domain, you can use a wildcard in the server_name directive:
server_name *.domain1.example;
This will match any subdomain of domain1.example.
Step-by-Step Guide to Configure Nginx for Multiple Domains
Editing the Nginx Configuration File
To set up multiple domains in Nginx, locate and open the main Nginx configuration file. On most systems, this file is at /etc/nginx/nginx.conf
. It's better to create separate configuration files for each domain in the /etc/nginx/sites-available/
directory.
To create separate server blocks for each domain:
-
Open a new file in the sites-available directory:
sudo nano /etc/nginx/sites-available/domain1.conf
-
Add a server block for your first domain:
server { listen 80; server_name domain1.com www.domain1.com; # Other directives... }
-
Repeat this process for your second domain in a new file:
sudo nano /etc/nginx/sites-available/domain2.conf
-
Add the server block for the second domain:
server { listen 80; server_name domain2.com www.domain2.com; # Other directives... }
Tip: Symlink Configuration Files
After creating configuration files in the sites-available directory, create symbolic links to enable them:
sudo ln -s /etc/nginx/sites-available/domain1.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/domain2.conf /etc/nginx/sites-enabled/
This method allows you to easily enable or disable sites by creating or removing symlinks.
Setting Up Root Directories
Separate root directories for each domain keep the files for different websites apart, making management and security easier.
To specify root directories in the configuration:
-
Create directories for each domain:
sudo mkdir -p /var/www/domain1.com sudo mkdir -p /var/www/domain2.com
-
In each domain's server block, add the root directive:
server { listen 80; server_name domain1.com www.domain1.com; root /var/www/domain1.com; # Other directives... }
Configuring PHP Processing (if applicable)
If your websites use PHP, set up FastCGI for PHP processing:
-
In each server block, add a location block for PHP:
server { # Other directives... location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }
-
Make sure the FastCGI parameters are correct for each domain. The
fastcgi_param
directives should point to the correct document root:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
After making these changes, test your Nginx configuration and restart the Nginx service to apply the changes.