Kong troubleshooting

A 502 Bad Gateway error in your setup:

Client → LB → KONG → LB → Server

means that Kong Gateway cannot successfully connect to the upstream server behind the second load balancer. Here’s how to troubleshoot and resolve it step by step:


🔁 Understanding the Flow

  1. Client hits the first LB (e.g., AWS ELB, NGINX).
  2. That LB forwards traffic to Kong Gateway.
  3. Kong receives the request and proxies it to a second LB.
  4. That LB routes to your actual upstream service.

🛠️ Troubleshooting Checklist

✅ 1. Check Upstream URL in Kong

  • Run: curl -s http://<KONG_ADMIN>:8001/services/<service-name>
  • Ensure the upstream URL (url or host) points to a valid, resolvable address (DNS or IP) and correct port.

Example:

{
  "host": "internal-lb.yourdomain.local",
  "port": 8080,
  "protocol": "http"
}

✅ 2. Test Kong’s Network Reachability

From the Kong container or host:

curl -v http://internal-lb.yourdomain.local:8080/

If this fails:

  • DNS resolution might be broken
  • Port might be blocked
  • LB might not route correctly

✅ 3. Enable Debug Logs in Kong

In kong.conf or via environment variable:

log_level = debug

Then check:

tail -f /usr/local/kong/logs/error.log

Look for messages like:

  • upstream timed out
  • could not resolve host
  • connection refused

✅ 4. Check Health of Second LB and Backend

  • Ensure second LB is up
  • Verify backend servers are healthy and accepting connections
  • Check if Kong’s IP is allowed (firewall or security group)

✅ 5. Check Kong Route & Service Configuration

Validate route is defined correctly:

curl -s http://<KONG_ADMIN>:8001/routes

Make sure paths, hosts, or methods match the request.


🧪 Example Kong Service & Route Setup

# Service pointing to internal load balancer
curl -i -X POST http://localhost:8001/services \
  --data name=upstream-service \
  --data url=http://internal-lb.yourdomain.local:8080

# Route for the service
curl -i -X POST http://localhost:8001/services/upstream-service/routes \
  --data paths[]=/api


🚫 Common Causes of 502 with LB Behind Kong

ProblemSolution
DNS resolution failureUse IP or fix /etc/resolv.conf or CoreDNS
Port not exposed or wrongConfirm port with nc or curl
Second LB not forwarding correctlyCheck LB target groups and health checks
Kong plugins (e.g., OIDC, rate-limit) errorDisable plugins temporarily to isolate
HTTP vs HTTPS mismatchEnsure protocol matches (http vs https)
Timeout too shortIncrease proxy_read_timeout or similar

✅ Final Tips

  • Try curl directly from Kong to the backend server.
  • Use Kong’s health check endpoint if you’re using upstream targets: curl http://localhost:8001/upstreams/<name>/health

If you share:

  • the exact curl call to Kong
  • the relevant Kong service/route config
  • error.log content from Kong

The error message “upstream prematurely closed connection while reading response header from upstream” in Kong Gateway indicates that Kong attempted to read the response headers from the upstream service, but the connection was closed unexpectedly before the headers were fully received. This typically results in a 502 Bad Gateway error.

Common Causes

  1. Upstream Service Crashes or Terminates Connection Early:
    • The upstream application may crash, encounter an error, or intentionally close the connection before sending a complete response.
  2. Timeouts:
    • The upstream service takes too long to respond, exceeding Kong’s configured timeouts.
  3. Keepalive Connection Issues:
    • Persistent connections (keepalive) between Kong and the upstream service may be closed unexpectedly by the upstream, leading to this error.
  4. Protocol Mismatch:
    • Kong expects a certain protocol (e.g., HTTP/1.1), but the upstream service responds differently or uses an incompatible protocol.
  5. Large Response Headers:
    • The upstream service sends headers that exceed Kong’s buffer sizes, causing the connection to be closed prematurely.

es, a mismatch between the protocol specified in Kong’s service configuration and the actual protocol used by the upstream service can lead to the error:

“upstream prematurely closed connection while reading response header from upstream”

This typically occurs when Kong attempts to communicate with an upstream service over HTTP, but the upstream expects HTTPS, or vice versa.


🔍 Understanding the Issue

When Kong is configured to connect to an upstream service, it uses the protocol specified in the service’s configuration. If the upstream service expects HTTPS connections and Kong is configured to use HTTP, the SSL/TLS handshake will fail, leading to the connection being closed prematurely.

Yes, a mismatch between the protocol specified in Kong’s service configuration and the actual protocol used by the upstream service can lead to the error:

“upstream prematurely closed connection while reading response header from upstream”

This typically occurs when Kong attempts to communicate with an upstream service over HTTP, but the upstream expects HTTPS, or vice versa.


🔍 Understanding the Issue

When Kong is configured to connect to an upstream service, it uses the protocol specified in the service’s configuration. If the upstream service expects HTTPS connections and Kong is configured to use HTTP, the SSL/TLS handshake will fail, leading to the connection being closed prematurely.

For example, if your upstream service is accessible at https://api.example.com, but Kong is configured with:

bashCopyEditcurl -i -X POST http://localhost:8001/services \
  --data name=example-service \
  --data url=http://api.example.com

Kong will attempt to connect over HTTP, resulting in a failed connection.


✅ Solution

Ensure that the protocol in Kong’s service configuration matches the protocol expected by the upstream service.

If the upstream service requires HTTPS, configure the service in Kong accordingly:

bashCopyEditcurl -i -X POST http://localhost:8001/services \
  --data name=example-service \
  --data url=https://api.example.com

This ensures that Kong establishes a secure connection using HTTPS, aligning with the upstream service’s expectations.

Kong API Gateway

Kong API Gateway is a lightweight, fast, and flexible solution for managing APIs. It acts as a reverse proxy, sitting between clients (e.g., applications, users) and upstream services (e.g., APIs, microservices). Kong provides features like request routing, authentication, rate limiting, logging, and monitoring.


How Kong API Gateway Works

  1. Clients Make Requests:
    • Applications or users send HTTP/HTTPS requests to the Kong Gateway.
  2. Kong Intercepts Requests:
    • Kong routes these requests to the appropriate upstream service based on configuration rules.
    • It can apply middleware plugins for authentication, rate limiting, transformations, logging, and more.
  3. Plugins Process Requests:
    • Plugins enhance Kong’s functionality. For example:
      • Authentication plugins: Validate tokens or credentials.
      • Rate limiting plugins: Control the number of requests allowed.
      • Logging plugins: Send logs to monitoring systems.
      • Transformation plugins: Modify requests or responses.
  4. Request Routed to Upstream:
    • Kong forwards the processed request to the backend service (API or microservice).
  5. Upstream Service Responds:
    • The upstream service sends the response back to Kong.
  6. Kong Returns Response:
    • Kong optionally applies response transformations (e.g., add headers) before sending the response to the client.

Key Components of Kong

ComponentDescription
ProxyRoutes incoming requests to the appropriate upstream service.
Admin APIManages Kong configurations, including services, routes, and plugins.
DatabaseStores Kong configuration data (e.g., PostgreSQL or Cassandra).
PluginsExtend Kong’s functionality (e.g., authentication, monitoring, logging).
Upstream ServicesThe actual backend services or APIs that Kong forwards requests to.

Diagram: How Kong API Gateway Works

Here’s a simplified visual representation of Kong’s architecture:


Detailed Kong Workflow with Features

  1. Request Received by Kong
    A request like https://api.example.com/v1/orders reaches Kong.
    Kong matches the request with:
    • A route: (e.g., /v1/orders).
    • A service: The upstream API serving the request.
  2. Plugins Applied
    Kong processes the request with active plugins for:
    • Authentication: Checks API keys, OAuth tokens, or LDAP credentials.
    • Rate Limiting: Ensures the client doesn’t exceed allowed requests.
    • Logging: Sends logs to external systems like ElasticSearch or Splunk.
  3. Routing to Upstream
    After processing, Kong forwards the request to the appropriate upstream service.
    Example:
  4. Response Handling
    The upstream service responds to Kong.
    Plugins can modify responses (e.g., masking sensitive data).
  5. Response Sent to Client
    Kong sends the final response back to the client.

Common Use Cases

  1. API Security:
    • Add layers of authentication (e.g., JWT, OAuth, mTLS).
    • Enforce access control policies.
  2. Traffic Control:
    • Apply rate limiting or request throttling to prevent abuse.
  3. API Management:
    • Route requests to appropriate backend APIs or microservices.
  4. Monitoring & Analytics:
    • Capture detailed logs and metrics about API usage.
  5. Ease of Scalability:
    • Kong can scale horizontally, ensuring high availability and performance.

Advanced Configurations

  1. Load Balancing: Kong can distribute requests across multiple instances of an upstream service.
  2. mTLS: Mutual TLS ensures secure communication between Kong and clients or upstream services.
  3. Custom Plugins: You can write custom Lua or Go plugins to extend Kong’s capabilities.