| Syntax: | listen
listen
listen
|
|---|---|
| Default: | listen *:80 | *:8000; |
| Context: | server |
Sets the address and port for IP, or the path for a UNIX-domain socket on which the server will accept requests. Both address and port, or only address or only port can be specified. An address may also be a hostname, for example:
listen 127.0.0.1:8000; listen 127.0.0.1; listen 8000; listen *:8000; listen localhost:8000;
IPv6 addresses (0.7.36) are specified in square brackets:
listen [::]:8000; listen [::1];
UNIX-domain sockets (0.8.21) are specified with the “unix:” prefix:
listen unix:/var/run/nginx.sock;
If only address is given, the port 80 is used.
If the directive is not present then either *:80 is used if nginx runs with the superuser privileges, or *:8000 otherwise.
The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair. If none of the directives have the default_server parameter then the first server with the address:port pair will be the default server for this pair.
In versions prior to 0.8.21 this parameter is named simply default.
The ssl parameter (0.7.14) allows specifying that all connections accepted on this port should work in SSL mode. This allows for a more compact configuration for the server that handles both HTTP and HTTPS requests.
The http2 parameter (1.9.5) configures the port to accept HTTP/2 connections. Normally, for this to work the ssl parameter should be specified as well, but nginx can also be configured to accept HTTP/2 connections without SSL.
The spdy parameter (1.3.15-1.9.4) allows accepting SPDY connections on this port. Normally, for this to work the ssl parameter should be specified as well, but nginx can also be configured to accept SPDY connections without SSL.
The proxy_protocol parameter (1.5.12) 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. These parameters can be specified in any listen directive, but only once for a given address:port pair.
In versions prior to 0.8.21, they could only be specified in thelistendirective together with thedefaultparameter.
-
setfib=number - this parameter (0.8.44) sets the associated routing table, FIB (the
SO_SETFIBoption) for the listening socket. This currently works only on FreeBSD. -
fastopen=number - enables “TCP Fast Open” for the listening socket (1.5.8) and limits the maximum length for the queue of connections that have not yet completed the three-way handshake.
Do not enable this feature unless the server can handle receiving the same SYN packet with data more than once.
-
backlog=number - sets the
backlogparameter in thelisten()call that limits the maximum length for the queue of pending connections. By default,backlogis set to -1 on FreeBSD, DragonFly BSD, and Mac OS X, and to 511 on other platforms. -
rcvbuf=size - sets the receive buffer size (the
SO_RCVBUFoption) for the listening socket. -
sndbuf=size - sets the send buffer size (the
SO_SNDBUFoption) for the listening socket. -
accept_filter=filter - sets the name of accept filter (the
SO_ACCEPTFILTERoption) for the listening socket that filters incoming connections before passing them toaccept(). This works only on FreeBSD and NetBSD 5.0+. Possible values are dataready and httpready. -
deferred - instructs to use a deferred
accept()(theTCP_DEFER_ACCEPTsocket option) on Linux. -
bind - instructs to make a separate
bind()call for a givenaddress:portpair. This is useful because 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 thesetfib,backlog,rcvbuf,sndbuf,accept_filter,deferred,ipv6only, orso_keepaliveparameters are used then for a givenaddress:portpair a separatebind()call will always be made. -
ipv6only=on|off - this parameter (0.7.42) 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.Prior to version 1.3.4, if this parameter was omitted then the operating system’s settings were in effect for the socket.
-
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 (1.1.11) 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.
Example:
listen 127.0.0.1 default_server accept_filter=dataready backlog=1024;
Please login to continue.