Skip to content

SSL Termination in API Gateway — TLS Management and Certificate Handling

DodaTech Updated 2026-06-28 4 min read

In this tutorial, you will learn about SSL Termination in API Gateway. We cover key concepts, practical examples, and best practices to help you master this topic.

SSL termination is the Process of decrypting HTTPS traffic at the gateway so that requests are forwarded to backend services over plain HTTP, reducing the encryption burden on individual Microservices.

What You'll Learn

  • How SSL termination works and why gateways are ideal for it
  • Certificate management, re-encryption, and TLS best practices
  • Mutual TLS (mTLS) for gateway-to-backend security

Why It Matters

Encrypting every backend service's traffic individually wastes resources. Each microservice would need its own certificate, renewal process, and TLS termination logic, creating management overhead. The gateway terminates SSL once and either forwards over HTTP or re-encrypts with internal certificates.

Real-World Use

Durga Antivirus Pro's public API at api.dodatech.com accepts HTTPS connections. The gateway holds the wildcard certificate *.dodatech.com, terminates TLS, and forwards requests to internal scan services over plain HTTP on a private network. Internal traffic never leaves the data center, so HTTP is safe and faster.

flowchart LR
    Client["Client\nHTTPS"] -->|"TLS"| GW["Gateway\nSSL Termination"]
    GW -->|"HTTP"| Backend1["Service A"]
    GW -->|"HTTP"| Backend2["Service B"]
    GW -->|"HTTP"| Backend3["Service C"]
    style GW fill:#dbeafe,stroke:#2563eb

Simple SSL Termination with Python

from flask import Flask
import ssl

app = Flask(__name__)

@app.route("/api/status")
def status():
    return {"status": "healthy"}

if __name__ == "__main__":
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    context.load_cert_chain(
        certfile="/etc/ssl/certs/dodatech.crt",
        keyfile="/etc/ssl/private/dodatech.key"
    )
    app.run(host="0.0.0.0", port=443, ssl_context=context)

Re-Encryption (mTLS) to Backends

For zero-trust networks, the gateway re-encrypts traffic to backends using mutual TLS:

import requests

def forward_request(method, url, data=None, headers=None):
    resp = requests.request(
        method=method,
        url=url,
        data=data,
        headers=headers,
        cert=("/etc/gateway/client.crt", "/etc/gateway/client.key"),
        verify="/etc/gateway/ca.crt"
    )
    return resp

The gateway presents its client certificate, and the backend verifies it against the CA. This ensures only the authenticated gateway can communicate with backends.

Certificate Auto-Renewal with Let's Encrypt

import subprocess

def renew_certificate(domain):
    result = subprocess.run([
        "certbot", "renew", "--cert-name", domain,
        "--deploy-hook", "systemctl reload nginx"
    ], capture_output=True, text=True)
    return result.returncode == 0

Automate this with a cron job or systemd timer to check renewal daily.

Common Mistakes

1. Using Self-Signed Certificates in Production

Self-signed certificates trigger browser warnings and break API clients. Use Let's Encrypt or a commercial CA for production.

2. Not Redirecting HTTP to HTTPS

Clients may accidentally use HTTP. The gateway should redirect all HTTP traffic to HTTPS with a 301 redirect.

3. Forwarding Plain HTTP Over the Internet

If the gateway and backend are in different data centers, re-encrypt traffic. Forwarding HTTP over the internet exposes data in transit.

4. Not Pinning Certificate Authorities

For mTLS, explicitly define which CAs are trusted for client and server certificates. Default trust stores may be too permissive.

5. Ignoring TLS Version and Cipher Restrictions

Supporting TLS 1.0 or weak ciphers creates vulnerabilities. Restrict to TLS 1.2+ with strong ciphers.

Practice Questions

  1. What does SSL termination mean and why is it beneficial?
  2. What is the difference between SSL termination and SSL passthrough?
  3. When should you re-encrypt traffic between the gateway and backends?
  4. Why should HTTP traffic be redirected to HTTPS?
  5. What is mutual TLS and when would you use it?

Answers:

  1. SSL termination decrypts HTTPS at the gateway. Benefits include centralized certificate management, reduced backend complexity, and offloading CPU-intensive decryption.
  2. SSL termination decrypts at the gateway; SSL passthrough forwards the encrypted connection directly to the backend without decryption.
  3. Re-encrypt when the gateway-to-backend network traverses untrusted networks or when Compliance requires end-to-end encryption.
  4. HTTP redirects ensure all traffic is encrypted. Search engines penalize sites with mixed content, and users expect secure connections.
  5. mTLS requires both the client and server to present certificates. It is used for zero-trust networks where every connection must authenticate both sides.

Challenge: Configure a gateway that terminates TLS with a Let's Encrypt certificate, re-encrypts to backends using mTLS with a private CA, and redirects all HTTP to HTTPS.

FAQ

Does SSL termination violate end-to-end encryption principles?

: Yes, the gateway can see plaintext. For true end-to-end encryption, use SSL passthrough or let the backend terminate TLS.

How do you handle multiple domains with different certificates?

: Use Server Name Indication (SNI) so the gateway serves the correct certificate based on the domain name in the TLS handshake.

What is OCSP stapling and why is it useful?

: OCSP stapling lets the gateway prove certificate validity without the client contacting the CA directly, improving performance and privacy.

Can the gateway support HTTP/2 and HTTP/3?

: Yes. Modern gateways like Envoy, Nginx, and Caddy support HTTP/2 and HTTP/3 (QUIC) for improved performance.

How long should TLS session resumption be cached?

: Typically 1-4 hours. Longer caches improve performance but increase the risk if a session key is compromised.

Mini Project

Create a Python gateway that terminates TLS on port 443, redirects HTTP on port 80 to HTTPS, and forwards requests to a backend over plain HTTP. Use a self-signed certificate for testing and document how to switch to Let's Encrypt for production.

What's Next

Continue with Request Transformation in API Gateway to modify requests before forwarding, or explore Caching in API Gateway for improving response times.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro