Adding HTTPS to a Website with Nginx
Plain HTTP traffic can be captured and read, and an intermediary can alter its contents. Chrome also marks sites without valid HTTPS as insecure. HTTPS protects the contents in transit and makes tampering detectable, so I wanted to enable it for my site.
The overall process is obtain an SSL certificate -> deploy it to Nginx -> set up an HTTP-to-HTTPS redirect (optional) -> configure Cloudflare (optional).
Obtain a certificate for your domain
HTTPS uses a certificate to verify the site as an encrypted connection is established. There are plenty of detailed explanations of HTTPS and SSL online; here I will focus on getting a certificate and configuring the site.
I used Let’s Encrypt, a free certificate authority. During the request, it checks that you control the domain for which you want a certificate.
First install Certbot with sudo apt-get install certbot. Certbot can also handle certificate renewals.
Run sudo certbot --manual --preferred-challenges dns certonly. After you agree to the prompt about recording your IP address, Certbot displays a DNS challenge like this:
1 | ------------------------------------------------------------------------------- |
Certbot pauses while you set up the challenge. Open your domain’s DNS management page and create a TXT record named _acme-challenge.yourdomain.xxxx, using the token shown above as its value.
Give the DNS change some time to propagate, then check for the token with dig -t txt _acme-challenge.yourdomain.xxxx. I ran this on Linux; my Windows 10 machine did not have the dig command.
Once the record is visible, return to Certbot on the server and press Enter. If verification succeeds, the files appear under /etc/letsencrypt/live/yourdomain.xxxx/. The two files I generally use are cert.pem, the certificate, and privkey.pem, the private key.
The files in live are links, much like Windows shortcuts. You can point Nginx at them directly. If you need to move the certificate, the underlying files are in /etc/letsencrypt/archive/yourdomain.xxxx/ and have slightly different names.
Deploy to Nginx
Next, tell Nginx where to find the certificate and which local service to proxy. Here is the configuration I used:
1 | server{ |
Save the configuration under /etc/nginx/conf.d with a name such as xxxx.conf. Check its syntax and reload Nginx:
1 | nginx -t //用于检查是否有语法错误 |
You should then be able to visit the site at an address such as https://abc.yourdomain.xxxx.
Force redirects to HTTPS
A browser request to the ordinary HTTP address uses port 80. To avoid typing https:// each time, add a server configuration that redirects http://abc.yourdomain.xxx to https://abc.yourdomain.xxx:
1 |
|
Again, run nginx -t to check the configuration, then nginx -s reload to apply it.
Configure Cloudflare
Cloudflare can put a CDN in front of the site. In the default SSL setup I encountered, the browser-to-CDN connection used HTTPS, but the CDN-to-origin connection used HTTP. For HTTPS on both legs, enable the cloud for the DNS record and set Cloudflare’s SSL mode to full(strict). The official documentation explains the options:
If the browser reports too many redirects, check whether the forced HTTPS redirect is enabled while the CDN is still connecting to the origin over HTTP.
That completes the HTTPS setup.
