How to Configure SSL for Multiple Domains in NGINX


configure ssl for multiple domains in nginx

When you run multiple websites, it is important to protect them all with SSL certificates. However, it can be costly to buy separate certificate for each domain. So let us take a look at how to configure SSL for multiple domains in NGINX, both with single as well as multiple certificates.

 

How to Configure SSL for Multiple Domains in NGINX

Here are the steps to configure SSL for multiple domains in NGINX.

Before you proceed, please ensure that you have enabled virtual hosts in NGINX. Here are the steps to do it.

Let’s say you want to host 2 websites www.domain1.com and www.domain2.com on same IP.

 

1. Create Virtual Host Files

First, we create separate virtual host files – one for each domain. You can add the config for both websites in the same config file if you want. But this way it is cleaner and easier to manage.


$ sudo vim /etc/nginx/sites-available/www.domain1.com
$ sudo vim /etc/nginx/sites-available/www.domain2.com

 

2. Add Virtual Host Configuration

Add the following lines of code in the 2 files

 

If you have a wildcard or multi-domain certificate

In this case we use the same SSL certificate details for both virtual hosts

#www.domain1.com


server {
 listen 80
 server_name www.domain1.com
 root /var/www/www.domain1.com/html; #also add a root dir here
  ssl on;
  ssl_certificate /var/www/ssl/certificate.crt;
  ssl_certificate_key /var/www/ssl/certificate.key;
} 

 

 

#www.domain2.com


server {
 listen 80
 server_name www.domain2.com
 root /var/www/www.domain2.com/html; #also add a root dir here
  ssl on;
  ssl_certificate /var/www/ssl/certificate.crt;
  ssl_certificate_key /var/www/ssl/certificate.key;
}

 

The key is to use different server_name and root values for each virtual host but use the same SSL certificate details for both.

If you have 2 different certificates

In this case, you use different SSL certificate details for both virtual hosts

#www.domain1.com


server {
 listen 80
 server_name www.domain1.com
 root /var/www/www.domain1.com/html; #also add a root dir here
  ssl on;
  ssl_certificate /var/www/ssl/certificate.crt;
  ssl_certificate_key /var/www/ssl/certificate.key;
} 

 

 

#www.domain2.com


server {
 listen 80
 server_name www.domain2.com
 root /var/www/www.domain2.com/html; #also add a root dir here
  ssl on;
  ssl_certificate /var/www/ssl/certificate2.crt;
  ssl_certificate_key /var/www/ssl/certificate2.key;
}

 

3. Enable the domains

Next, enable the 2 domains


$ sudo ln -s /etc/nginx/sites-available/www.domain1.com /etc/nginx/sites-enabled/www.domain1.com
$ sudo ln -s /etc/nginx/sites-available/www.domain2.com /etc/nginx/sites-enabled/www.domain2.com

 

 

4. Reload NGINX web server

Reload NGINX server to apply changes.

$ sudo service nginx reload

 

That’s it! NGINX will serve both your websites via SSL.

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!