编译安装Nginx及Nginx模块
1. I/O(Input Output)模型相关概念
Linux 的 I/O
-
磁盘I/O 磁盘I/O是进程向内核发起系统调用,请求磁盘上的某个资源比如是html 文件或者图片,然后内核通过相应的驱动程序将目标文件加载到内核的内存空间,加载完成之后把数据从内核内存再复制给进程内存,如果是比较大的数据也需要等待时间
-
网络I/O : 一切皆文件,本质为对socket文件的读写 网络通信就是网络协议栈到用户空间进程的IO就是网络IO
-
同步/异步(消息反馈机制)
- 同步:被调用者不会主动通知调用者是否完成
- 异步:被调用者主动通知调用者是否完成
-
阻塞/非阻塞:关注调用者在等待结果返回之前所处的状态
- 阻塞:blocking,IO操作需要彻底完成后才返回到用户空间,调用结果返回之前,调用者被挂起,干不了别的事情
- 非阻塞:nonblocking,被IO操作被调用后立即返回给用户一个状态值,而无需等到IO操作彻底完成,在最终的调用结果返回之前,调用者不会被挂起,可以去做别的事情
异步非阻塞最好
select | poll | epoll | |
---|---|---|---|
操作方式 | 遍历 | 遍历 | 回调 |
底层实现 | 数组 | 链表 | 哈希表 |
IO效率 | 每次调用都进行线性遍历、时间复杂度为O(n) | 同左 | 事件通知方式,每当fd就绪,系统注册的回调函数就会被调用,将就绪的fd放到rdlllist里,时间复杂度O(1) |
最大连接数 | 1024(x86)2048(x86) | 无上限 | 无上限 |
fd拷贝 | 每次调用select都需要把fd集合从用户拷贝到内核态 | 每次调用poll,都需要把fd集合从用户态拷贝到内核态 | 调用epoll_ct时拷贝进内核并保存,之后每次epoll_wait不拷贝 |
epoll是poll的升级版
select不如poll
- apache使用select:处理方式遍历,打开文件最大值1024
- nginx使用epoll:处理方式回调,打开文件最大值,理论上无上限
2. Nginx
2.1 Nginx概述
Nginx 功能介绍
-
静态的web资源服务器html,图片,js,css,txt等静态资源
-
http/https协议的反向代理 7层(URL)
-
结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
-
tcp/udp协议的请求转发(反向代理) 4层(IP地址和端口号)
-
一款高性能、轻量级web服务软件
- 稳定性高
- 系统资源消耗低
- 对HTTP并发连接的处理能力高
- 单台物理服务器可支持30000-50000个并发请求
基础特性
- 模块化设计,较好的扩展性
- 高可靠性
- 支持热部署:不停机更新配置文件,升级版本,更换日志文件
- 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
- event-driven,aio(支持异步),mmap(零拷贝),sendfile(零拷贝)
主机之间通讯:主机的IP地址和进程的端口号
套接字Socket:允许位于不同主机(或同一主机)上不同进程之间进行通信和数据交换,进程间的传输要有两个标志:主机IP地址和进程的端口号,合称为套接字地址Socket address
管道符同一主机
[root@localhost ~]#cd /
[root@localhost /]#mkdir data
[root@localhost /]#ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@localhost /]#cd /data
[root@localhost data]#ls
[root@localhost data]#rz -E
rz waiting to receive.
[root@localhost data]#tar xf nginx-1.18.0_.tar.gz
[root@localhost data]#ls
nginx-1.18.0 nginx-1.18.0_.tar.gz
[root@localhost data]#
[root@localhost data]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]#cd src/
[root@localhost src]#find -name *.c
[root@localhost src]#find -name *.c | cat | wc -l
205
[root@localhost src]#find -name *.c | xargs -n1 cat | wc -l
179605
#xargs 一般是和管道一起使用,xargs(英文全拼: eXtended ARGuments)是给命令传递参数的一个过滤器,也是组合多个命令的一个工具
#xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据
#xargs 也可以将单行或多行文本输入转换为其他格式,例如多行变单行,单行变多行
2.2 Nginx原理
1. 当用户发起http请求,请求一个index.html网页文件
2. 客户端请求与服务器端建立连接
3. 建立连接后,会发送请求报文
4. 服务端的网卡收到请求报文,会将该报文复制到内核空间
5. 内核空间分析报文后交给对应的程序
6. nginx分析该报文,将报文和自己的配置文件,一一比对,按照配置文件完成请求
7. 分析后发现,客户需要index.html
8. 由于程序的权限问题,没有资格直接调用磁盘上的文件,程序会再将这个请求,再次转发给内核
9. 内核得到后,去磁盘上找文件
10. 找到文件后,复制给程序
11. 程序会构建响应报文,构建好后再交给内核空间
12. 内核空间得到响应报文后,再交给网卡发给客户
2.3 Nginx模块
- 核心模块:是 Nginx 服务器正常运行必不可少的模块,提供错误日志记录 、配置文件解析 、事件驱动机制 、进程管理等核心功能
- 标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等等
- 可选HTTP模块:主要用于扩展标准的 HTTP 功能,让 Nginx 能处理一些特殊的服务,比如:Flash 多媒体传输 、解析 GeoIP 请求、 网络传输压缩 、 安全协议 SSL 支持等
- 邮件服务模块:主要用于支持 Nginx 的邮件服务 ,包括对 POP3 协议、 IMAP 协议和 SMTP协议的支持
- Stream服务模块:实现反向代理功能,包括TCP协议代理
- 第三方模块:是为了扩展 Nginx 服务器应用,完成开发者自定义功能,比如: Json 支持、 Lua 支持等
3. yum安装Nginx
centos7 需要安装epel源
[root@localhost ~]#yum install epel-release.noarch -y
#安装epel源 额外源
yum install nginx -y
使用官方源安装较新版本
http://nginx.org/en/linux_packages.html#RHEL
[root@localhost ~]#nginx -v
nginx version: nginx/1.20.1
[root@localhost ~]#nginx -V
[root@localhost ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]#nginx -?
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help #帮助
-v : show version and exit #显示当前文本
-V : show version and configure options then exit #显示安装配置
-t : test configuration and exit #测试配置,显示问题
-T : test configuration, dump it and exit #测试配置,显示问题
-q : suppress non-error messages during configuration testing #退出
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号(nginx -s reload)
-p prefix : set prefix path (default: /usr/share/nginx/) #指定运行目录
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #使用指定配置文件
-g directives : set global directives out of configuration file #指定配置指令,以后面配置生效,不以默认配置文件生效
4. 编译安装Nginx
[root@localhost ~]#systemctl stop firewalld.service
[root@localhost ~]#setenforce 0
[root@localhost ~]#cd /opt
[root@localhost opt]#wget http://nginx.org/download/nginx-1.18.0.tar.gz
#官网下载安装包
[root@localhost opt]#yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
#安装依赖包
[root@localhost opt]#useradd -M -s /sbin/nologin nginx
#新建nginx用户便于管理
[root@localhost opt]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost opt]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]#cd conf
[root@localhost conf]#ls
fastcgi.conf fastcgi_params koi-utf koi-win mime.types nginx.conf scgi_params uwsgi_params win-utf
[root@localhost nginx-1.18.0]#cd contrib/
[root@localhost contrib]#ls
geo2nginx.pl README unicode2nginx vim
contrib:vim 格式文件,修改nginx配置文件的格式,高亮
cp -r /opt/nginx-1.18.0/contrib/vim/* /usr/share/vim/vimfiles/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]#cd man/
[root@localhost man]#ls
nginx.8
[root@localhost man]#man ./nginx.8
#相对路径查看man帮助
[root@localhost man]#pwd #查看路径
/opt/nginx-1.18.0/man
[root@localhost man]#man /opt/nginx-1.18.0/man/nginx.8
#绝对路径查看man帮助
[root@localhost man]#cd
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost opt]#cd nginx-1.18.0/
[root@localhost 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
./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@localhost nginx-1.18.0]#make -j2 && make install
[root@localhost nginx-1.18.0]#chown -R nginx.nginx /apps/nginx
[root@localhost nginx-1.18.0]#cd /apps/
[root@localhost apps]#ls
nginx
[root@localhost apps]#cd nginx/
[root@localhost nginx]#ls #查看四大文件
conf html logs sbin
#配置文件 网页文件 日志文件 可执行文件
5. 配置详细解释
nginx 官方帮助文档:http://nginx.org/en/docs/
tengine 帮助文档:http://tengine.taobao.org/nginx_docs/cn/docs/
Nginx的配置文件的组成部分:
主配置文件:nginx.conf
子配置文件: include conf.d/*.conf
配置文件由指令和指令块构成
每条指令以;分号结尾,指令与值之间以空格符号分隔
pid /apps/run/nginx.pid
指令已{}达括号将多条指令组织在一起且可以嵌套指令块
include语句允许组合多个配置文件以提升可维护性
#号注释
$使用变量
部分支持正则
自定义变量:由用户使用set命令定义,格式: set variable_name value
5.1 修改worker启动的进程数
[root@localhost ~]#cd /apps/nginx/
[root@localhost nginx]#ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]#cd conf/
[root@localhost conf]#ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@localhost conf]#vim nginx.conf
#worker_processes auto; #worker进程数量改成auto
[root@localhost conf]#nginx -s reload #修改好worker进程数量,重新加载
[root@localhost conf]#lscpu | grep -i cpu
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 2
On-line CPU(s) list: 0,1
CPU 系列: 6
型号名称: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
CPU MHz: 1799.483
NUMA 节点0 CPU: 0,1
[root@localhost conf]#ps aux | grep -v grep | grep nginx #有几个cpu就有几个worker
root 70544 0.0 0.1 46296 2032 ? Ss 16:01 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 70789 0.0 0.1 48808 2132 ? S 16:27 0:00 nginx: worker process
nginx 70790 0.0 0.1 48808 2132 ? S 16:27 0:00 nginx: worker process
5.2 信号
- nginx 当做单独命令使用有以下选项
[root@localhost nginx]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@localhost nginx]#nginx -v #查看版本号
nginx version: nginx/1.18.0
[root@localhost nginx]#nginx -V #显示编译安装的配置
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
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
[root@localhost nginx]#nginx -t #检测配置文件是否有问题
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
Options:
-?,-h : this help #帮助
-v : show version and exit #显示当前文本
-V : show version and configure options then exit #显示安装配置
-t : test configuration and exit #测试配置,显示问题
-T : test configuration, dump it and exit #测试配置,显示问题
-q : suppress non-error messages during configuration testing #退出
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号(nginx -s reload)
-p prefix : set prefix path (default: /usr/share/nginx/) #指定加载目录
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #使用指定配置文件
-g directives : set global directives out of configuration file #指定配置指令,以后面配置生效,不以默认配置文件生效
[root@localhost ~]#man /opt/nginx-1.18.0/man/nginx.8
[root@localhost ~]#nginx -c / #指明加载配置文件
[root@localhost ~]#nginx -? #代表帮助
[root@localhost ~]#nginx -h
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
#配置文件中以nobody身份运行
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/logs/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@localhost ~]#systemctl daemon-reload
[root@localhost ~]#systemctl start nginx.service
[root@localhost ~]#ss -natp | grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=58836,fd=6),("nginx",pid=58835,fd=6))
[root@localhost ~]#ps aux | grep nginx
root 58835 0.0 0.0 46156 1160 ? Ss 17:18 0:00 nginx: master process /apps/nginx/sbin/nginx -c /a
nginx 58836 0.0 0.1 48688 2004 ? S 17:18 0:00 nginx: worker process
root 58911 0.0 0.0 112676 984 pts/1 S+ 17:21 0:00 grep --color=auto nginx
[root@localhost ~]#systemctl stop nginx
- nginx -g 指定配置,不以配置文件中的为准
- nginx默认以后台方式运行
[root@localhost ~]#nginx -g 'daemon off;' #前台运行命令,一般默认后台,不以前台方式运行
[root@localhost ~]#nginx -g 'user lj;' #以lj身份运行,默认是以nginx身份
[root@localhost ~]#ps aux | grep nginx
root 58976 0.0 0.0 46148 1168 ? Ss 17:27 0:00 nginx: master process nginx -g user lj;
lj 58977 0.0 0.1 48600 2016 ? S 17:27 0:00 nginx: worker process
root 58979 0.0 0.0 112676 984 pts/1 S+ 17:27 0:00 grep --color=auto nginx
5.3 发送信号
kill -l 看信号大全
nginx -h 中可以看到的信号较少
s signal : send signal to a master process: stop, quit, reopen, reload
可以使用man手册来查看详细的信号 如果没安装,去源码包里找到man文件
man 路径/nginx.8 不加路径打不开man帮助
stop SIGTERM 直接停止
quit SIGQUIT 优雅的退出:有人在访问不会结束进程
reopen SIGUSR1 分割日志
reload SIGHUP 重新加载配置文件
SIGHUP Reload configuration, start the new worker process with a new configuration, and
gracefully shut down old worker processes.
SIGQUIT Shut down gracefully. 优雅的关闭:有人在访问不会结束进程
SIGUSR1 Reopen log files. 重新分割日志
SIGUSR2 Upgrade the nginx executable on the fly. 运行中升级
SIGWINCH Shut down worker processes gracefully. 优雅的关闭worker进程,work进程负责处理请求,还有请求不会关闭
kill -s USR1 进程号
#kill要跟进程
nginx -s Reopen
#nginx -s 不能跟进程
选项:
nginx -s stop #立即关闭nginx
nginx -s quit #优雅退出 不影响业务的状态下退出
nginx -s reload #重新加载
5.4 USR1分割日志
[root@localhost ~]#nginx -s reload #重新加载配置文件
[root@localhost ~]#cd /apps/nginx
[root@localhost nginx]#ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@localhost nginx]#cd logs/
[root@localhost logs]#ls
access.log error.log nginx.pid
[root@localhost logs]#mv access.log access-24.log #移动日志
[root@localhost logs]#ls
access-24.log error.log nginx.pid
[root@localhost logs]#touch access.log #新建日志
[root@localhost logs]#ls
access-24.log access.log error.log nginx.pid
[root@localhost logs]#tail -f access.log
^C
[root@localhost logs]#tail -f /apps/nginx/logs/access-24.log
192.168.8.106 - - [24/Aug/2023:18:52:19 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
[root@localhost nginx-1.18.0]#cd /apps/nginx/logs/
[root@localhost logs]#tail -f access.log
[root@localhost ~]#curl 192.168.8.100
[root@localhost logs]#cat /apps/nginx/logs/nginx.pid #查看nginx进程号
59135
[root@localhost logs]#kill -s USR1 59135 #发送信号
[root@localhost ~]#curl 192.168.8.100
[root@localhost logs]#tail -f access.log
192.168.8.106 - - [24/Aug/2023:18:59:58 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0"
5.5 quit优雅退出
[root@localhost logs]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=10
记录了10+0 的读入
记录了10+0 的写出
10737418240字节(11 GB)已复制,68.3601 秒,157 MB/秒
[root@localhost logs]#pstree -p | grep nginx
|-nginx(59135)---nginx(59197)
[root@localhost logs]#nginx -s quit
[root@localhost logs]#pstree -p | grep nginx
|-nginx(59135)---nginx(59197)
[root@localhost ~]#wget --limit-rate=1M http://192.168.8.100/m.img
--2023-08-24 19:25:54-- http://192.168.8.100/m.img
正在连接 192.168.8.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:7516192768 (7.0G) [application/octet-stream]
正在保存至: “m.img”
2% [> ] 167,652,632 1.00MB/s 剩余 2h 4m
^C
[root@localhost ~]#wget --limit-rate=1M http://192.168.8.100/m.img
--2023-08-24 19:25:54-- http://192.168.8.100/m.img
正在连接 192.168.8.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:7516192768 (7.0G) [application/octet-stream]
正在保存至: “m.img”
2% [> ] 167,652,632 1.00MB/s 剩余 2h 4m
[root@localhost logs]#pstree -p | grep nginx
[root@localhost logs]#pstree -p | grep nginx
5.6 关闭版本
#修改配置文件放在http语句中
http {
server_tokens off;
}
[root@localhost conf]#vim nginx.conf
[root@localhost conf]#nginx -s reload
[root@localhost conf]#nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost data]#curl -I 192.168.8.100
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 25 Aug 2023 08:36:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
5.7 修改名字
#去修改源码,在安装包里, 再重新编译 #号不要去掉
#先把服务关闭,不然编译不成功
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost opt]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.18.0]#systemctl stop nginx #重新编译需要先关闭
[root@localhost nginx-1.18.0]#cd src/
[root@localhost src]#ls
core event http mail misc os stream
[root@localhost src]#cd core/
[root@localhost core]#ls
nginx.c ngx_crc32.c ngx_list.c ngx_palloc.c ngx_rbtree.h ngx_spinlock.c
nginx.h ngx_crc32.h ngx_list.h ngx_palloc.h ngx_regex.c ngx_string.c
ngx_array.c ngx_crc.h ngx_log.c ngx_parse.c ngx_regex.h ngx_string.h
ngx_array.h ngx_crypt.c ngx_log.h ngx_parse.h ngx_resolver.c ngx_syslog.c
ngx_buf.c ngx_crypt.h ngx_md5.c ngx_parse_time.c ngx_resolver.h ngx_syslog.h
ngx_buf.h ngx_cycle.c ngx_md5.h ngx_parse_time.h ngx_rwlock.c ngx_thread_pool.c
ngx_conf_file.c ngx_cycle.h ngx_module.c ngx_proxy_protocol.c ngx_rwlock.h ngx_thread_pool.h
ngx_conf_file.h ngx_file.c ngx_module.h ngx_proxy_protocol.h ngx_sha1.c ngx_times.c
ngx_config.h ngx_file.h ngx_murmurhash.c ngx_queue.c ngx_sha1.h ngx_times.h
ngx_connection.c ngx_hash.c ngx_murmurhash.h ngx_queue.h ngx_shmtx.c
ngx_connection.h ngx_hash.h ngx_open_file_cache.c ngx_radix_tree.c ngx_shmtx.h
ngx_core.h ngx_inet.c ngx_open_file_cache.h ngx_radix_tree.h ngx_slab.c
ngx_cpuinfo.c ngx_inet.h ngx_output_chain.c ngx_rbtree.c ngx_slab.h
[root@localhost core]#vim nginx.h
13 #define NGINX_VERSION "9527"
14 #define NGINX_VER "oop/" NGINX_VERSION
[root@localhost core]#cd ../
[root@localhost src]#ls
core event http mail misc os stream
[root@localhost src]#cd http/
[root@localhost http]#ls
modules ngx_http_header_filter_module.c ngx_http_upstream.c
ngx_http.c ngx_http_parse.c ngx_http_upstream.h
ngx_http_cache.h ngx_http_postpone_filter_module.c ngx_http_upstream_round_robin.c
ngx_http_config.h ngx_http_request_body.c ngx_http_upstream_round_robin.h
ngx_http_copy_filter_module.c ngx_http_request.c ngx_http_variables.c
ngx_http_core_module.c ngx_http_request.h ngx_http_variables.h
ngx_http_core_module.h ngx_http_script.c ngx_http_write_filter_module.c
ngx_http_file_cache.c ngx_http_script.h v2
ngx_http.h ngx_http_special_response.c
[root@localhost http]#vim ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: wwe" CRLF;
[root@localhost ~]#cd /opt
[root@localhost opt]#ls
nginx-1.18.0 nginx-1.18.0.tar.gz
[root@localhost opt]#cd nginx-1.18.0/
[root@localhost nginx-1.18.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost 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@localhost nginx-1.18.0]#make -j2 && make install
[root@localhost nginx-1.18.0]#systemctl start nginx #重新启动
[root@localhost nginx-1.18.0]#vim /apps/nginx/conf/nginx.conf
[root@localhost nginx-1.18.0]#nginx -s reload
[root@localhost data]#curl -I 192.168.8.100 #显示oop
HTTP/1.1 200 OK
Server: oop/9527
Date: Fri, 25 Aug 2023 09:31:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
[root@localhost core]#vim /apps/nginx/conf/nginx.conf
[root@localhost core]#nginx -s reload
[root@localhost data]#curl -I 192.168.8.100 #显示wew
HTTP/1.1 200 OK
Server: wew
Date: Fri, 25 Aug 2023 09:31:31 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
5.8 平滑升级
[root@localhost ~]#pstree -p | grep nginx #查看nginx线程
[root@localhost ~]#systemctl start nginx #启动nginx
[root@localhost ~]#pstree -p | grep nginx
|-nginx(60850)---nginx(60851)
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf #进入nginx配置文件修改
[root@localhost ~]#nginx -s reload #重新加载nginx
[root@localhost ~]#pstree -p | grep nginx #有2个进程
|-nginx(60850)-+-nginx(61027)
| `-nginx(61028)
[root@localhost ~]#curl -I 192.168.8.100
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 06:46:58 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
[root@localhost ~]#ps aux | grep nginx
root 60850 0.0 0.1 46296 2028 ? Ss 14:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 61027 0.0 0.1 48808 2376 ? S 14:25 0:00 nginx: worker process
nginx 61028 0.0 0.1 48808 2128 ? S 14:25 0:00 nginx: worker process
root 62009 0.0 0.0 112676 980 pts/1 S+ 14:47 0:00 grep --color=auto nginx
[root@localhost ~]#cd /usr/local/src/
[root@localhost src]#ls
[root@localhost src]#wget http://nginx.org/download/nginx-1.22.0.tar.gz
[root@localhost src]#tar xf nginx-1.22.0.tar.gz
[root@localhost src]#ls
nginx-1.22.0 nginx-1.22.0.tar.gz
[root@localhost src]#cd nginx-1.22.0/
[root@localhost nginx-1.22.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.22.0]#nginx -V #显示编译安装配置
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
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
[root@localhost nginx-1.22.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@localhost nginx-1.22.0]#make #make生成二进制文件(注意不做make install操作,会覆盖原来的版本,手动升级)
[root@localhost ~]#curl -I 192.168.8.100 #版本还是1.18
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 07:06:51 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
[root@localhost nginx-1.22.0]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@localhost nginx-1.22.0]#cd objs/
[root@localhost objs]#ls
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@localhost objs]#./nginx -v
nginx version: nginx/1.22.0 #版本为新的1.22,将老版本1.18做一个备份
[root@localhost objs]#cd /apps/nginx/sbin/
[root@localhost sbin]#ls
nginx
[root@localhost sbin]#mv nginx nginx.bak #将低版本的nginx主程序改名
[root@localhost sbin]#ls #查看 低版本nginx改为名nginx.bak
nginx.bak
[root@localhost sbin]#cd - #切换上一级目录
/usr/local/src/nginx-1.22.0/objs
[root@localhost objs]#ls #查看
autoconf.err Makefile nginx nginx.8 ngx_auto_config.h ngx_auto_headers.h ngx_modules.c ngx_modules.o src
[root@localhost objs]#cp nginx /apps/nginx/sbin/ #将objs下的nginx复制到sbin文件下
[root@localhost objs]#cd /apps/nginx/sbin/ #进入sbin
[root@localhost sbin]#ls #查看 nginx为复制进来的新版本,nginx.bak为备份的旧版本
nginx nginx.bak
[root@localhost sbin]#cat /apps/nginx/logs/nginx.pid
60850
[root@localhost sbin]#kill -USR2 60850
#发送2信号
[root@localhost sbin]#ps aux | grep nginx
root 60850 0.0 0.1 46296 1980 ? Ss 14:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 61027 0.0 0.1 48808 2352 ? S 14:25 0:00 nginx: worker process
nginx 61028 0.0 0.1 48808 2352 ? S 14:25 0:00 nginx: worker process
root 69899 0.0 0.1 46176 3344 ? S 15:29 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 69900 0.0 0.1 48712 2004 ? S 15:29 0:00 nginx: worker process
nginx 69901 0.0 0.1 48712 2004 ? S 15:29 0:00 nginx: worker process
root 69964 0.0 0.0 112676 980 pts/1 S+ 15:31 0:00 grep --color=auto nginx
[root@localhost ~]#curl -I 192.168.8.100 #版本已升级成1.22
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Fri, 25 Aug 2023 07:33:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
[root@localhost sbin]#ls ../logs
access-24.log access.log error.log nginx.pid nginx.pid.oldbin
[root@localhost sbin]#cat ../logs/nginx.pid
69899
[root@localhost sbin]#cat ../logs/nginx.pid.oldbin
60850
[root@localhost data]#wget --limit-rate=1M http://192.168.8.100/m.img
[root@localhost sbin]#kill -WINCH `cat /apps/nginx/logs/nginx.pid.oldbin`
#优雅关闭老进程worker进程
[root@localhost sbin]#ps aux | grep nginx #查看nginx进程,少了一个worker进程
root 60850 0.0 0.0 46296 1796 ? Ss 14:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 69899 0.0 0.1 46176 3344 ? S 15:29 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 69900 0.1 0.1 48712 2252 ? S 15:29 0:01 nginx: worker process
nginx 69901 0.0 0.1 48712 2004 ? S 15:29 0:00 nginx: worker process
root 70342 0.0 0.0 112676 984 pts/1 S+ 15:45 0:00 grep --color=auto nginx
[root@localhost data]#curl -I 192.168.8.100 #版本永远为1.22,升级成功
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Fri, 25 Aug 2023 07:48:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
5.9 回滚
[root@localhost sbin]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
[root@localhost sbin]#ps aux | grep nginx
root 60850 0.0 0.0 46296 1840 ? Ss 14:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 69899 0.0 0.1 46176 3344 ? S 15:29 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 69900 0.1 0.1 48712 2252 ? S 15:29 0:01 nginx: worker process
nginx 69901 0.0 0.1 48712 2004 ? S 15:29 0:00 nginx: worker process
nginx 70403 0.0 0.1 48808 2112 ? S 15:52 0:00 nginx: worker process
nginx 70404 0.0 0.1 48808 2108 ? S 15:52 0:00 nginx: worker process
nginx 70414 0.0 0.1 48808 2116 ? S 15:52 0:00 nginx: worker process
nginx 70415 0.0 0.1 48808 2116 ? S 15:52 0:00 nginx: worker process
root 70417 0.0 0.0 112676 980 pts/1 S+ 15:53 0:00 grep --color=auto nginx
[root@localhost sbin]#kill -QUIT `cat /apps/nginx/logs/nginx.pid`
[root@localhost sbin]#ps aux | grep nginx
root 60850 0.0 0.0 46296 1840 ? Ss 14:20 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 70403 0.0 0.1 48808 2112 ? S 15:52 0:00 nginx: worker process
nginx 70404 0.0 0.1 48808 2108 ? S 15:52 0:00 nginx: worker process
nginx 70414 0.0 0.1 48808 2116 ? S 15:52 0:00 nginx: worker process
nginx 70415 0.0 0.1 48808 2116 ? S 15:52 0:00 nginx: worker process
root 70452 0.0 0.0 112676 980 pts/1 S+ 15:57 0:00 grep --color=auto nginx
[root@localhost data]#curl -I 192.168.8.100
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Fri, 25 Aug 2023 07:57:30 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 24 Aug 2023 08:38:21 GMT
Connection: keep-alive
ETag: "64e716fd-264"
Accept-Ranges: bytes
[root@localhost sbin]#systemctl start nginx
[root@localhost sbin]#ls
nginx nginx.bak
[root@localhost sbin]#systemctl stop nginx
[root@localhost sbin]#ls
nginx nginx.bak
[root@localhost sbin]#mv nginx.bak nginx
mv:是否覆盖"nginx"? y
[root@localhost sbin]#systemctl start nginx
[root@localhost sbin]#ls
nginx
5.10 cpu与work进程绑定(亲缘性)
- 将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。
亲缘性:让1号进程只固定在cpu1上,不乱跳
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n
80553 nginx: master process /apps 1 0
80622 nginx: worker process 0 0
80623 nginx: worker process 1 0
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n
80553 nginx: master process /apps 1 0
80622 nginx: worker process 1 0
80623 nginx: worker process 1 0
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf #进入nginx配置文件
worker_cpu_affinity 00000001 00000010 00000100 00001000;第0号---第3号CPU
#序号绑定cpu 亲缘性
[root@localhost ~]#nginx -s reload #重新加载
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n
80553 nginx: master process /apps 1 0
81580 nginx: worker process 0 0
81581 nginx: worker process 1 0
5.11 nginx进程的优先级(work进程的优先级)
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep nginx|sort -n
#查看默认优先级,默认优先级为0
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep | grep nginx|sort -n
80553 nginx: master process /apps 1 0
81580 nginx: worker process 0 0
81581 nginx: worker process 1 0
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
worker_priority -20; #工作进程优先级,-20~20(19)
[root@localhost ~]#nginx -s reload #重新加载
[root@localhost ~]#ps axo pid,cmd,psr,ni|grep -v grep | grep nginx|sort -n
80553 nginx: master process /apps 1 0
82170 nginx: worker process 0 -20
82171 nginx: worker process 1 -20