The Simplest Way to Make CockroachDB and Nginx Work Like They Should
You pair cockroachdb and nginx to route SQL queries and web console traffic across nodes. This setup spreads client load across healthy database nodes and keeps single node faults away from your apps. CockroachDB runs as a multi-node cluster where every node can take reads and writes. Nginx sits in front of those nodes and sends each new request to an active node. When a node restarts or drops offline, Nginx shifts traffic to the remaining healthy nodes right away. How traffic and logins flow
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You pair cockroachdb and nginx to route SQL queries and web console traffic across nodes. This setup spreads client load across healthy database nodes and keeps single node faults away from your apps.
CockroachDB runs as a multi-node cluster where every node can take reads and writes. Nginx sits in front of those nodes and sends each new request to an active node. When a node restarts or drops offline, Nginx shifts traffic to the remaining healthy nodes right away.
How traffic and logins flow between cockroachdb and nginx
CockroachDB exposes two main network ports on every node in the cluster. One port handles wire-protocol SQL traffic from apps and command-line clients. The second port serves the web admin console over standard HTTPS.
Nginx handles these two traffic types in two distinct ways. For SQL traffic, Nginx uses its TCP stream module to forward raw packets to database nodes. For the web console, Nginx acts as a reverse web router and forwards HTTP requests.
Security between the two systems relies on TLS certificates and database roles. In most setups, Nginx passes encrypted SQL traffic straight through to the database nodes. The database node then checks the client certificate or password before opening a session.
For the web console, teams often end the outer TLS link at Nginx. Nginx then opens a fresh encrypted link to the backend database console port. The user still logs in with a database username and password on the console page.
Common failure modes and how to scope roles and network paths
A common mistake is using one shared database account for every app behind Nginx. When all traffic flows through Nginx using one role, database logs lose individual user context. You can seeNginx's network address in the log, not the engineer or service that ran the query.
Broad network access is another frequent failure mode in this setup. If engineers can reach the Nginx SQL port from anywhere, stolen keys create a big risk. The database console port also should not sit open to public networks.
End-to-End Encryption + NGINX Security Configuration: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
To fix network paths, place the database nodes in a private subnet. Allow inbound traffic on the SQL and admin ports only from the Nginx hosts. Configure Nginx to run active health checks against the database health endpoint before sending SQL traffic.
To fix role sprawl, give each backend app its own database role with least-privilege rules. Limit read and write rules to the exact tables each app needs. For human users, require individual accounts instead of shared admin logins. Masking rewrites matched fields like emails, phone numbers, card numbers, and national IDs in query results.
How to keep health checks and timeouts in sync
Long-running queries and idle sessions can drop if Nginx and CockroachDB timeouts clash. Nginx might close an idle TCP link while an app still holds the pool entry open. The next query on that pool link then fails with a broken pipe error.
You can prevent this by matching idle timeout settings on both systems. Set the app pool idle limit lower than the Nginx stream timeout. Enable TCP keepalive probes so both sides detect dead links fast.
When you drain a database node for an upgrade, health checks matter even more. Tell the node to drain first so its health endpoint reports a non-ready state. Nginx will spot the change, stop sending new connections, and let open queries finish cleanly.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Should Nginx terminate TLS for CockroachDB SQL connections?
Pass-through TLS is usually safer for SQL traffic on the TCP stream port. This allows the database node to check client certs directly and keeps query data encrypted in transit.
How does Nginx know when a CockroachDB node is down?
Each database node serves an HTTP health path on its admin port. You can point Nginx health checks at that path so it stops sending traffic to unhealthy nodes.
Why do database logs show the Nginx IP address for every query?
TCP forwarding rewrites the source IP address to the Nginx host address by default. You must rely on distinct database user roles or identity tools to track who ran each query.
Related guides
In this post