Nacos Cluster Nginx Load Balancing Guide
This guide explains how to use Nginx as a load balancer for a Nacos cluster, covering the HTTP API (port 8848), Console (port 8080), and gRPC communication (port 9848).
Nacos 3.x Port Change: Starting from Nacos 3.x, the console port defaults to 8080 (no longer 8848), and the console path no longer has the
/nacosprefix. The HTTP API port remains 8848.
Background
When connecting to a Nacos cluster, you can configure multiple server addresses
in server-addr using commas:
spring.cloud.nacos.discovery.server-addr=192.168.190.128:8848,192.168.190.129:8848,192.168.190.130:8848However, this approach has a drawback: when the cluster scales up or down, you need to repackage and restart your application. Using Nginx as a load balancer avoids this problem by providing a single entry point.
Prerequisites
- Nacos cluster with 3+ nodes properly configured
- Nginx installed with the
streammodule (--with-streamduring./configure)
Important: The
streammodule is required for gRPC (TCP) proxying. Most package-manager installations include it by default, but if you compile Nginx from source, you must add--with-stream.
Nacos Cluster Setup
Ensure each Nacos node is configured for cluster mode:
cluster.conf (on each node):
192.168.190.128:8848192.168.190.129:8848192.168.190.130:8848application.properties (database configuration):
spring.datasource.platform=mysqldb.url.0=jdbc:mysql://192.168.190.1:3306/config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTCdb.user.0=rootdb.password.0=your_passwordNginx Configuration
Nacos uses three communication ports:
| Protocol | Default Port | Purpose |
|---|---|---|
| HTTP API | 8848 | Client API calls |
| HTTP Console | 8080 | Web console access |
| gRPC | 9848 | Client-server long connections (API port + 1000) |
Your Nginx configuration must handle all three ports.
Complete Nginx Configuration
worker_processes 1;
events { worker_connections 1024;}
# HTTP load balancing (for API and console)http { include mime.types; default_type application/octet-stream;
client_max_body_size 300m;
sendfile on; keepalive_timeout 65;
# Upstream Nacos API servers (port 8848) upstream nacos_api_cluster { server 192.168.190.128:8848; server 192.168.190.129:8848; server 192.168.190.130:8848; }
# Upstream Nacos console servers (port 8080) upstream nacos_console_cluster { server 192.168.190.128:8080; server 192.168.190.129:8080; server 192.168.190.130:8080; }
server { listen 7847; server_name localhost; charset utf-8;
# API requests location /nacos/ { proxy_pass http://nacos_api_cluster/nacos/; }
# Console (Nacos 3.x default port 8080, path without /nacos prefix) location / { proxy_pass http://nacos_console_cluster; } }}
# gRPC (TCP) load balancing (for client long connections)stream { # Upstream Nacos gRPC servers upstream nacos_grpc_cluster { server 192.168.190.128:9848 weight=1; server 192.168.190.129:9848 weight=1; server 192.168.190.130:9848 weight=1; }
server { listen 8847; proxy_pass nacos_grpc_cluster; }}Port Mapping
The Nginx listen ports are chosen to maintain the same offset relationship as Nacos defaults:
| Nginx Port | Nacos Port | Protocol | Purpose |
|---|---|---|---|
| 7847 | 8848 | HTTP | API calls |
| 7847 | 8080 | HTTP | Console access |
| 8847 | 9848 | gRPC | Client long connections |
Note: Nacos uses a fixed offset of 1000 from the HTTP API port for gRPC. If your Nacos API port is
8848, gRPC is on9848.
Client Configuration
Point your application to the Nginx address instead of individual Nacos nodes:
spring.cloud.nacos.discovery.server-addr=192.168.190.128:7847spring.cloud.nacos.config.server-addr=192.168.190.128:7847For multi-environment setups, you can use Maven profiles in your root pom.xml
to switch server addresses per environment.
Verification
- Start all Nacos cluster nodes
- Start Nginx with the above configuration
- Access the Nacos console at
http://192.168.190.128:7847(Nacos 3.x console path is/, port 8080) - Verify that your application can register services and fetch configurations
Troubleshooting
gRPC connection fails
- Ensure Nginx was compiled with
--with-stream(runnginx -Vto check) - Verify the gRPC port (9848) is accessible on all Nacos nodes
- Check that the
streamblock is at the top level ofnginx.conf, not insidehttp
Console not accessible
- Nacos 3.x console defaults to port 8080 — ensure
nacos_console_clusterpoints to the correct port - For older Nacos versions (< 3.x), the console port is 8848 with path
/nacos
Client gets stale configurations
- This is a known issue in Nacos cluster mode. See spring-cloud-alibaba#3644 for the fix that uses config snapshots during refresh.
Adding new Nacos nodes
When scaling the cluster, update only the Nginx configuration and reload:
nginx -s reloadNo application changes or restarts required.