0
0
Nginxdevops~5 mins

Connection limiting (limit_conn) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes a web server gets too many users at once, which can slow it down or crash it. Connection limiting helps control how many users connect at the same time to keep the server stable and fast.
When you want to stop one user from opening too many connections and slowing down the server.
When your website is getting a sudden spike of visitors and you want to keep it running smoothly.
When you want to protect your server from simple attacks that open many connections quickly.
When you run multiple websites on one server and want to limit connections per site.
When you want to make sure your server resources are shared fairly among users.
Config File - nginx.conf
nginx.conf
http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    server {
        listen 80;
        server_name example.com;

        location / {
            limit_conn addr 5;
            root /var/www/html;
            index index.html;
        }
    }
}

limit_conn_zone defines a shared memory zone named 'addr' to track connections by client IP address.

limit_conn addr 5; limits each IP address to 5 simultaneous connections.

This setup helps prevent any single user from opening too many connections at once.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new connection limiting settings without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Test the server response to confirm nginx is running and serving pages after applying limits.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 Jun 2023 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: limit_conn_zone tracks clients and limit_conn sets how many connections each client can open at once.

Common Mistakes
Not defining limit_conn_zone before using limit_conn.
Nginx needs the zone to track connections; without it, the limit_conn directive will cause errors.
Always define limit_conn_zone with a unique zone name and enough memory before using limit_conn.
Setting the limit_conn value too low, like 1, causing users to get blocked unnecessarily.
Users often open multiple connections for images, scripts, or tabs; too low a limit blocks normal use.
Set a reasonable limit like 5 or more depending on your site's needs.
Forgetting to reload nginx after changing the config.
Changes won't take effect until nginx reloads the configuration.
Run 'sudo systemctl reload nginx' after editing the config.
Summary
Define a shared memory zone with limit_conn_zone to track client connections.
Use limit_conn inside server or location blocks to set max simultaneous connections per client.
Test the configuration syntax with 'nginx -t' and reload nginx to apply changes.