nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
nginx的模块从结构上
分为核心模块、基础模块和第三方模块
用户根据自己的需要开发的模块都属于第三方模块。
nginx模块从功能上
分为三类,分别是:
nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等
nginx基本模块:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用,包括:
更多的指令,请参考nginx官方文档
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载
,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
worker 进程中,ngx_worker_process_cycle()函数就是这个无限循环的处理函数。在这个函数中,一个请求的简单处理流程如下:
nginx模块一次常规的HTTP请求和响应的过程
一个典型的HTTP处理周期:
7. 客户端发送HTTP请求
8. Nginx基于配置文件中的位置选择一个合适的处理模块
9. (如果有)负载均衡模块选择一台后端服务器
10. 处理模块进行处理并把输出缓冲放到第一个过滤模块上
11. 第一个过滤模块处理后输出给第二个过滤模块
12. 然后第二个过滤模块又到第三个
13. 依此类推 –> 最后把响应发给客户端。
Nginx本身做的工作实际很少,当它接到一个HTTP请求时,它仅仅是通过查找配置文件将此次请求映射到一个location block,而此location中所配置的各个指令则会启动不同的模块去完成工作,因此模块可以看做Nginx真正的劳动工作者。
# 关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# getenforce 0
Disabled
# 创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
# 安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
[root@localhost ~]# yum -y groups mark install 'Development Tools'
# 创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
# 下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
# 编译安装
[root@localhost src]# ls
debug kernels nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
# 配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
# 启动nginx
[root@localhost ~]# nginx
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
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 128 [::]:22 [::]:*
# 写service文件让nginx开机自启
[root@localhost ~]# cat > /usr/lib/systemd/system/nginx.service <
主配置文件:/usr/local/nginx/conf/nginx.conf
配置文件 | 作用 |
---|---|
nginx.conf | nginx的基本配置文件 |
mime.types | MIME类型关联的扩展文件 |
fastcgi.conf | 与fastcgi相关的配置 |
proxy.conf | 与proxy相关的配置 |
sites.conf | 配置nginx提供的网站,包括虚拟主机 |
nginx.conf的内容分为以下几段:
derective value1 [value2 ...];
支持使用变量:
daemon {on|off}; //是否以守护进程方式运行nginx,调试时应设置为off
master_process {on|off}; //是否以master/worker模型来运行nginx,调试时可以设置为off
error_log 位置 级别; //配置错误日志
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
daemon off;
[root@localhost ~]# nginx -s stop;nginx
正常运行必备的配置参数
不过Nginx还是提供了关闭守护进程的模式,之所以提供这种模式,是为了方便跟踪调试Nginx,毕竟用gdb调试进程时最烦琐的就是如何继续跟进fork出的子进程了。
# user USERNAME [GROUPNAME] //指定允许worker进程的用户和组
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
# pid /path/to/pid_file //指定nginx守护进程的pid文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
pid logs/nginx.pid;
[root@localhost ~]# ls /usr/local/nginx/logs/
nginx.pid
[root@localhost ~]# systemctl stop nginx.service
[root@localhost ~]# ls /usr/local/nginx/logs/
# worker_rlimit_nofile number //设置所有woker进程最大可以打开的文件数,默认1024
# worker_rlimit_core size //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
0000 1000 //第四颗cpu核心
0001 0000 //第五颗cpu核心
0010 0000 //第六颗cpu核心
0100 0000 //第七颗cpu核心
1000 0000 //第八颗cpu核心
#查看cpu
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 3;
worker_cpu_affinity 0001 0010 0100;
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# ps -ef | grep nginx
root 1571 1 0 23:13 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1572 1571 0 23:13 ? 00:00:00 nginx: worker process
nginx 1573 1571 0 23:13 ? 00:00:00 nginx: worker process
nginx 1574 1571 0 23:13 ? 00:00:00 nginx: worker process
root 1576 1485 0 23:13 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]# top
top - 23:31:29 up 22 min, 1 user, load average: 0.00, 0.00, 0.00
Tasks: 223 total, 1 running, 222 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3752.0 total, 3300.8 free, 216.8 used, 234.4 buff/cache
MiB Swap: 4044.0 total, 4044.0 free, 0.0 used. 3305.5 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 242256 10628 8180 S 0.0 0.3 0:00.92 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 0 -20 0 0 0 I 0.0 0.0 0:00.00 rcu_gp
#按shift+f ,输入nginx,回车
Locate string nginx
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1485 root 20 0 26516 4928 3452 S 0.0 0.1 0:00.03 bash
1571 root 20 0 80256 1092 56 S 0.0 0.0 0:00.00 nginx
1572 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
1573 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
1574 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx
# 按f, 将光标移到 P = Last Used Cpu (SMP)
Fields Management for window 1:Def, whose current sort field is %CPU
Navigate with Up/Dn, Right selects for move then or Left commits,
'd' or toggles display, 's' sets sort. Use 'q' or to end!
* PID = Process Id WCHAN = Sleeping in Function
* USER = Effective User Name Flags = Task Flags
* PR = Priority CGROUPS = Control Groups
* NI = Nice Value SUPGIDS = Supp Groups IDs
* VIRT = Virtual Image (KiB) SUPGRPS = Supp Groups Names
* RES = Resident Size (KiB) TGID = Thread Group Id
* SHR = Shared Memory (KiB) OOMa = OOMEM Adjustment
* S = Process Status OOMs = OOMEM Score current
* %CPU = CPU Usage ENVIRON = Environment vars
* %MEM = Memory Usage (RES) vMj = Major Faults delta
* TIME+ = CPU Time, hundredths vMn = Minor Faults delta
* COMMAND = Command Name/Line USED = Res+Swap Size (KiB)
PPID = Parent Process pid nsIPC = IPC namespace Inode
UID = Effective User Id nsMNT = MNT namespace Inode
RUID = Real User Id nsNET = NET namespace Inode
RUSER = Real User Name nsPID = PID namespace Inode
SUID = Saved User Id nsUSER = USER namespace Inode
SUSER = Saved User Name nsUTS = UTS namespace Inode
GID = Group Id LXC = LXC container name
GROUP = Group Name RSan = RES Anonymous (KiB)
PGRP = Process Group Id RSfd = RES File-based (KiB)
TTY = Controlling Tty RSlk = RES Locked (KiB)
TPGID = Tty Process Grp Id RSsh = RES Shared (KiB)
SID = Session Id CGNAME = Control Group name
nTH = Number of Threads NU = Last Used NUMA node
P = Last Used Cpu (SMP)
TIME = CPU Time
SWAP = Swapped Size (KiB)
CODE = Code Size (KiB)
DATA = Data+Stack (KiB)
nMaj = Major Page Faults
nMin = Minor Page Faults
nDRT = Dirty Pages Count
# 空格选择,按q
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND P
1571 root 20 0 80256 1092 56 S 0.0 0.0 0:00.00 nginx 2
1572 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 0
1573 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 1
1574 nginx 20 0 111708 6276 4700 S 0.0 0.2 0:00.00 nginx 2
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值 number取19~-20,数值越低优先级越高
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_priority -20;
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# ps -elf |grep nginx
1 S root 1750 1 0 80 0 - 20064 - 00:21 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nginx 1751 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
5 S nginx 1752 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
5 S nginx 1753 1750 0 60 -20 - 27927 do_epo 00:21 ? 00:00:00 nginx: worker process
0 S root 1756 1485 0 80 0 - 3086 - 00:22 pts/0 00:00:00 grep --color=auto nginx
事件相关的配置:event{}段中的配置参数
accept_mutex {off|on}; //master调度用户请求至各worker进程时使用的负载均衡锁;on表示能让多个worker轮流地、序列化地去响应新请求
lock_file file; //accept_mutex用到的互斥锁锁文件路径
use [epoll | rtsig | select | poll]; //指明使用的事件模型,建议让nginx自行选择
worker_connections #; //每个进程能够接受的最大连接数
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10240;
}
[root@localhost ~]# dnf -y install httpd-tools
[root@localhost ~]# ab -n 3000 http://192.168.8.132/index.html
This is ApacheBench, Version 2.3 <$Revision: 1843412 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.8.132 (be patient)
Completed 300 requests
Completed 600 requests
Completed 900 requests
Completed 1200 requests
Completed 1500 requests
Completed 1800 requests
Completed 2100 requests
Completed 2400 requests
Completed 2700 requests
Completed 3000 requests
Finished 3000 requests
Server Software: nginx/1.20.1
Server Hostname: 192.168.111.141
Server Port: 80
Document Path: /index.html
Document Length: 612 bytes
Concurrency Level: 1
Time taken for tests: 0.376 seconds
Complete requests: 3000
Failed requests: 0
Total transferred: 2535000 bytes
HTML transferred: 1836000 bytes
Requests per second: 7974.21 [#/sec] (mean)
Time per request: 0.125 [ms] (mean)
Time per request: 0.125 [ms] (mean, across all concurrent requests)
Transfer rate: 6580.28 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 0.0 0 1
Waiting: 0 0 0.0 0 1
Total: 0 0 0.0 0 1
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 0
99% 0
100% 1 (longest request)
网络连接相关的配置参数
keepalive_timeout number; //长连接的超时时长,默认为65s
keepalive_requests number; //在一个长连接上所能够允许请求的最大资源数
keepalive_disable [msie6|safari|none]; //为指定类型的UserAgent禁用长连接
tcp_nodelay on|off; //是否对长连接使用TCP_NODELAY选项,为了提升用户体验,通常设为on
client_header_timeout number; //读取http请求报文首部的超时时长
client_body_timeout number; //读取http请求报文body部分的超时时长
send_timeout number; //发送响应报文的超时时长
fastcgi的相关配置参数
# LNMP: php需启用fpm模型
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000; //定义反向代理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; //.php文件路径
include fastcgi_params;
}
nginx.conf配置文件案例
更改默认端口号以及进程数和指定特定配置文件
[root@localhost conf]# head 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 logs/nginx.pid;
// 使用80端口
server {
listen 80;
server_name localhost;
// 使用源文件运行进程数如下
[root@localhost conf]# ps -ef | grep nginx
root 257815 1 0 16:46 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 257816 257815 0 16:46 ? 00:00:00 nginx: worker process
root 258199 102060 0 16:46 pts/0 00:00:00 grep --color=auto nginx
// 将源文件以及mime.types文件copy一份到/opt目录中
[root@localhost conf]# cp nginx.conf /opt/
[root@localhost conf]# cp mime.types /opt/
[root@localhost conf]# cd /opt/
[root@localhost opt]# ls
mime.types nginx.conf
[root@localhost opt]# nginx -t -c /opt/nginx.conf
nginx: the configuration file /opt/nginx.conf syntax is ok
nginx: configuration file /opt/nginx.conf test is successful
// 修改 worker_rlimit_nofile number; 参数为4
#user nobody;
worker_processes 4;
server {
listen 8070;
server_name localhost;
使用nginx服务控制命令重启并指定配置文件路径
[root@localhost opt]# nginx -s stop;nginx -c /opt/nginx.conf
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8070 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost ~]# ps -ef | grep nginx
root 276931 1 0 16:56 ? 00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx 276932 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276933 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276934 276931 0 16:56 ? 00:00:00 nginx: worker process
nginx 276935 276931 0 16:56 ? 00:00:00 nginx: worker process
root 283466 276242 0 16:58 pts/2 00:00:00 grep --color=auto nginx
[root@localhost nginx-1.12.0]# vim objs/Makefile
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werrori -g
// 找到 -Werrori 后将其去掉
[root@localhost nginx-1.12.0]# vim src/os/unix/ngx_user.c
#ifdef __GLIBC__
/* work around the glibc bug */
cd.current_salt[0] = ~salt[0];
#endif
// 把cd.current_salt[0] = ~salt[0];前后加上/* cd.current_salt[0] = ~salt[0];*/