<!-- review: finished -->

<a id="http-limit-conn"></a>

# Limit Conn

The module is used to limit the number of connections per the defined key, in particular, the number of connections from a single IP address.

Not all connections are counted. A connection is counted only if it has a request being processed by the server and the whole request header has already been read.

<a id="configuration-example-25"></a>

## Configuration Example

```nginx
http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    ...

    server {

        ...

        location /download/ {
            limit_conn addr 1;
        }
```

<a id="directives-26"></a>

## Directives

<a id="index-0"></a>

<a id="limit-conn-1"></a>

### limit_conn

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `limit_conn` zone number;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| Default                                                                                  | —                           |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

Sets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the [error](#limit-conn-status) in reply to a request. For example, the directives

```nginx
limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
    location /download/ {
        limit_conn addr 1;
    }
```

allow only one connection per IP address at a time.

#### NOTE
In HTTP/2 and HTTP/3, each concurrent request is considered a separate connection.

There could be several `limit_conn` directives. For example, the following configuration will limit the number of connections to the server per client IP and, at the same time, the total number of connections to the virtual server:

```nginx
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;

server {
    ...
    limit_conn perip 10;
    limit_conn perserver 100;
}
```

These directives are inherited from the previous configuration level if and only if there are no `limit_conn` directives defined on the current level.

<a id="index-1"></a>

<a id="limit-conn-dry-run"></a>

### limit_conn_dry_run

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `limit_conn_dry_run` `on` | `off`;   |
|------------------------------------------------------------------------------------------|--------------------------------------|
| Default                                                                                  | `limit_conn_dry_run off;`            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location               |

Enables the dry run mode. In this mode, the number of connections is not limited, however, in the [shared memory zone](#limit-conn-zone), the number of excessive connections is accounted as usual.

<a id="index-2"></a>

<a id="limit-conn-log-level"></a>

### limit_conn_log_level

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `limit_conn_log_level` `info` | `notice` | `warn` | `error`;   |
|------------------------------------------------------------------------------------------|----------------------------------------------------------------|
| Default                                                                                  | `limit_conn_log_level error;`                                  |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location                                         |

Sets the desired logging level for cases when the server limits the number of connections.

<a id="index-3"></a>

<a id="limit-conn-status"></a>

### limit_conn_status

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `limit_conn_status` code;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| Default                                                                                  | `limit_conn_status 503;`    |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | http, server, location      |

Sets the status code to return in response to rejected requests.

<a id="index-4"></a>

<a id="limit-conn-zone"></a>

### limit_conn_zone

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `limit_conn_zone` key zone = name:size;   |
|------------------------------------------------------------------------------------------|-------------------------------------------|
| Default                                                                                  | —                                         |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | http                                      |

Sets parameters for a shared memory zone that will keep states for various keys. In particular, the state includes the current number of connections. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.

Usage example:

```nginx
limit_conn_zone $binary_remote_addr zone=addr:10m;
```

Here, a client IP address serves as a key. Note that instead of `$remote_addr`, the `$binary_remote_addr` variable is used here.

The `$remote_addr` variable's size can vary from 7 to 15 bytes. The stored state occupies either 32 or 64 bytes of memory on 32-bit platforms and always 64 bytes on 64-bit platforms.

The `$binary_remote_addr` variable's size is always 4 bytes for IPv4 addresses or 16 bytes for IPv6 addresses. The stored state always occupies 32 or 64 bytes on 32-bit platforms and 64 bytes on 64-bit platforms.

One megabyte zone can keep about 32 thousand 32-byte states or about 16 thousand 64-byte states. If the zone storage is exhausted, the server will return the [error](#limit-conn-status) to all further requests.

<a id="built-in-variables-2"></a>

## Built-in Variables

<a id="v-limit-conn-status"></a>

### `$limit_conn_status`

keeps the result of limiting the number of connections: `PASSED`, `REJECTED`, or `REJECTED_DRY_RUN`
