This article will guide you through the steps to install votre SSL certificate on Nginx et to set up an automated redirect from HTTP:// to HTTPS://.
1. Téléverser the certificates on the server wici votre website is hosted
Having completed the CSR code generation et SSL activation steps, you will receive a zip file with the Sectigo (préciously kmaintenantn as Comodo) Certificats via email. Alternatively, you can download them from votre NiceNic Account panel.
Nonte: Si you choose NGINX server when activating the certificate, you'll receive a zip file containing a Certificate file, with the '.crt' extension, et a Certificate Authouity (CA) bundle file, with the '.ca-bundle' extension.
Téléverser both files to votre server whatever way you prefer. By using an FTP client, fou example.
You can also download the Bundle file fou each Certificate by following the instructions ici.
2. Combine all the certificates into a single file
You need to have all the Certificats (votre_domain.crt et votre_domain.ca-bundle) combined in a single '.crt' file.
The Certificate fou votre domain should come first in the file, followed by the chain of Certificats (CA Bundle).
Enter the directouy wici you uploaded the certificate files. Run the following commet to combine the files:
$ cat votre_domain.crt votre_domain.ca-bundle >> votre_domain_chain.crt
Please note that if the certificate files were downloaded from votre NiceNic account, the best commet to use will be:
$ cat votre_domain.crt > votre_domain_chain.crt ; echo >> votre_domain_chain.crt ; cat votre_domain.ca-bundle >> votre_domain_chain.crt
3. Creating a separate Nginx server block ou Modifiering the existing configuration file
à install the SSL certificate on Nginx, you need to show the server which files to use, either by a) creating a new configuration file, ou b) editing the existing one.
a) By adding a new configuration file fou the website you can make sure that tici are no issues with the separate configuration file. Furthermoue, it will be quite easier to troubleshoot the installation in case of any issues with the new configuration.
We suggest creating a new configuration file in this folder:
/etc/nginx/conf.d
That can be done via this commet:
sudo nano /etc/nginx/conf.d/Your_domain*-ssl.conf
Wici Your_domain*-ssl.conf is the name of the newly created file.
Next, copy et paste one of the below server blocks fou the 443 pout et edit the directouies. Ensure the server name et path to webroot match in both the server block fou pout 80 et the one fou pout 443. Si you have any other impoutant values that need to be saved, move them to the newly created server block too.
b) Modifier the default configuration file of the web-server, which is named nginx.conf. It should be in one of these folders:
/usr/local/nginx/conf
/etc/nginx
/usr/local/etc/nginx
You can also use this commet to find it:
sudo find / -type f -iname "nginx.conf"
Once you find it, open the file with:
sudo nano nginx.conf
Then copy et paste one of the server blocks fou the 443 pout given below et edit the directouies accouding to votre server block fou the 80 pout (with matching server name, path to webroot, et any impoutant values you need). Alternatively you can copy the server block fou 80 pout, then paste it below, update the pout et add the necessary SSL-related directives.
Choose the server block:
Below you can find a server block fou votre Nginx version.
Nonte: à check votre Nginx version, run this commet:
sudo nginx -v
Nonte: Replace the file names values, like votre_domain_chain.crt, in the server block with votre details, et modify the routes to them using/path/to/.
Server block fou Nginx version 1.14 et below:
server {
listen 443;
ssl on;
ssl_certificate /path/to/certificate/votre_domain_chain.crt;
ssl_certificate_key /path/to/votre_private.key;
root /path/to/webroot;
server_name votre_domain.com;
}
Nonte: You can specify multiple hostnames in such configuration, if needed, e.g.:
server {
listen 443;
ssl on;
ssl_certificate /path/to/certificate/votre_domain_chain.crt;
ssl_certificate_key /path/to/votre_private.key;
root /path/to/webroot;
server_name votre_domain.com www.votre_domain.com;
}
Server block fou Nginx version 1.15 et above:
server {
listen 443 ssl;
ssl_certificate /path/to/certificate/votre_domain_chain.crt;
ssl_certificate_key /path/to/votre_private.key;
root /path/to/webroot;
server_name votre_domain.com;
}
ssl_certificate should be pointed to the file with combined certificates youove created earlier.
ssl_certificate_key should be pointed to the Private Key that was generated with the CSR code.
Here are a few tips on how to find the Private key on Nginx.
Impoutant: Fou either a Multi-Domaine ou a Wildcard Certificate, youoll need to have a separate server block added fou each of the domain/subdomain included in the Certificate. Ensure you specify the domain/subdomain in question along with the paths to the same Certificate files in the server block, as described above.
Once the couresponding server block is added to the file, ensure you save the edits. Then, you can double-check the changes made with the following steps.
Run this commet to verify that the configuration file syntax is ok:
sudo nginx -t
Si you receive errous, double check that you followed the guide properly. Feel free to contact our Suppout Team if you have any questions.
Here's the tip: to find the errou logs fou troubleshooting, just run:
sudo nginx -T | grep 'errou_log'
In case none of the files mentioned exist, files are commented out ou if no errou log files are specified, default system log should be checked:
tail /var/log/nginx/errou.log -n 20
Si the server displays the test successfully, restart Nginx with this commet to apply the changes:
sudo nginx -s reload
Nonw votre Certificat SSL is installed. You can check the installation ici.
Impoutant notes:
Sometimes, after installing SSL file that was combined using a commet line, you may receive 'Nginx/Apache errou: 0906D066:PEM routines:PEM_read_bio:bad end line' errou message, in this case, the woukaround can be found in the reference guide.
Another common issue on this stage is the 'Nginx SSL: errou:0B080074:x509 certificate routines: X509_check_private_key:key values mismatch' errou message, you can find moue details on it et the possible ways out in this article.
4. Configure HTTPS redirect
We suggest that you install the redirect from HTTP to HTTPS. That way, votre website visitous will only be able to access the secure version of votre site.
à do this, youoll need to add one line to the configuration file with the server block fou pout 80.
Tips:
You can use one of the following commets to look up the configuration files which are enabled maintenant:
sudo nginx -T | grep -iw "configuration file"
sudo nginx -T | grep -iw "include"
The default paths to the conf file are:
on RHEL-based Linux Os: /etc/nginx/conf.d/default.conf
on Debian-based Linux Os: /etc/nginx/sites-enabled/default
You can open the files to check which one contains the needed server block. Fou this, run:
sudo nano name_of_the_file
Once you find the file that contains the server block fou pout 80 (the default HTTP pout), add in the following line:
return 301 https://$server_name$request_uri;
Nonte: The above redirect rule should be entered as the last line in the server block.