【就业班】第16周作业

1.对常用I/O模型进行比较说明

1.1I/O 模型相关概念
同步/异步:关注的是消息通信机制,即调用者在等待一件事情的处理结果时,被调用者是否提供完成状态的通知。

  同步:synchronous,被调用者并不提供事件的处理结果相关的通知消息,需要调用者主动询问事情是否处理完成
  异步:asynchronous,被调用者通过状态、通知或回调机制主动通知调用者被调用者的运行状态

阻塞/非阻塞:关注调用者在等待结果返回之前所处的状态

  阻塞:blocking,指IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情。
  非阻塞:nonblocking,指IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情。

1.2网络 I/O 模型
常用的网络I/O模型有五种,分别是阻塞型、非阻塞型、复用型、信号驱动型、异步型
1.2.1阻塞I/O模型(blocking IO)
最简单的I/O模型,用户线程在内核进行I/O操作时被阻塞

  用户线程通过系统调用read发起I/O读操作,由用户空间转到内核空间。内核等到数据包到达后,然后将接收的数据拷贝到用户空间,完成read操作。
  用户需要等待read将数据读取到buffer后,才继续处理接收的数据。整个I/O请求的过程中,用户线程是被阻塞的,这导致用户在发起I/O请求时,不能做任何事情,对CPU的资源利用率不够。
  优点:程序简单,在阻塞等待数据期间进程/线程挂起,基本不会占用 CPU 资源。
  缺点:每个连接需要独立的进程/线程单独处理,当并发请求量大时为了维护程序,内存、线程切换开销较大,apache 的preforck使用的是这种模式。

1.2.2非阻塞型 I/O 模型(nonblocking IO)

  用户线程发起I/O请求时立即返回。但并未读取到任何数据,用户线程需要不断地发起IO请求,直到数据到达后,才真正读取到数据,继续执行。是比较浪费CPU的方式,一般很少直接使用这种模型,而是在其他I/O模型中使用非阻塞IO这一特性。

1.2.3多路复用 I/O 型(I/O multiplexing)
I/O multiplexing 主要包括:select,poll,epoll三种系统调用,select/poll/epoll的好处就在于单个process就可以同时处理多个网络连接的I/O。

  它的基本原理就是select/poll/epoll这个function会不断的轮询所负责的所有socket,当某个socket有数据到达了,就通知用户进程。当用户进程调用了select,那么整个进程会被block,而同时,kernel会“监视”所有select负责的socket,当任何一个socket中的数据准备好了,select就会返回。这个时候用户进程再调用read操作,将数据从kernel拷贝到用户进程。
  优点:可以基于一个阻塞对象,同时在多个描述符上等待就绪,而不是使用多个线程(每个文件描述符一个线程),这样可以大大节省系统资源。
  缺点:当连接数较少时效率相比多线程+阻塞 I/O 模型效率较低,可能延迟更大,因为单个连接处理需要 2 次系统调用,占用时间会有增加。

IO多路复用适用如下场合:

(1)当客户端处理多个描述符时(一般是交互式输入和网络套接口),必须使用I/O复用
(2)当一个客户端同时处理多个套接字时,此情况可能的但很少出现
(3)当一个服务器既要处理监听套接字,又要处理已连接套接字,一般也要用到I/O复用
(4)当一个服务器即要处理TCP,又要处理UDP,一般要使用I/O复用
(5)当一个服务器要处理多个服务或多个协议,一般要使用I/O复用

1.2.4信号驱动式 I/O 模型 (signal-driven IO)
信号驱动I/O就是进程不用等待也不用去轮询,而是让内核在数据就绪时,发送信号通知进程。

  调用步骤:通过系统调用 sigaction ,并注册一个信号处理的回调函数,该调用会立即返回,然后主程序可以继续向下执行,当有I/O操作准备就绪,即内核数据就绪时,内核会为该进程产生一个 SIGI/O信号,并回调注册的信号回调函数,这样就可以在信号回调函数中系统调用 recvfrom 获取数据,将用户进程所需要的数据从内核空间拷贝到用户空间。

异步阻塞:程序进程向内核发送I/O调用后,不用等待内核响应,可以继续接受其他请求,内核收到进程请求后进行的I/O如果不能立即返回,就由内核等待结果,直到IO完成后内核再通知进程。

  优点:线程并没有在等待数据时被阻塞,内核直接返回调用接收信号,不影响进程继续处理其他请求因此可以提高资源的利用率。
  缺点:信号 I/O 在大量 I/O 操作时可能会因为信号队列溢出导致没法通知

1.2.5 异步 I/O 模型 (asynchronous IO)

  异步I/O 与 信号驱动I/O最大区别在于,信号驱动是内核通知用户进程何时开始一个I/O操作,而异步I/O是由内核通知用户进程I/O操作何时完成。
  相对于同步I/O,异步I/O不是顺序执行。用户进程进行aio_read系统调用之后,无论内核数据是否准备好,都会直接返回给用户进程,然后用户态进程可以去做别的事情。等到socket数据准备好了,内核直接复制数据给进程,然后从内核向进程发送通知。I/O两个阶段,进程都是非阻塞的。

异步非阻塞:程序进程向内核发送I/O调用后,不用等待内核响应,可以继续接受其他请求,内核调用的IO如果不能立即返回,内核会继续处理其他事物,直到I/O完成后将结果通知给内核,内核在将IO完成的结果返回给进程,期间进程可以接受新的请求,内核也可以处理新的事物,因此相互不影响,可以实现较大的同时并实现较高的I/O复用,因此异步非阻塞使用最多的一种通信方式。

  优点:异步 I/O 能够充分利用 DMA 特性,让 I/O 操作与计算重叠
  缺点:要实现真正的异步 I/O,操作系统需要做大量的工作。
  目前 Windows 下通过 IOCP 实现了真正的异步 I/O,在 Linux 系统下,Linux 2.6才引入,目前 AIO 并不完善,因此在 Linux 下实现高并发网络编程时以 I/O 复用模型模式+多线程任务的架构基本可以满足需求。Linux提供了AIO库函数实现异步,但是用的很少。目前有很多开源的异步I/O库,例如libevent、libev、libuv。

1.3五种 IO 对比

  这五种 I/O 模型中,越往后,阻塞越少,理论上效率也是最优前四种属于同步 I/O,因为其中真正的 I/O 操作(recvfrom)将阻塞进程/线程,只有异步 I/O 模型才与 POSIX 定义的异步 I/O 相匹配。

2.nginx中的模块分类及常见核心模块有哪些

2.1nginx模块分类

1.核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件驱动机制 、进程管理等核心功能。
2.标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响头设置 等等。
3.可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如: Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等邮件服务模块:主要用于支持 Nginx 的 邮件服务 ,包括对 POP3 协议、IMAP 协议和 SMTP协议的支持。
4.Stream服务模块: 实现反向代理功能,包括TCP协议代理。
5.第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等。

2.2常见核心模块

nginx的常见核心模块有:ngx_core、ngx_errlog、ngx_conf、ngx_events、ngx_event、ngx_epoll、ngx_regex。

3.描述nginx中worker_processes、worker_cpu_affinity、worker_rlimit_nofile、worker_connections配置项的含义

worker_processes:启动工作进程数数量
worker_cpu_affinity:将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
worker_rlimit_nofile:有worker进程能打开的文件数量上限,包括:Nginx的所有连接(例如与代理服务器的连接等),而不仅仅是与客户端的连接,另一个考虑因素是实际的并发连接数不能超过系统级别的最大打开文件数的限制。最好与ulimit -n 或者limits.conf的值保持一致。
worker_connections:设置单个nginx工作进程可以接受的最大并发数,作为web服务器的时候最大并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections * worker_processes)/2

4.编译安装nginx,实现多域名 https

4.1编译安装 Nginx
   [root@centos7 ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel
   [root@centos7 ~]#useradd -s /sbin/nologin nginx
   [root@centos7 ~]#cd /usr/local/src/
   [root@centos7 src]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
   [root@centos7 src]#tar xf nginx-1.18.0.tar.gz 
   [root@centos7 src]#cd nginx-1.18.0/
   [root@centos7 nginx-1.18.0]#./configure --prefix=/apps/nginx \
   --user=nginx \
   --group=nginx \
   --with-http_ssl_module \
   --with-http_v2_module \
   --with-http_realip_module \
   --with-http_stub_status_module \
   --with-http_gzip_static_module \
   --with-pcre \
   --with-stream \
   --with-stream_ssl_module \
   --with-stream_realip_module
   [root@centos7 nginx-1.18.0]#make && make install

   #修改权限
   [root@centos7 nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
nginx完成安装以后,有四个主要的目录,分别是conf、html、logs、sbin

  conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
  html:保存nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
  logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
  sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。

4.2验证版本及编译参数
   [root@centos7 nginx-1.18.0]#ls /apps/nginx/sbin/
   nginx
   [root@centos7 nginx-1.18.0]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
   #查看版本
   [root@centos7 ~]#nginx -v
   nginx version: nginx/1.18.0
   #查看编译参数
   [root@centos7 ~]#cd /apps/nginx/sbin/
   [root@centos7 sbin]#nginx -V
   nginx version: nginx/1.18.0
   built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC) 
   built with OpenSSL 1.1.1c FIPS  28 May 2019
   TLS SNI support enabled
   configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
4.3启动和停止 nginx 测试访问 web 界面
   #启动nginx
   [root@centos7 ~]#nginx
   #浏览器可以访问看到下面图示
   [root@centos7 ~]#ss -ntl
   State             Recv-Q           Send-Q     Local Address:Port    Peer Address:Port           
   LISTEN            0                 100             127.0.0.1:25         0.0.0.0:*              
   LISTEN            0                 128               0.0.0.0:80         0.0.0.0:*              
   LISTEN            0                 128               0.0.0.0:22         0.0.0.0:*              
   LISTEN            0                 100                 [::1]:25            [::]:*              
   LISTEN            0                 128                  [::]:22            [::]:* 
   #关闭nginx
   [root@centos7 ~]#nginx -s stop
   [root@centos7 ~]#ss -ntl
   State             Recv-Q           Send-Q   Local Address:Port   Peer  Address:Port           
   LISTEN            0                 100           127.0.0.1:25         0.0.0.0:*              
   LISTEN            0                 128             0.0.0.0:22         0.0.0.0:*              
   LISTEN            0                 100               [::1]:25            [::]:*              
   LISTEN            0                 128                [::]:22            [::]:* 
4.3创建 Nginx 自启动文件

复制同一版本的nginx的yum安装生成的service文件

   [root@centos7 ~]#vim /usr/lib/systemd/system/nginx.service 
   [Unit]
   Description=nginx - high performance web server
   Documentation=http://nginx.org/en/docs/
   After=network-online.target remote-fs.target nss-lookup.target
   Wants=network-online.target
   [Service]
   Type=forking
   PIDFile=/apps/nginx/run/nginx.pid
   ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
   ExecReload=/bin/kill -s HUP $MAINPID
   ExecStop=/bin/kill -s TERM $MAINPID
   [Install]
   WantedBy=multi-user.target
   #创建目录
   [root@centos7 ~]#mkdir /apps/nginx/run/
   #修改配置文件
   [root@centos7 ~]#vim /apps/nginx/conf/nginx.conf
   pid   /apps/nginx/run/nginx.pid;
4.4验证 Nginx 自启动文件
   [root@centos7 ~]#systemctl daemon-reload 
   [root@centos7 ~]#systemctl enable --now nginx
   Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.      
   [root@centos7 ~]#ll /apps/nginx/run/
   total 4
   -rw-r--r-- 1 root root 5 Sep 22 13:01 nginx.pid
   [root@centos7 ~]#ss -ntl
   State             Recv-Q           Send-Q Local Address:Port     Peer Address:Port           
   LISTEN            0                 100         127.0.0.1:25          0.0.0.0:* 
         
   LISTEN            0                 128           0.0.0.0:80          0.0.0.0:* 
         
   LISTEN            0                 128           0.0.0.0:22          0.0.0.0:* 
         
   LISTEN            0                 100             [::1]:25             [::]:* 
         
   LISTEN            0                 128              [::]:22             [::]:* 
   [root@centos7 ~]#systemctl stop nginx
   [root@centos7 ~]#systemctl status nginx
   ● nginx.service - The nginx HTTP and reverse proxy server
      Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset:        disabled)
      Active: inactive (dead) since Tue 2020-09-22 13:01:49 CST; 1s ago
    Process: 8113 ExecStart=/apps/nginx/sbin/nginx (code=exited,status=0/SUCCESS)
    Process: 8112 ExecStartPre=/apps/nginx/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 8110 ExecStartPre=/usr/bin/rm -f /apps/nginx/logs/nginx.pid (code=exited, status=0/SUCCESS)
    Main PID: 8115 (code=exited, status=0/SUCCESS)
   Sep 22 12:58:53 centos8.wangxiaochun.com systemd[1]: Starting The nginx HTTP and reverse proxy server..
   Sep 22 12:58:53 centos8.wangxiaochun.com nginx[8112]: nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
   Sep 22 12:58:53 centos8.wangxiaochun.com nginx[8112]: nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
   Sep 22 12:58:53 centos8.wangxiaochun.com systemd[1]: Started The nginx HTTP and reverse proxy server.
   Sep 22 13:01:49 centos8.wangxiaochun.com systemd[1]: Stopping The nginx HTTP and reverse proxy server...
   Sep 22 13:01:49 centos8.wangxiaochun.com systemd[1]: Stopped The nginx HTTP and reverse proxy server.
   [root@centos7 ~]#ss -ntl
   State             Recv-Q           Send-Q   Local Address:Port    Peer Address:Port           
   LISTEN            0                 100         127.0.0.1:25           0.0.0.0:* 
         
   LISTEN            0                 128           0.0.0.0:22           0.0.0.0:* 
         
   LISTEN            0                 100             [::1]:25              [::]:* 
         
   LISTEN            0                 128              [::]:22              [::]:* 
4.5自签名证书

自签名CA证书

   [root@centos7 ~]# cd /apps/nginx/
   [root@centos7 nginx]# mkdir certs
   [root@centos7 nginx]# cd certs/
   [root@centos7 nginx]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt #自签名CA证书
   Generating a 4096 bit RSA private key
   .................++
   .....
   Country Name (2 letter code) [XX]:CN #国家代码
   State or Province Name (full name) []:BeiJing   #省份
   Locality Name (eg, city) [Default City]:Beijing #城市名称
   Organization Name (eg, company) [Default Company Ltd]:ceshi.Ltd #公司名称   
   Organizational Unit Name (eg, section) []:ceshi #部门
   Common Name (eg, your name or your server's hostname) []:ca.ceshi.org #通用名称
   Email Address []: #邮箱
   [root@centos7 certs]# ll ca.crt 
   -rw-r--r-- 1 root root 2118 Oct 12 15:10 ca.crt

   #自制key和csr文件
   [root@centos7 certs]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.ceshi.org.key -out www.ceshi.org.csr 
   Generating a 4096 bit RSA private key
   ........................................................................++
   ......
   Country Name (2 letter code) [XX]:CN
   State or Province Name (full name) []:BeiJing
   Locality Name (eg, city) [Default City]:BeiJing
   Organization Name (eg, company) [Default Company Ltd]:ceshi.org
   Organizational Unit Name (eg, section) []:ceshi.org
   Common Name (eg, your name or your server's hostname) []:www.ceshi.org
   Email Address []:
   Please enter the following 'extra' attributes
   to be sent with your certificate request
   A challenge password []:      
   An optional company name []:
   [root@centos8 certs]# ll
   total 16
   -rw-r--r-- 1 root root 2118 Oct 12 15:10 ca.crt
   -rw-r--r-- 1 root root 3272 Oct 12 15:10 ca.key
   -rw-r--r-- 1 root root 1760 Oct 12 15:15 www.ceshi.org.csr
   -rw-r--r-- 1 root root 3272 Oct 12 15:15 www.ceshi.org.key
   #签发证书
   [root@centos7 certs]# openssl x509 -req -days 3650 -in www.ceshi.org.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.ceshi.org.crt
   #验证证书内容
   [root@centos7 certs]# openssl x509 -in www.ceshi.org.crt -noout -text 
   Certificate:
      Data:
          Version: 1 (0x0)
          Serial Number:
       bb:76:ea:fe:f4:04:ac:06
      Signature Algorithm: sha256WithRSAEncryption
          Issuer: C=CN, ST=BeiJing, L=Beijing, O=ceshi.Ltd, OU=ceshi, CN=ceshi.ca
          Validity
              Not Before: Oct 12 09:19:03 2022 GMT
              Not After : Oct 12 09:19:03 2023 GMT
          Subject: C=CN, ST=BeiJing, L=BeiJing, O=ceshi.org, OU=ceshi.org, CN=www.ceshi.org
          Subject Public Key Info:
              Public Key Algorithm: rsaEncryption
                  Public-Key: (4096 bit)

合并CA和服务器证书成一个文件,注意服务器证书在前

[root@centos7 certs]#cat www.ceshi.org.crt ca.crt > www.ceshi.org.pem

4.6实现多域名https

  Nginx 支持基于单个IP实现多域名的功能,并且还支持单IP多域名的基础之上实现HTTPS,其实是基于Nginx的 SNI(Server Name Indication)功能实现,SNI是为了解决一个Nginx服务器内使用一个IP绑定多个域名和证书的功能,其具体功能是客户端在连接到服务器建立SSL链接之前先发送要访问站点的域名(Hostname),这样服务器再根据这个域名返回给客户端一个合适的证书。

   #签名证书
   [root@centos7 certs]# openssl x509 -req -days 3650 -in m.ceshi.org.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out m.ceshi.org.crt
   [root@centos8 certs]#cat m.ceshi.org.crt ca.crt > m.ceshi.org.pem
   #Nginx 配置
   [root@centos7 certs]# vim /apps/nginx/conf/nginx.conf
   [root@centos7 certs]# tail -n 3 /apps/nginx/conf/nginx.conf
       #}
       include  /apps/nginx/conf/conf.d/*.conf;
   }
   [root@centos7 certs]# cat /apps/nginx/conf/conf.d/mobile.conf 
   server {
    listen 80 default_server;
    server_name m.ceshi.org;
    rewrite ^(.*)$ https://$server_name$1 permanent;
   }
   server {
    listen 443 ssl;
    server_name m.ceshi.org;
    ssl_certificate /apps/nginx/certs/m.ceshi.org.pem;
    ssl_certificate_key /apps/nginx/certs/m.ceshi.org.key;
    ssl_session_cache shared:sslcache:20m;
    ssl_session_timeout 10m;
    location / {
      root "/data/nginx/html/mobile";
    }
   location /mobile_status {
      stub_status;
    }
   }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 就业和全程班第十六周作业: 1、对常用I/O模型进行比较说明 服务端 I/O 流程 I/O在计算机中指Input/...
    布格雷斯阅读 268评论 1 0
  • 1、对常用I/O模型进行比较说明 I/O模型概念: 同步:synchronous,被调用者并不提供事件的处理结果相...
    铛铃叮阅读 202评论 0 0
  • 1、对常用I/O模型进行比较说明 阻塞型IO:内核的read调用是阻塞的,需要等待网卡到内核缓冲区;内核到用户缓冲...
    yabao11阅读 181评论 0 1
  • 1,编译安装nginx应用,提供wordpress服务 wgethttp://nginx.org/download...
    stephe_c阅读 413评论 0 3
  • 1、对常用I/O模型进行比较说明 一、 I/O模型相关概念同步/异步:关注的是消息通信机制,即调用者在等待一键事情...
    cherry03s阅读 328评论 0 0