linux运维篇16
一、对常用I/O模型进行比较说明
数据访问:
- 客户端发起服务器网站访问
- 服务器httpd进程接收请求
- 服务器httpd进程向内核发送数据调用请求
- 内核接收数据调用请求,将数据复制到内核空间
- 内核将内核空间的数据再调用给httpd进程(用户空间)
- httpd进程返回数据给客户端
同步:如客户端访问服务器网站,进程向内核发起请求,在内核调用磁盘的数据过程中,进程一直发送消息询问内核是否完成数据调用,这个就是同步。比如:记者一直发起问题询问被采访人。
异步:如客户端访问服务器网站,进程向内核发起请求,在内核调用磁盘的数据过程中,进程只会等待内核进行数据调用完成,不会一直询问内核,当内核数据调用完成后再通知进程。比如:微信发送消息后,发送方去做其他事情,等待别人的回复再聊天。
同步和异步的区别:注重内核在调用数据过程中的消息通知
阻塞:当客户端发起请问时,进程向内核发起请求,然后一直等待内核调用数据完成,期间进程不能做其他工作。比如年龄,年龄一直增长,但是不能倒退。
非阻塞:当客户端发起请问时,进程向内核发起请求,在等待内核调用数据过程中,进程可以去做其他事情,不会一直等待。比如:看网络视频时,遇到广告就去上洗手间一样。
阻塞和非阻塞区别:注重在内核调用数据过程中进程的状态
- 阻塞型(同步阻塞)
在这个过程中,进程会一直等待内核的数据调用,同时还会一直询问内核是否调用完成,期间不会去做其他工作。当内核空间复制完成后,httpd接收数据到用户空间,然后返回用户。httpd进程一直属于阻塞状态。
特性:数据不会丢失,数据完整性高。每个请求就会挂起一个进程,会造成系统资源的浪费,当发生大量请求时,系统容易卡死。
- 非阻塞型(同步非阻塞)
在这个过程中,进程会一直询问内核是否调用完成,但是等待期间可以去做其他事情。一旦内核空间复制完成,httpd进程需要接收数据到用户空间,然后返回数据。
特点:数据基本不会丢失,数据完整性较高。每个请求就会挂起一个进程,会造成系统资源的浪费,当发生大量请求时,系统容易卡死。
- 多路复用型(异步半阻塞)
在这个过程中,httpd进程只需要向代理进程发起代理请求后,代理进程会把请求转护给内核,询问都是代理进程进行询问,代理进程属于阻塞状态,而httpd进程可以接收更多请求。当内核空间复制完成后,httpd进程接收数据到内核空间,然后返回数据。
特性:httpd进程可以接收更多请求,适用于多个协议的不同请求,系统资源利用率较高。
- 信号驱动型(异步半阻塞)
在这个过程中,httpd进程发送信号请求给内核,内核收到请求后进行系统调用,期间httpd进程去做其他事情,不会生成询问。只有当内核空间复制完成后,内核发送信号通知httpd进程接收数据,在httpd进程接收数据过程中属于阻塞状态。
特性:httpd进程可以接收更多请求,系统资源利用率提高,但是当发生大量请求时,信号队列会有延迟,数据可能会不能及时同步。
- 异步非阻塞
在这个过程中,httpd进程接到请求后就发送请求给内核,内核收到请求后进行系统调用,期间httpd进程可以去做其他事情,不会生成询问。当用户空间复制完成后,通知httpd进程返回数据给客户。
特性:能接收大量请求,系统资源利用性高。
二、nginx中的模块分类及常见核心模块有哪些
nginx模块说明:nginx模块说明
- ngx_core 核心模块
ngx_error_log nginx日志配置
ngx_include 虚拟主机文件管理
ngx_events 事件驱动管理
ngx_worker_cpu_affinity CPU控制
ngx_worker_priority 进程优先级管理
ngx_worker_processes 启动工作进程数量配置
ngx_worker_connections 单个工作进程并发连接数配置
ngx_worker_rlimit_nofile 所有worker进程打开文件限制
ngx_thread_pool 线程管理
ngx_load_module 模块管理
- ngx_http模块
ngx_http_core_module http协议核心功能配置
ngx_http_auth_basic_module 网站访问控制
ngx_http_fastcgi_module 动态页面解析
ngx_http_gzip_module 文件压缩
ngx_http_headers_module http响应头管理
ngx_http_status_module 网站状态管理
ngx_http_proxy_module 代理转发
ngx_http_log_module 虚拟主机日志管理
ngx_http_referer_module 防盗链
ngx_http_upstream_module 负载均衡管理
ngx_http_ssl_module 网站加密
ngx_http_rewrite_module 网站重定向
- ngx_stream模块
ngx_stream_core_module 负载均衡管理
ngx_stream_access_module 网站访问控制
ngx_stream_log_module 网站访问日志管理
ngx_stream_proxy_module 反向代理转发
ngx_stream_return_module 返回值管理
ngx_stream_upstream_module 虚拟主机管理
ngx_stream_ssl_module 网站加密管理
- ngx_mail邮件模块
ngx_mail_core_module 邮件核心配置
ngx_mail_auth_http_module 认证配置
ngx_mail_proxy_module 反向代理
ngx_mail_ssl_module 加密处理
- 第三方模块
echo-nginx-module 在网页上输出自定义内容
nginx-geoip2 网站访问IP来源数据分析
三、描述nginx中worker_processes、worker_cpu_affinity、worker_rlimit_nofile、worker_connections配置项的含义
worker_processes nginx进程管理
worker_cpu_affinity CPU配置管理
worker_rlimit_nofile 所有worker进程打开文件限制
worker_connections 单个工作进程并发连接数配置
四、编译安装nginx,实现多域名 https
nginx下载:nginx下载
代理拓扑:
- web1配置
http编译看这里:httpd编译
用yum安装也可以
yum install -y httpd
vi /etc/hosts
192.168.116.132 nginx.test1.io
echo nginx.test1.io-192.168.116.132>/var/www/html/index.html
systemctl start httpd
- web2配置
yum install -y httpd
vi /etc/hosts
192.168.116.133 nginx.test2.io
echo nginx.test2.io-192.168.116.133>/var/www/html/index.html
systemctl start httpd
- nginx(192.168.116.130)配置
编译安装nginx:
wget http://nginx.org/download/nginx-1.18.0.tar.gz
yum install -y gcc pcre-devel openssl-devel zlib-devel 安装依赖包
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0
useradd -r -s /sbin/nologin nginx 设置nginx账户
编译安装
./configure --prefix=/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
make && make install
echo 'PATH=/nginx/sbin:$PATH' >/etc/profile.d/nginx.sh
source /etc/profile.d/nginx.sh
mkdir /nginx/run
vi /nginx/conf/nginx.conf
pid /nginx/run/nginx.pid; 修改PID这行
chown -R nginx.nginx /nginx
配置service
vi /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=/nginx/run/nginx.pid
ExecStart=/nginx/sbin/nginx -c /nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
systemctl daemon-reload 加载service文件
证书原理链接:ssl证书配置
证书配置
mkdir /nginx/conf/ssl/
cd /nginx/conf/ssl
vi ssh.sh 利用脚本生成test1和test2证书,ca文件可以不用改
#!/bin/bash
#设置CA
CA_SUBJECT="/O=heaven/CN=io" #第2个证书这里不用改
#设置客户端名称
SUBJECT="/C=CN/ST=Beijing/L=Beijing/O=nginx/CN=nginx.test1.io"
#设置证书文件名
FILE=nginx.test1.io
KEY_SIZE=2048
SERIAL=34
SERIAL2=35
CA_EXPIRE=202002
EXPIRE=365
#生成自签名的CA证书
openssl req -x509 -newkey rsa:${KEY_SIZE} -subj $CA_SUBJECT -keyout ca.key -nodes -days $CA_EXPIRE -out ca.crt
#客户端生成私钥和证书申请
openssl req -newkey rsa:${KEY_SIZE} -nodes -keyout ${FILE}.key -subj $SUBJECT -out ${FILE}.csr
#颁发证书
openssl x509 -req -in ${FILE}.csr -CA ca.crt -CAkey ca.key -set_serial $SERIAL -days $EXPIRE -out ${FILE}.crt
chmod 600 *.key
bash ssl.sh 开始生成证书
将客户端的证书和ca的证书合并成nginx需要的证书
cat nginx.test1.io.crt ca.crt >nginx.test1.io.ssl.crt
cat nginx.test2.io.crt ca.crt >nginx.test2.io.ssl.crt
开始配置虚拟主机
修改hosts,将域名转发到web服务器
vi /etc/hosts
192.168.116.132 nginx.test1.io
192.168.116.133 nginx.test2.io
nginx虚拟主机配置
mkdir /nginx/conf.d
vi /nginx/conf/nginx.conf 删除其他,留下这些
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /nginx/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /nginx/conf.d/*.conf;
}
配置虚拟主机1
vi /nginx/conf.d/nginx.test1.conf
server {
listen 80;
listen 443 ssl;
server_name nginx.test1.io;
ssl_certificate /nginx/conf/ssl/nginx.test1.io.ssl.crt;
ssl_certificate_key /nginx/conf/ssl/nginx.test1.io.key;
location / {
proxy_pass http://192.168.116.132/;
index index.html;
}
}
配置虚拟主机2
vi /nginx/conf.d/nginx.test2.conf
server {
listen 80;
listen 443 ssl;
server_name nginx.test2.io;
ssl_certificate /nginx/conf/ssl/nginx.test2.io.ssl.crt;
ssl_certificate_key /nginx/conf/ssl/nginx.test2.io.key;
location / {
proxy_pass http://192.168.116.133/;
index index.html;
}
}
systemctl start nginx 启动服务
- 客户端测试
vi /etc/hosts 修改hosts地址为nginx的地址,要不然解析不了网站
192.168.116.130 nginx.test1.io
192.168.116.130 nginx.test2.io
curl http://nginx.test1.io/
curl http://nginx.test2.io/
curl -k https://nginx.test1.io/
curl -k https://nginx.test2.io/