Skip to content

Running and Securing the Server

Outcome: your server runs from a config file you can re-read and change, you can see who is connected and what it is doing, and the door is locked — first with a shared token, then with named users, one of them fenced to a single subject tree.

A note before the locks, because it belongs in the text early: a NATS server with no configuration accepts every connection from anyone who can reach its port. That is the right default for a first afternoon on your own machine and the wrong steady state for anything else. Everything in this chapter's second half exists because messages are only as private as the server's door. The lab's passwords below are lab inventions, printed so the transcripts make sense; yours should be real secrets, kept out of files other people can read.

Running from a config file

Flags are for experiments; a server you keep runs from a file (nats-server -c server.conf). The file's dialect is forgiving — key: value, blocks in braces, comments with # — and chapter 3's flags all have file equivalents:

listen: 127.0.0.1:18084
http: 127.0.0.1:18085
server_name: labserver-token

Two habits: bind listen to the addresses that should reach the server (loopback above, for a same-machine system), and change one thing at a time — a running server can be told to re-read its file (send it a SIGHUP, or nats-server --signal reload), and chapter 14's first move for a sick server is still its log.

Watching it work

The monitoring port from chapter 3 serves the server's own account of itself as JSON pages:

  • /healthz — the one-word answer, for scripts and supervisors: {"status":"ok"}.
  • /varz — the vital signs: version, uptime, memory, connections, messages in and out, and the JetStream facts from chapter 2.
  • /connz — who is connected right now. The lab caught its own subscriber there, listed as NATS CLI Version 0.5.1 with 1 subscription (transcript t12). When a stranger is on your server, this page is where you would see them first.

And a sense of scale, honestly framed: the lab's nats bench pushed 2,000 small messages through one publisher and subscriber in about a hundredth of a second — roughly 190,000 messages a second on a small, shared sandbox machine, loopback only. That is a floor for what one server does without trying, not a promise about your network, your message sizes, or JetStream's disk-backed path, which trades speed for memory on purpose.

Lock one: a shared token

The smallest real lock is one secret every client must present (nats-lab/server-token.conf):

authorization {
  token: "lab-token-123"
}

Clients put the token in the server address — nats://lab-token-123@127.0.0.1:18084 — and transcript t10 shows the three outcomes side by side: no token → nats: Authorization Violation; wrong token → the same; right token → published. A token is a household key: fine for one person's machines, useless for telling which client did something. For that, names.

Lock two: named users and a fence

authorization {
  users: [
    { user: keeper, password: keeperpass }
    { user: till,   password: tillpass,
      permissions: {
        publish:   ["shop.>"]
        subscribe: ["shop.>", "_INBOX.>"]
      }
    }
  ]
}

(nats-lab/server-users.conf.) A user with no permissions block may do anything its credentials allow it to reach. A user with one may do only what it lists — separate fences for publishing and subscribing, written as subject patterns, wildcards included. The _INBOX.> entry is the one everyone forgets: replies arrive on inbox subjects, so a fenced user who cannot subscribe there can ask questions but never hear answers.

Transcript t11 grades all four cases against the running server: till publishes on shop.order.9 ✔; till publishing on greetings is refused by name — Permissions Violation for Publish to "greetings"; keeper publishes anywhere ✔; and a made-up user, mallory, gets the door answer, Authorization Violation, before subjects are even discussed.

Two honest boundaries. First, passwords in a config file are stored as written unless you hash them (the server accepts bcrypt hashes, and the CLI's nats tooling can generate them) — on a shared machine, hash them and mind the file's permissions. Second, config-file users are the small end of NATS security: larger deployments use its account and operator machinery, with signed credentials instead of a shared file. That machinery is documented by the project and deliberately not taught here; the fence above is the one a self-hosted server actually needs, and it is real.

Check: with the users config running, nats pub greetings "x" --server nats://till:tillpass@127.0.0.1:18084 must fail with a permissions violation, while shop.order.9 succeeds. If both succeed, the server is not running the config you think it is.