Nginx listen指令处理连接请求
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] [| 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] [| 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] [| 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 a2: ens32: 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 forever3: ens34: 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 foreverlisten 8080;[root@localhost ~]# netstat -tpln | grep 80tcp 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 80tcp 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 80tcp 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 80tcp 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 80tcp 0 0 192.168.179.141:8080 0.0.0.0:* LISTEN 1452/nginx: master [root@localhost ~]# curl -I 192.168.179.101:8080curl: (7) Failed connect to 192.168.179.101:8080; Connection refused[root@localhost ~]# curl -I 192.168.179.141:8080HTTP/1.1 200 OKServer: nginx/1.16.1Date: Thu, 20 Aug 2020 02:47:33 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Mon, 23 Mar 2020 12:32:23 GMTConnection: keep-aliveETag: "5e78ac57-264"Accept-Ranges: bytes----------------------------------------------------------------------------------------listen 192.168.179.141;[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload[root@localhost ~]# netstat -tpln | grep 80tcp 0 0 192.168.179.141:80 0.0.0.0:* LISTEN 1452/nginx: master
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~