How to Apply an SSL on Nginx

Introduction
In this article, we will guide you through the steps of installing an SSL certificate on the Nginx web server in the CentOS/Ubuntu Operation System.
 
We assume that you have Nginx installed on your server and uploaded certificate files (.crt file and .key file) to your Linux server.
 

Edit virtual host configuration file

1.1 Open the configuration file of virtual host

Open the configuration file of the virtual host. The path to the configuration file is usually as follows:
/etc/nginx/sites-available/your_domain_name.vhost
 

1.2 Add the related SSL certificate information

Add the following command lines to the end of the configuration file to update your SSL information:
 
server {
listen 443 ssl;
server_name ssl.xxx.ml;  # Your domain name
# The path of the .crt file of your SSL certificate
ssl_certificate /path/to/your_domain_name.crt;
# The path of the .key file of your SSL certificate
ssl_certificate_key /path/to/your_domain_name.key;
}
-
 
 

Restart Nginx service

# systemctl restart nginx
 

Test the HTTPs of your website

You can access your website via https://your_domain_name now.
 
 

Add redirection from HTTP to HTTPS

Edit the configuration file ‘vi /etc/nginx/sites-available/your_domain_name.vhost’ and add the following command line to the file, as shown in the screenshot.
 
rewrite ^(.*)$  https://$server_name$1 permanent;
 
 

Restart Nginx service

# systemctl restart nginx

Add Feedback