| Syntax: | listen
|
|---|---|
| Default: | — |
| Context: | server |
Sets the address and port for the socket on which the server will accept connections. It is possible to specify just the port. The address can also be a hostname, for example:
listen 127.0.0.1:12345; listen *:12345; listen 12345; # same as *:12345 listen localhost:12345;
IPv6 addresses are specified in square brackets:
listen [::1]:12345; listen [::]:12345;
UNIX-domain sockets are specified with the “unix:” prefix:
listen unix:/var/run/nginx.sock;
The ssl parameter allows specifying that all connections accepted on this port should work in SSL mode.
The udp parameter configures a listening socket for working with datagrams (1.9.13).
The proxy_protocol parameter (1.11.4) allows specifying that all connections accepted on this port should use the PROXY protocol.
The listen directive can have several additional parameters specific to socket-related system calls.
-
backlog=number - sets the
backlogparameter in thelisten()call that limits the maximum length for the queue of pending connections (1.9.2). By default,backlogis set to -1 on FreeBSD, DragonFly BSD, and Mac OS X, and to 511 on other platforms. -
bind - this parameter instructs to make a separate
bind()call for a given address:port pair. The fact is that if there are severallistendirectives with the same port but different addresses, and one of thelistendirectives listens on all addresses for the given port (*:port), nginx willbind()only to*:port. It should be noted that thegetsockname()system call will be made in this case to determine the address that accepted the connection. If theipv6onlyorso_keepaliveparameters are used then for a givenaddress:portpair a separatebind()call will always be made. -
ipv6only=on|off - this parameter determines (via the
IPV6_V6ONLYsocket option) whether an IPv6 socket listening on a wildcard address[::]will accept only IPv6 connections or both IPv6 and IPv4 connections. This parameter is turned on by default. It can only be set once on start. -
reuseport - this parameter (1.9.1) instructs to create an individual listening socket for each worker process (using the
SO_REUSEPORTsocket option), allowing a kernel to distribute incoming connections between worker processes. This currently works only on Linux 3.9+ and DragonFly BSD.Inappropriate use of this option may have its security implications.
-
so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt] - this parameter configures the “TCP keepalive” behavior for the listening socket. If this parameter is omitted then the operating system’s settings will be in effect for the socket. If it is set to the value “
on”, theSO_KEEPALIVEoption is turned on for the socket. If it is set to the value “off”, theSO_KEEPALIVEoption is turned off for the socket. Some operating systems support setting of TCP keepalive parameters on a per-socket basis using theTCP_KEEPIDLE,TCP_KEEPINTVL, andTCP_KEEPCNTsocket options. On such systems (currently, Linux 2.4+, NetBSD 5+, and FreeBSD 9.0-STABLE), they can be configured using thekeepidle,keepintvl, andkeepcntparameters. One or two parameters may be omitted, in which case the system default setting for the corresponding socket option will be in effect. For example,so_keepalive=30m::10
will set the idle timeout (TCP_KEEPIDLE) to 30 minutes, leave the probe interval (TCP_KEEPINTVL) at its system default, and set the probes count (TCP_KEEPCNT) to 10 probes.
Different servers must listen on different address:port pairs.
Please login to continue.