{"record":{"id":"aff6c36678ef9ce4","repo":"nats-io/nats-server","slug":"attempted-to-connect-to-leaf-node-port","errorCode":null,"errorMessage":"attempted to connect to leaf node port","messagePattern":"attempted to connect to leaf node port","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/errors.go","lineNumber":87,"sourceCode":"\tErrTooManyAccountConnections = errors.New(\"maximum account active connections exceeded\")\n\n\t// ErrLeafNodeLoop signals a leafnode is trying to register for a cluster we already have registered.\n\tErrLeafNodeLoop = errors.New(\"leafnode loop detected\")\n\n\t// ErrTooManySubs signals a client that the maximum number of subscriptions per connection\n\t// has been reached.\n\tErrTooManySubs = errors.New(\"maximum subscriptions exceeded\")\n\n\t// ErrTooManySubTokens signals a client that the subject has too many tokens.\n\tErrTooManySubTokens = errors.New(\"subject has exceeded number of tokens limit\")\n\n\t// ErrClientConnectedToRoutePort represents an error condition when a client\n\t// attempted to connect to the route listen port.\n\tErrClientConnectedToRoutePort = errors.New(\"attempted to connect to route port\")\n\n\t// ErrClientConnectedToLeafNodePort represents an error condition when a client\n\t// attempted to connect to the leaf node listen port.\n\tErrClientConnectedToLeafNodePort = errors.New(\"attempted to connect to leaf node port\")\n\n\t// ErrLeafNodeHasSameClusterName represents an error condition when a leafnode is a cluster\n\t// and it has the same cluster name as the hub cluster.\n\tErrLeafNodeHasSameClusterName = errors.New(\"remote leafnode has same cluster name\")\n\n\t// ErrLeafNodeDisabled is when we disable leafnodes.\n\tErrLeafNodeDisabled = errors.New(\"leafnodes disabled\")\n\n\t// ErrConnectedToWrongPort represents an error condition when a connection is attempted\n\t// to the wrong listen port (for instance a LeafNode to a client port, etc...)\n\tErrConnectedToWrongPort = errors.New(\"attempted to connect to wrong port\")\n\n\t// ErrAccountExists is returned when an account is attempted to be registered\n\t// but already exists.\n\tErrAccountExists = errors.New(\"account exists\")\n\n\t// ErrBadAccount represents a malformed or incorrect account.\n\tErrBadAccount = errors.New(\"bad account\")","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/errors.go#L69-L105","documentation":"ErrClientConnectedToLeafNodePort means a regular client connection attempted to connect to the leaf-node listen port. The leafnode CONNECT handshake carries no 'lang' field, so when a connection supplies lang the server knows it is a client library and not a leaf node, sends the error, and closes with WrongPort (leafnode.go:2226).","triggerScenarios":"Connecting a normal NATS client (which sends lang in CONNECT) to the leafnodes listen port, e.g. nats://host:7422 when 7422 is the leafnode port. Triggered during leaf CONNECT processing when lang != \"\".","commonSituations":"Using the leafnode port in an application's client URL; confusing leafnode port with client port after deploying gateways/superclusters; docker service exposing multiple ports where the wrong one is mapped in the client config.","solutions":["Point the client at the client port (default 4222), not the leafnodes port (default 7422)","Check server config under 'leafnodes { listen }' to identify the leaf port and avoid it in client URLs","If a leaf node connection is intended, use a server that speaks the leaf protocol (another NATS server), not a client library"],"exampleFix":"// before\nnc, _ := nats.Connect(\"nats://nats-1:7422\") // leafnode port\n// after\nnc, _ := nats.Connect(\"nats://nats-1:4222\") // client port","handlingStrategy":"validation","validationCode":"u, err := url.Parse(natsURL)\nif err != nil { return err }\nif port := u.Port(); port == \"7422\" { return fmt.Errorf(\"%s is a leafnode port; use the client port\", natsURL) }","typeGuard":"func isClientPort(u *url.URL) bool { return u.Port() != \"7422\" && u.Port() != \"6222\" }","tryCatchPattern":"nc, err := nats.Connect(url)\nif err != nil && strings.Contains(err.Error(), \"leaf node port\") {\n    log.Fatalf(\"%q points at the leafnode port; use the client port\", url)\n}","preventionTips":["Document each exposed port in deployments and which component may use it","Restrict leafnode ports to inter-server traffic (firewall/ACL)","Template client URLs only from client-port variables"],"tags":["nats","configuration","wrong-port","leafnode"],"backgroundTag":"wrong-port-connection","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}