Backend TLS
Verified : Automated tests passed for: Configuration files: The configuration files on this page were checked against their schema. End-to-end run: Every step ran in a fresh test environment and the results were checked.Configure TLS for secure connections to backend services.
Attaches to:
Note
Agentgateway supports more than one configuration style. Where a feature can also be configured in the simplified llm or mcp modes, the examples on this page show each option in tabs. For more information, see Routing-based configuration.
By default, requests to backends use HTTP. To use HTTPS, configure a backend TLSTLS (Transport Layer Security)A cryptographic protocol that provides secure communication over a network. Agentgateway supports TLS for both incoming connections (listeners) and outgoing connections (backends). policy.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
mcp:
port: 3000
policies:
backendTLS:
# A file containing the root certificate to verify.
# If unset, the system trust bundle will be used.
root: ./certs/root-cert.pem
# For mutual TLS, the client certificate to use
cert: ./certs/cert.pem
# For mutual TLS, the client certificate key to use.
key: ./certs/key.pem
# The server name to use for SNI and certificate verification.
# hostname: backend.internal.example.com
# If set, hostname verification is disabled
# insecureHost: true
# If set, all TLS verification is disabled
# insecure: true
targets:
- name: everything
stdio:
cmd: npx
args: ["@modelcontextprotocol/server-everything"]The backendTLS policy supports the following fields.
| Field | Description |
|---|---|
root | The path to a file with the root certificate that verifies the backend certificate. If unset, agentgateway uses the system trust bundle. If set, the file replaces the system trust bundle for this backend instead of adding to the bundle, so agentgateway refuses a certificate from a public CA unless the file also holds that CA. |
cert | The path to the client certificate for mutual TLS. |
key | The path to the private key of the client certificate for mutual TLS. |
hostname | The server name that agentgateway sends as Server Name Indication (SNI) and verifies the backend certificate against. If unset, agentgateway uses the host that it connects to. |
subjectAltNames | A list of names that agentgateway accepts instead of hostname. Agentgateway accepts the certificate if the certificate is valid for any name in the list, and no longer checks the certificate against hostname. This setting does not change the SNI, so set hostname as well if the backend needs a specific SNI. |
insecureHost | If set to true, agentgateway skips only the name check. The certificate must still be signed by a trusted CA, either the CA in root or, if root is unset, a CA in the system trust bundle. Use this setting when the name cannot be listed or changes, not to trust a certificate from an unknown CA. This setting takes precedence over subjectAltNames. |
insecure | If set to true, agentgateway disables all TLS verification. |
By default, agentgateway verifies the backend certificate against the host that it connects to. Set hostname when the certificate is issued for a different name.
Note
To use TLS for a custom prompt guard webhook, set target.policies.backendTLS on the webhook target instead. For more information, see Configure webhook backend policies.
Mutual TLS and certificate rotation
To originate mutual TLS (mTLS) to a backend, set the cert and key fields of the backendTLS policy to the client certificate and private key files. Set root to the CA bundle that verifies the backend server certificate.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
gateways:
default:
port: 3000
routes:
- backends:
- host: backend.example.com:443
policies:
backendTLS:
root: /etc/agentgateway/certs/ca.crt
cert: /etc/agentgateway/certs/tls.crt
key: /etc/agentgateway/certs/tls.keyAgentgateway reads these files at startup and watches them. When a file changes, agentgateway reloads the configuration and uses the new certificate for new connections to the backend. You do not need to restart agentgateway. This behavior is useful when another process, such as a certificate manager or a SPIFFE helper, renews the certificate on disk.
When agentgateway reloads, it logs the changed file.
info state_manager resource changed, reloading resource="file:/etc/agentgateway/certs/tls.crt"
info state_manager loaded config from File("/etc/agentgateway/config.yaml")If the new files are not valid, such as when the certificate is not PEM or does not match the key, agentgateway logs an error and keeps using the previous configuration.
error state_manager Failed to reload config: keys may not be consistent: KeyMismatchIf you replace the certificate and the key with two separate writes, agentgateway might log this error after the first write, because the new certificate does not match the old key. The reload succeeds after the second write.
Note
The config.fileAccess setting does not apply to the files that your configuration file references, such as the root, cert, and key files. Agentgateway loads these files at startup and when they change.
Verify the backend name
Agentgateway checks that the backend certificate is signed by a trusted CA and is valid for a name. By default, the name is the host of the backend, such as localhost for a backend at localhost:8443. If the certificate is issued for a different name, set hostname. Agentgateway sends that name as the Server Name Indication (SNI) and checks the certificate against that name.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
gateways:
default:
port: 3000
routes:
- backends:
- host: localhost:8443
policies:
backendTLS:
root: ./certs/root-cert.pem
# Send this name as the SNI and verify the certificate against this name.
hostname: backend.example.comIf you cannot connect to the backend by a name that the certificate lists, set subjectAltNames instead. Agentgateway accepts the certificate when the certificate is valid for any name in the list, and no longer checks the certificate against hostname. A certificate that is valid only for hostname is refused if hostname is not in the list.
# yaml-language-server: $schema=https://agentgateway.dev/schema/config
gateways:
default:
port: 3000
routes:
- backends:
- host: localhost:8443
policies:
backendTLS:
root: ./certs/root-cert.pem
# Accept the certificate if the certificate is valid for any of these names.
subjectAltNames:
- backend.example.com
- backend.internal.example.comIf the backend certificate fails verification, agentgateway ends the connection before it sends the request and returns a 503 response. The backend does not receive the request.
Invalid backend TLS settings
If a backendTLS block is invalid, such as when the root file does not contain valid PEM, agentgateway rejects the configuration instead of sending requests to the backend without TLS. At startup, agentgateway exits with code 1 and an error such as Error: base64 decode error: InvalidCharacter(33). When you edit the configuration file of a running agentgateway, agentgateway logs Failed to reload config with the error and keeps using the previous configuration.