listen指令


nginx作为一个高性能的HTTP服务器,网络的处理是其核心,了解网络的初始化有助于加深对nginx网络处理的了解。与网络有关的配置命令主要有两个:listen和sever_name。listen命令设置nginx监听地址,对于IP协议,这个地址就是address和port,对于UNIX域套接字协议,这个地址就是path,一条listen指令只能指定一个address或者port,address也可以是主机名。

一个请求进入Nginx在处理之前先要监听端口,使得Nginx与客户端建立TCP连接。监听端口的指令叫listen,是放在server配置下的,通过监听的端口或者地址。

Syntax: listen address[:port] [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

listen port [default_server] [ssl] [http2 | spdy] [proxy_protocol] [setfib=number] [fastopen=number] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

listen unix:path [default_server] [ssl] [http2 | spdy] [proxy_protocol] [backlog=number] [rcvbuf=size] [sndbuf=size] [accept_filter=filter] [deferred] [bind] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
Default:
listen *:80 | *:8000;
Context: server

 

Listen语法主要有三类


第一类:监听地址加相应的端口,nginx所在的机器上面可能有多块网卡,包括内网网卡和外网网卡,可以通过选择地址,指定server块只处理向这个地址建立连接的请求。

第二类:也可以指定端口,这个好理解比如监听80,8080,443端口.

第三类:或者也可以监听uninx socket地址,这只能用于本机通信。通过address或者port是要走内核的网络栈的,而unix path是不需要的,所以用于本地通信性能会更加好。

listen unix:/var/run/nginx.sock; 监听unix socket地址
listen 127.0.0.1:8000; 因为nginx上面可能有多个地址 端口+地址来确定一个连接


listen 127.0.0.1; 默认使用80端口
listen 8000; 监听8000端口但是没有指定地址
listen*:8000;

 

示例如下


[root@localhost ~]# ip a
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:33:e2:f0 brd ff:ff:ff:ff:ff:ff
inet 192.168.179.101/24 brd 192.168.179.255 scope global ens32
valid_lft forever preferred_lft forever
inet6 fe80::eb42:2f23:95cb:44b6/64 scope link
valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:33:e2:fa brd ff:ff:ff:ff:ff:ff
inet 192.168.179.141/24 brd 192.168.179.255 scope global dynamic ens34
valid_lft 1329sec preferred_lft 1329sec
inet6 fe80::2c95:c457:14e:92ef/64 scope link
valid_lft forever preferred_lft forever

listen 8080;
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1452/nginx: master

--------------------------------------------------------------------------------------
listen *:8080;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1452/nginx: master

-------------------------------------------------------------------------------------
listen 127.0.0.1:8080;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1452/nginx: master


--------------------------------------------------------------------------------------
listen 8080;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 1452/nginx: master


--------------------------------------------------------------------------------------
listen 192.168.179.141:8080;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 192.168.179.141:8080 0.0.0.0:* LISTEN 1452/nginx: master

[root@localhost ~]# curl -I 192.168.179.101:8080
curl: (7) Failed connect to 192.168.179.101:8080; Connection refused

[root@localhost ~]# curl -I 192.168.179.141:8080
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 20 Aug 2020 02:47:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 23 Mar 2020 12:32:23 GMT
Connection: keep-alive
ETag: "5e78ac57-264"
Accept-Ranges: bytes

----------------------------------------------------------------------------------------
listen 192.168.179.141;
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost ~]# netstat -tpln | grep 80
tcp 0 0 192.168.179.141:80 0.0.0.0:* LISTEN 1452/nginx: master