HTTPS Servers
Definition
HTTPS encrypts traffic between the browser and Nginx. Enable SSL on the listen socket and point to a certificate file plus a private key.
Beginner pattern: obtain a certificate (for example Let’s Encrypt), set listen 443 ssl;, then reload. Keep HTTP on 80 for redirects later.
Config
Start with this shape. Adjust paths for your server.
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/nginx/certs/www.example.com.crt;
ssl_certificate_key /etc/nginx/certs/www.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
root /var/www/html;
}
}
The certificate is public. The key must stay private but readable by the Nginx master process.
Key Directives
| Name | Plain meaning |
|---|
listen 443 ssl | Accept TLS on port 443. |
ssl_certificate | Path to the server certificate (and chain). |
ssl_certificate_key | Path to the private key. |
ssl_protocols | Allowed TLS versions (modern default includes 1.2 and 1.3). |
ssl_session_cache | Shared cache to reuse sessions and cut handshake cost. |
Examples
Example 1 — Enable SSL on a Server
Turn on ssl for the listen socket and set certificate paths.
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
}
How It Works
- ssl on listen is the modern way (not a separate ssl on; directive).
- Paths can be absolute under /etc/nginx/certs.
- nginx -t then reload.
Example 2 — One Server for HTTP and HTTPS
Listen on both 80 and 443 in the same server block while you migrate.
server {
listen 80;
listen 443 ssl;
server_name www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
}
How It Works
- Same site answers HTTP and HTTPS.
- Later add a redirect from 80 to https://.
- Open port 443 in the firewall / security group.
Example 3 — Certificate Plus Chain File
Browsers may need intermediate certificates. Put the server cert first, then the chain.
# cat www.example.com.crt bundle.crt > www.example.com.chained.crt
ssl_certificate www.example.com.chained.crt;
ssl_certificate_key www.example.com.key;
How It Works
- Server cert first in the combined file.
- Wrong order can make Nginx fail to start.
- openssl s_client -connect host:443 shows the chain the server sends.
Frequently Asked Questions
Let's Encrypt (Certbot) is common on servers you control. On AWS you may terminate TLS at an ALB with ACM instead.
Server Name Indication lets the browser tell Nginx the hostname during the TLS handshake so the right certificate can be chosen on one IP.
Yes, but restrict file permissions. Only the certificate portion is sent to clients.
Often a missing intermediate in the chain file.
No. Get HTTP proxying working first, then add certificates.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful