<!-- review: finished -->

<a id="stream-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.

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

## Configuration Example

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

    ...

    server {

        ...

        limit_conn           addr 1;
        limit_conn_log_level error;
    }
}
```

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

## Directives

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

<a id="s-limit-conn"></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)  | stream, server              |

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 close the connection. For example, the directives

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

server {
    ...
    limit_conn addr 1;
}
```

allow only one connection per IP address at a time.

When several `limit_conn` directives are specified, any configured limit will apply.

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="s-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)  | stream, server                       |

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

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

<a id="s-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)  | stream, server                                                 |

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

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

<a id="s-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)  | stream                                    |

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 combinations. Connections 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 is set by the `$binary_remote_addr` variable.

The size of `$binary_remote_addr` is 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 close the connection.

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

## Built-in Variables

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

### `$limit_conn_status`

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