{"record":{"id":"a01ae6bc723a1ee9","repo":"nats-io/nats-server","slug":"gateway-q-has-no-port-specified-select-1-for-ra","errorCode":null,"errorMessage":"gateway %q has no port specified (select -1 for random port)","messagePattern":"gateway %q has no port specified \\(select -1 for random port\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/gateway.go","lineNumber":317,"sourceCode":"\t\tclone.TLSConfig = r.TLSConfig.Clone()\n\t\tclone.TLSTimeout = r.TLSTimeout\n\t}\n\treturn clone\n}\n\n// Ensure that gateway is properly configured.\nfunc validateGatewayOptions(o *Options) error {\n\tif o.Gateway.Name == _EMPTY_ && o.Gateway.Port == 0 {\n\t\treturn nil\n\t}\n\tif o.Gateway.Name == _EMPTY_ {\n\t\treturn errors.New(\"gateway has no name\")\n\t}\n\tif strings.Contains(o.Gateway.Name, \" \") {\n\t\treturn ErrGatewayNameHasSpaces\n\t}\n\tif o.Gateway.Port == 0 {\n\t\treturn fmt.Errorf(\"gateway %q has no port specified (select -1 for random port)\", o.Gateway.Name)\n\t}\n\tfor i, g := range o.Gateway.Gateways {\n\t\tif g.Name == _EMPTY_ {\n\t\t\treturn fmt.Errorf(\"gateway in the list %d has no name\", i)\n\t\t}\n\t\tif len(g.URLs) == 0 {\n\t\t\treturn fmt.Errorf(\"gateway %q has no URL\", g.Name)\n\t\t}\n\t}\n\tif err := validatePinnedCerts(o.Gateway.TLSPinnedCerts); err != nil {\n\t\treturn fmt.Errorf(\"gateway %q: %v\", o.Gateway.Name, err)\n\t}\n\treturn nil\n}\n\n// Computes a hash of 6 characters for the name.\n// This will be used for routing of replies.\nfunc getGWHash(name string) []byte {","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/gateway.go#L299-L335","documentation":"Option validation for clustered gateways (validateGatewayOptions) requires an explicit Gateway.Port. Port 0 is treated as unset; NATS uses -1 to mean 'pick a random port', so 0 is rejected with guidance to use -1 instead.","triggerScenarios":"Starting a server with a gateway block that has a name but no port: gateway { name: \"A\" } with no port field, or programmatically Options.Gateway.Port == 0, passed to validateOptions at startup.","commonSituations":"Config file omits the gateway port, a template left the port blank, or code copying options reset Port to its zero value; users often expect 0 to mean random as it does for client ports.","solutions":["Set gateway { port: -1 } in the config to request a random port","Or set an explicit port, e.g. gateway { port: 7222 }","In code, set opts.Gateway.Port = -1 (or a real port) instead of leaving the zero value","Check for config templating/rendering that dropped the port value"],"exampleFix":"// before (config)\ngateway { name: \"A\" }\n// after\ngateway { name: \"A\", port: -1 }  # or port: 7222","handlingStrategy":"validation","validationCode":"// validate gateway options before server.New\nif opts.Gateway != nil && opts.Gateway.Name != \"\" && opts.Gateway.Port == 0 {\n    opts.Gateway.Port = -1 // or an explicit port\n}","typeGuard":null,"tryCatchPattern":"o, err := server.ProcessConfigFile(cfgPath)\nif err != nil {\n    if strings.Contains(err.Error(), \"has no port specified\") {\n        o.Gateway.Port = -1\n    }\n}\n// or: nats-server -t config.conf in CI to catch it pre-deploy","preventionTips":["Always set an explicit gateway port in production configs","Use -1 consciously when random ports are acceptable","Lint config templates so gateway blocks include a port","Run nats-server -t in CI to validate configs before deploy"],"tags":["config","gateway","validation"],"backgroundTag":"missing-port-configuration","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}