KAHIBARO
Discord Login Register

22.7. HTTPS

Why HTTPS Matters

HTTPS is HTTP over an encrypted connection. It protects the data that travels between a client and your backend server.

With plain HTTP, anyone on the network can:

HTTPS solves these problems by adding:

Rule: Every production backend that handles user data must use HTTPS, never HTTP.

Today, browsers mark non-HTTPS sites as “Not secure”, and many features (like service workers) only work over HTTPS.


HTTP vs HTTPS

From a backend developer’s point of view, HTTP and HTTPS look very similar.

AspectHTTPHTTPS
Default port80443
URL schemehttp://https://
EncryptionNoneUses TLS
Data visibilityPlain textEncrypted (cannot be read by observers)
CertificateNot requiredRequires X.509 certificate
Browser indicatorNo lock icon, “Not secure”Lock icon, sometimes “Secure”
PerformanceSlightly faster handshake onlySlight overhead, but usually negligible

Your backend code (for example a FastAPI app) usually does not change. The main differences are:

TLS: The Protocol Behind HTTPS

HTTPS uses TLS (Transport Layer Security) to secure connections. You might still encounter the older name SSL, but SSL is deprecated. Modern HTTPS uses TLS 1.2 or TLS 1.3.

The core goals of TLS:

  1. Encrypt all data sent between client and server.
  2. Verify identity of the server using certificates.
  3. Detect tampering of traffic.

At a high level, a TLS connection has two phases:

  1. Handshake
    Client and server:
    • Agree on a protocol version and cipher suite.
    • Authenticate the server using its certificate.
    • Securely establish shared secret keys.
  2. Secure data transfer
    All HTTP data is sent encrypted using the keys from the handshake.

As a backend beginner, you do not need to know every cryptographic detail, but you should understand:

How HTTPS Works Step by Step

Let us walk through what happens when a user opens https://api.example.com/users.

1. DNS and TCP Connection

  1. The browser resolves api.example.com to an IP using DNS.
  2. The browser opens a TCP connection to the server on port 443.

At this point, the connection is not yet encrypted.

2. TLS Handshake (Simplified)

  1. ClientHello
    The client sends:
    • Supported TLS versions
    • Supported cipher suites
    • Random value
  2. ServerHello
    The server replies with:
    • Chosen TLS version
    • Chosen cipher suite
    • Its own random value
  3. Server Certificate
    The server sends its certificate, which contains:
    • The server’s public key
    • The domain name it is valid for
    • Information about the issuing Certificate Authority (CA)
    • Validity period
  4. Certificate verification (client side)
    The browser checks:
    • The certificate is not expired.
    • The domain in the certificate matches the URL (for example api.example.com).
    • The certificate is signed by a trusted CA.

If anything fails, the user sees a big warning.

  1. Key exchange
    Client and server perform a key exchange algorithm (for example using elliptic curve Diffie-Hellman) to create a shared secret, even though they never send the secret directly.
  2. Session keys
    Both sides derive symmetric encryption keys from the shared secret. These keys are used to encrypt and decrypt HTTP data.
  3. Finished messages
    Each side sends an encrypted “Finished” message to confirm the handshake is done.

At the end of this process:

3. Encrypted HTTP Traffic

Once TLS is established, normal HTTP requests and responses flow inside the encrypted channel.

To your application framework (FastAPI, Django, etc.), it looks like a regular HTTP request. The web server or reverse proxy handles decryption.


Certificates and Certificate Authorities

TLS relies on certificates to prove identity. These are X.509 certificates in practice.

What Is an HTTPS Certificate?

A certificate is a digitally signed file that contains:

The server also has a private key associated with the certificate’s public key. The private key must remain secret.

Rule: Never commit private keys to Git or any code repository. Keep them secret and protected.

Certificate Authorities (CAs) and Trust

The browser must trust the certificate. It does this by:

This creates a chain of trust:

  1. Root CA certificate (trusted by OS / browser).
  2. Intermediate CA certificate.
  3. Your server certificate.

If any link is missing or invalid, the browser warns the user.


Types of Certificates

For a backend developer, the most important aspects are domain coverage and validation level.

By Domain Coverage

TypeDescriptionExample
Single-domainValid for exactly one domainapi.example.com
WildcardValid for a domain and all its first-level subdomains*.example.com covers api.example.com, shop.example.com
Multi-domain (SAN)Valid for several specific domainsapi.example.com, admin.example.com

For microservices, wildcard or multi-domain certificates are often used.

By Validation Level

TypeValidationTypical use
DV (Domain Validated)Only checks domain ownershipMost APIs and websites, including Let’s Encrypt
OV (Organization Validated)Verifies organization detailsBusiness sites that want extra assurance
EV (Extended Validation)Very strict verification, shows org name in some UIsHigh profile banking or finance sites

Modern practice: DV certificates are enough for almost all backends and public APIs.


Self-Signed vs CA-Signed Certificates

You can create certificates yourself or get them from a CA.

Self-Signed Certificates

A self-signed certificate is signed by its own private key. It is free and easy to generate, for example for testing.

Example (Linux, OpenSSL):

bash
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem \
  -days 365 -nodes -subj "/CN=localhost"

Problems:

Self-signed certificates are useful for:

CA-Signed Certificates

Certificates from a trusted CA, such as Let’s Encrypt, are recognized by browsers.

Advantages:

For public production APIs, you must use CA-signed certificates.


TLS Termination and Your Backend

In real deployments, your actual backend application (Uvicorn, Gunicorn, FastAPI app) usually does not handle TLS directly.

Instead, the setup looks like this:

text
Client (HTTPS)
   |
   v
Reverse Proxy (Nginx, Traefik, cloud load balancer)
   - Terminates TLS
   - Decrypts HTTPS to HTTP
   |
   v
Application Server (Uvicorn/Gunicorn, FastAPI)
   - Receives plain HTTP on localhost

This is called TLS termination at the reverse proxy.

Why do this?

Rule: Expose only the HTTPS endpoint to the public internet. The internal HTTP between proxy and app should not be publicly reachable.


Common HTTPS Configurations (Conceptual)

You will see detailed Nginx, Traefik, and certificate setup in other chapters. Here is what you need to conceptually understand.

1. Redirect HTTP to HTTPS

Always redirect http:// traffic to https:// so users and clients use secure connections.

Typical behavior:

This ensures:

2. HSTS (HTTP Strict Transport Security)

HSTS tells browsers to always use HTTPS for your domain.

The server sends a header like:

http
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

Meaning:

Be careful: once enabled with a long max-age, you cannot easily go back to plain HTTP.


Security Benefits and Limitations of HTTPS

What HTTPS Protects Against

What HTTPS Does Not Solve

HTTPS does not replace other security practices. It does not solve:

Rule: HTTPS is necessary but not sufficient. You still must implement all other web security best practices.


Performance Considerations

TLS used to be considered “heavy” but modern hardware and protocols make HTTPS overhead small.

Key points:

For almost all backend applications, the benefits of HTTPS far outweigh the performance cost.


Practical Tips for Backend Beginners

As you start deploying backend services, keep these in mind:

  1. Use HTTPS everywhere in production
    • APIs, admin panels, user-facing sites.
    • Avoid mixing HTTP and HTTPS on the same app.
  2. Redirect HTTP to HTTPS
    • Configure your reverse proxy or cloud load balancer.
    • Test that http://your-domain always becomes https://your-domain.
  3. Use Let’s Encrypt or another CA
    • Automate certificate issue and renewal.
    • Never let certificates expire unnoticed.
  4. Keep private keys secret
    • Do not commit them to Git.
    • Restrict filesystem permissions.
    • Consider using secret management tools, covered in security chapters.
  5. Check your configuration
    • Use tools like SSL Labs’ SSL Server Test to analyze your domain.
    • Aim to disable old protocols (like TLS 1.0, 1.1) and unsafe ciphers.
  6. Understand termination
    • Know which component in your stack terminates TLS.
    • Forward client IPs and protocol info (for example with X-Forwarded-For, X-Forwarded-Proto) so your app can log correctly.

Example: From HTTP to HTTPS in an API

Imagine you built a FastAPI application that runs on http://localhost:8000 in development.

For production:

  1. You deploy it behind Nginx.
  2. Nginx listens on port 443 with a valid certificate.
  3. Nginx forwards decrypted HTTP traffic to your app on http://127.0.0.1:8000.
  4. Nginx also listens on port 80 and redirects all HTTP traffic to HTTPS.

From the client’s point of view:

This separation lets you focus on application logic while still providing secure HTTPS to your users and API consumers.


By understanding HTTPS in this way, you will be ready to configure secure endpoints together with web servers and reverse proxies, and to recognize when your deployments are not properly protected.

Views: 7

Comments

Please login to add a comment.

Don't have an account? Register now!