HTTPS Servers

Beginner
6 min read
Updated: Nov 2026
3 examples
TLS

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.

Nginx
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

NamePlain meaning
listen 443 sslAccept TLS on port 443.
ssl_certificatePath to the server certificate (and chain).
ssl_certificate_keyPath to the private key.
ssl_protocolsAllowed TLS versions (modern default includes 1.2 and 1.3).
ssl_session_cacheShared 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.

Nginx
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

  1. ssl on listen is the modern way (not a separate ssl on; directive).
  2. Paths can be absolute under /etc/nginx/certs.
  3. 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.

Nginx
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

  1. Same site answers HTTP and HTTPS.
  2. Later add a redirect from 80 to https://.
  3. 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.

Nginx
# 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

  1. Server cert first in the combined file.
  2. Wrong order can make Nginx fail to start.
  3. 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.

Next: WebSocket proxying

Keep going through the beginner Nginx path.

WebSocket proxying →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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