{"record":{"id":"ef2cd5b3fc8fea4d","repo":"nats-io/nats-server","slug":"attempted-to-connect-to-gateway-port","errorCode":null,"errorMessage":"attempted to connect to gateway port","messagePattern":"attempted to connect to gateway port","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/errors.go","lineNumber":157,"sourceCode":"\n\t// ErrStreamImportBadPrefix is returned when a stream import prefix contains wildcards.\n\tErrStreamImportBadPrefix = errors.New(\"stream import prefix can not contain wildcard tokens\")\n\n\t// ErrStreamImportDuplicate is returned when a stream import is a duplicate of one that already exists.\n\tErrStreamImportDuplicate = errors.New(\"stream import already exists\")\n\n\t// ErrServiceImportAuthorization is returned when a service import is not authorized.\n\tErrServiceImportAuthorization = errors.New(\"service import not authorized\")\n\n\t// ErrImportFormsCycle is returned when an import would form a cycle.\n\tErrImportFormsCycle = errors.New(\"import forms a cycle\")\n\n\t// ErrCycleSearchDepth is returned when we have exceeded our maximum search depth..\n\tErrCycleSearchDepth = errors.New(\"search cycle depth exhausted\")\n\n\t// ErrClientOrRouteConnectedToGatewayPort represents an error condition when\n\t// a client or route attempted to connect to the Gateway port.\n\tErrClientOrRouteConnectedToGatewayPort = errors.New(\"attempted to connect to gateway port\")\n\n\t// ErrWrongGateway represents an error condition when a server receives a connect\n\t// request from a remote Gateway with a destination name that does not match the server's\n\t// Gateway's name.\n\tErrWrongGateway = errors.New(\"wrong gateway\")\n\n\t// ErrGatewayNameHasSpaces signals that the gateway name contains spaces, which is not allowed.\n\tErrGatewayNameHasSpaces = errors.New(\"gateway name cannot contain spaces\")\n\n\t// ErrNoSysAccount is returned when an attempt to publish or subscribe is made\n\t// when there is no internal system account defined.\n\tErrNoSysAccount = errors.New(\"system account not setup\")\n\n\t// ErrRevocation is returned when a credential has been revoked.\n\tErrRevocation = errors.New(\"credentials have been revoked\")\n\n\t// ErrServerNotRunning is used to signal an error that a server is not running.\n\tErrServerNotRunning = errors.New(\"server is not running\")","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/errors.go#L139-L175","documentation":"ErrClientOrRouteConnectedToGatewayPort is returned when a plain client or route connection sends its CONNECT to a gateway listener. Gateway connections are expected to be from other servers and must include a `gateway` name in the CONNECT protocol (server/gateway.go:1002 rejects anything with `connect.Gateway == \"\"`). The server sends an error to the connection and closes it with the WrongPort reason.","triggerScenarios":"Pointing a NATS client's servers URL at the gateway port; configuring a route (cluster) connection to the gateway port; any tool that dials the gateway port as if it were the normal client port.","commonSituations":"Port mix-ups in config where gateway.port equals client port or the client is given the gateway port; copy-pasting the gateway address into a client's `nats://` URL; deploying with load balancers that route to the wrong backend port.","solutions":["Point the client at the client port (default 4222), not the gateway port (default 7222).","Point cluster/routes at the cluster port (default 6222), not the gateway port.","Audit server config so `port`, `cluster { port }` and `gateway { port }` are distinct and documented.","If behind a proxy/LB, ensure the listener matches the protocol it forwards to."],"exampleFix":"// before\nnats.Connect(\"nats://gateway-host:7222\")\n// after\nnats.Connect(\"nats://gateway-host:4222\")","handlingStrategy":"validation","validationCode":"// Validate the URL port before connecting:\nfunc checkPort(raw string) error {\n    u, err := url.Parse(raw)\n    if err != nil { return err }\n    p := u.Port()\n    if p == \"7222\" { return fmt.Errorf(\"%s looks like a gateway port; use the client port (4222)\", raw) }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"nc, err := nats.Connect(url)\nif err != nil && strings.Contains(err.Error(), \"attempted to connect to gateway port\") {\n    // fix URL to use client port and retry once\n}","preventionTips":["Keep client, cluster (6222), and gateway (7222) ports distinct and documented per environment.","Store client connection URLs in config separate from inter-server URLs.","Never point load balancers that serve clients at gateway listeners.","Smoke-test client connectivity with `nats server check` against the client port."],"tags":["nats","gateway","port-misconfiguration","connection"],"backgroundTag":"wrong-port-connection","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}