SimpleACME
Sign inCreate account →
Software certificate guides

NATS native TLS without confusing the messaging handshake

Configure native NATS TLS, understand INFO versus TLS-first handshakes, preserve subject permissions, and validate certificate reloads with fresh clients.

Securing NATS means deciding which connection you are securing. Application clients, cluster routes, gateways, leaf nodes, monitoring endpoints, and WebSocket clients do not all inherit one universal TLS policy. A correct certificate on port 4222 says nothing about a route on another listener. Start with a narrow, testable change: native TLS for application clients, with authentication and subject permissions preserved.

This guide assumes a maintained NATS Server 2.x release that supports the illustrated configuration, the current NATS CLI, and an existing authenticated client deployment on Linux. It uses the normal NATS INFO-then-TLS handshake initially. TLS-first is a separate compatibility decision later in the guide. Check the exact installed version before adopting newer options. The commands are examples for an operator to review; they have not been executed against a live messaging system.

Step 1: Draw the client connection boundary

Choose nats.example.com as the name applications connect to, and identify which server addresses the deployment advertises back to clients. If clients reconnect to discovered individual server names, those names must also have appropriate certificates. A load-balancer name alone is insufficient when a library learns and uses direct addresses. Record the clients' actual connection URLs and the behavior of their reconnect logic.

Separate this work from message authorization. Existing users, NKeys, JWT credentials, account isolation, and publish/subscribe permissions remain relevant after TLS is enabled. A certificate authenticates the listener to a client; it does not decide whether that client may publish orders.created or subscribe to every subject. Keep an explicit test account with limited subjects so connectivity tests cannot accidentally become privileged production publishers.

For a cluster, write down the route and leaf-node ports without changing them in this initial step. Native TLS for client connections belongs in the top-level tls block. Other connection types have their own settings and may require mutual authentication. Treat each as a separate trust relationship with a separate migration plan. This prevents a misleading completion report that says the whole NATS topology is encrypted after only the application edge changed.

Take a copy of the active configuration, included account files, service definition, and current certificate generation. Confirm how the process receives reload signals under your supervisor or container runtime. A host systemctl command cannot reload a server running in an unrelated container, and a process ID copied from yesterday's notes is not a reliable signal target.

Step 2: Arrange delegated validation and a local deployment directory

Create the domain registration in SimpleACME and install its assigned CNAME before configuring issuance. The account registration provides the challenge target; active paid service permits the TXT updates that your ACME client needs. Use a hostname you control even when NATS is reachable only over a VPN. DNS-01 validation does not require making the NATS listener or an HTTP challenge server publicly reachable.

Follow the Certbot integration instructions and the subscriber deployment material rather than inventing a NATS-specific ACME plugin. NATS consumes certificate files. Your chosen ACME client performs the CA order, challenge handling, key generation, and renewal scheduling. SimpleACME is the delegated update service and does not issue certificates or run a NATS administration command.

Deploy a PEM full chain and its matching key into a dedicated directory, such as /etc/nats/tls/current. Restrict key access to the NATS service identity and the trusted deployment identity. Do not give all application containers access to the server key merely because they use the messaging system. Clients need to trust a CA and verify a hostname; they do not need a copy of the server's private key.

For bind-mounted containers, mount a directory rather than an individual file when you intend to atomically replace files. A process bound to an old file inode can otherwise keep seeing an earlier generation. Keep the old directory available for rollback, but protect it as carefully as the current directory because previous private keys remain sensitive after renewal.

Step 3: Configure server authentication without changing user identity

Merge the following settings into your existing NATS configuration. Retain authorization or account declarations already used by your applications. The example leaves client certificate authentication off; applications continue presenting their existing credentials after negotiating TLS. Do not add verify_and_map unless you are intentionally changing the authentication system and have designed the matching identities.

server_name: nats-a
port: 4222
tls {
  cert_file: "/etc/nats/tls/current/fullchain.pem"
  key_file: "/etc/nats/tls/current/privkey.pem"
  timeout: 5
}
# Preserve your existing authorization/accounts configuration here.

Before activation, parse the configuration using the server binary from the same release as the running service. This checks syntax, not certificate trust from every client or subject authorization. If a wrapper generates configuration from environment variables, inspect its effective output as well. Editing a file that the container entrypoint immediately overwrites can make a successful maintenance window appear to revert at the next restart.

nats-server -t -c /etc/nats/nats.conf
openssl x509 -in /etc/nats/tls/current/fullchain.pem \
  -noout -dates -ext subjectAltName
openssl pkey -in /etc/nats/tls/current/privkey.pem -check -noout

Apply the change during an agreed client reconnect window if enabling TLS for the first time. Existing plaintext clients must be updated; a successful configuration parse does not make them TLS-aware. In a multiple-server deployment, confirm every advertised endpoint and client library is ready before enforcing a policy that some of the pool cannot satisfy. Preserve enough healthy servers to meet your own messaging availability requirements.

Step 4: Prove messaging over TLS with the native client

Use a NATS context or an approved credentials file with restrictive permissions. Do not place a token directly in a command that will be captured in process listings or incident logs. Create a subscription on a harmless test subject and publish a distinct marker from another terminal. Restrict both commands to a test account so the demonstration cannot consume business events or affect production subscribers.

nats --server tls://nats.example.com:4222 \
  --creds /secure/nats/tls-check.creds sub _tlscheck.demo
nats --server tls://nats.example.com:4222 \
  --creds /secure/nats/tls-check.creds pub _tlscheck.demo 'deployment-check'

A private CA deployment may require the CLI's CA option; use the flag supported by your installed CLI and distribute the CA through an approved channel. Do not use an insecure flag to turn a trust failure into a green screenshot. Test with the same hostname and network path your applications use, including a reconnect to another member if discovery is enabled.

Plain openssl s_client is a poor first diagnostic against the default NATS handshake. NATS normally sends an INFO line before TLS negotiation, so a tool expecting an immediate TLS record can report a protocol error even though a native NATS client works correctly. Distinguish this from an expired certificate or a wrong SAN. Use native client diagnostics first, and consult the official TLS guide for handshake-aware testing tools.

Observe server logs for rejected authentication, authorization violations, handshake failures, and repeated reconnect loops. Those are different failure classes. A subject permission error after a successful TLS connection should be repaired in the intended account policy, not by loosening certificate verification or handing the application a global administrator credential.

Step 5: Decide whether TLS-first is compatible with every client

TLS-first hides the initial protocol metadata behind the TLS handshake and makes the listener behave more like an ordinary TLS endpoint. It also changes what clients must do on connection. Enable it only after checking each client library and configuring its corresponding TLS-first option. Do not assume a library that supports ordinary NATS TLS automatically enables the different handshake order.

tls {
  cert_file: "/etc/nats/tls/current/fullchain.pem"
  key_file: "/etc/nats/tls/current/privkey.pem"
  handshake_first: true
}

The current CLI uses --tlsfirst for this mode. A mixed estate may need the documented transitional behavior rather than an immediate strict switch. Keep that compatibility exercise separate from certificate renewal. Changing handshake order and certificate material at once gives you two plausible explanations for every failed connection and makes rollback harder to interpret.

Once TLS-first is deliberately enabled, a direct certificate probe becomes meaningful. Connect with SNI and hostname verification, inspect the chain and expiry, and compare the fresh connection with the deployed generation. That probe verifies transport identity; repeat the limited publish/subscribe test to verify application behavior as well. Neither check alone covers the other layer.

Step 6: Reload renewed files and retain a fast reversal

Design the deploy operation around a validated certificate generation. Check key correspondence, SANs, expiry, and file ownership before replacing the current files. Validate the NATS configuration, signal the correct process, then create a new client connection. The documented reload mechanism is SIGHUP or nats-server's signal command; use whichever is appropriate to your process manager.

nats-server -t -c /etc/nats/nats.conf
nats-server --signal reload=12345

Replace 12345 with the verified running NATS process ID; it is not a reusable constant. Existing sessions may retain their original TLS handshake, so watching an already-connected subscriber is not proof the new certificate loaded. Open a fresh connection and inspect server logs. In a clustered deployment, rotate members deliberately and check reconnect behavior rather than broadcasting unverified signals across the fleet.

If the fresh connection fails, restore the previous generation and reload it while investigating. Keep the authentication files unchanged unless they caused the problem. Alert on both the issued certificate and the certificate served by each advertised listener. DNS activity in SimpleACME confirms a challenge update happened; it does not prove NATS reloaded, messages flowed, or a leaf node remained connected.

Keep JetStream durability outside the certificate change

If your server also runs JetStream, preserve its store directory, account limits, and cluster settings throughout the TLS rollout. Encryption in transit and encryption of stored message data solve different problems. A certificate replacement should not introduce a new storage encryption key, move stream files, or alter replication policy. Keeping those changes separate makes a messaging regression easier to isolate and protects the rollback plan from unrelated state transitions.

Frequently asked questions

Does the top-level TLS block encrypt cluster routes?

No. Cluster, gateway, leaf-node, monitoring, MQTT, and WebSocket connections have their own configuration boundaries. Plan and validate those explicitly. This guide secures the native application listener and does not claim the entire topology is encrypted.

Why does OpenSSL fail while the NATS CLI connects?

The usual NATS handshake begins with plaintext INFO before upgrading to TLS. OpenSSL expects immediate TLS unless the protocol is handled appropriately. Use the native CLI to separate handshake-order incompatibility from actual certificate verification errors.

Should I enable verify_and_map for a public ACME certificate?

Not simply to enable encryption. That option changes how client certificate identities map to users. Retain your existing credentials unless you intentionally deploy mutual TLS with suitable client certificates and an explicit identity mapping design.

Will renewal disconnect every subscriber?

Reloaded certificates affect new handshakes, while existing sessions can retain their original connection state. Availability depends on your exact configuration and clients. Exercise reconnects in a maintenance plan and monitor real subject traffic rather than promising that any reload is interruption-free.

Official references

NATS encryption and TLS documents the separate connection types and handshake modes. NATS configuration management covers validation and reload behavior. Use those primary references with your pinned NATS Server and CLI versions.

KEEP EXPLORING

A little more clarity.

ONE CNAME. A REPEATABLE WORKFLOW.

Your next certificate. A little less complicated.

Keep your DNS provider and your ACME client.
Prepare your domain before activating paid updates.

Create your account →Explore the integrations