1、Nginx 进程结构

(1)master 进程
当 Nginx 在启动后,会有一个 master 进程和多个worker 进程。master 进程主要用来管理 worker 进程,master 要做的就是:接收来自外界的信号,向各 worker 进程发送信号,监控 worker 进程的运行状态,当 worker 进程退出后(异常情况下),会自动重新启动新的 worker 进程。
master 进程主要完成如下工作:
读取并验证配置信息;
创建、绑定及关闭套接字;
启动、终止 worker 进程及维护 worker 进程的个数;
无须中止服务而重新配置工作;
控制非中断式程序升级,启用新的二进制程序并在需要时回滚至老版本;
重新打开日志文件;
编译嵌入式 Perl 脚本。
(2) worker 进程
对于基本的网络事件,Nginx 则是放在 worker 进程中来处理。多个 worker 进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。(会发生惊群<内核小于2.6.18,Nginx中处理epoll时惊群问题的思路很简单,多个子进程有一个锁,谁拿到锁,谁才将accept的fd加入到epoll队列中,其他的子进程拿不到锁,也就不会将fd加入到epoll中,连接到来也就不会导致所有子进程的epoll被唤醒返回)。一个请求,只可能在一个 worker 进程中处理,一个 worker 进程,不可能处理其它进程的请求(一对一)。然而 Nginx 没有专门地仲裁或连接分布的 worker,这项工作是由操作系统内核机制完成的。在启动时,创建一组初始的监听套接字,HTTP 请求和响应之时,worker 连续接收、读取和写入套接字。
worker 进程主要完成如下工作:
接收、传入并处理来自客户端的连接
提供反向代理及过滤功能
nginx 任何能完成的其它任务
2、Nginx 核心配置详解
(1)、Nginx的配置文件的组成部分:
(2)、主配置文件结构:四部分
main block:主配置段,即全局配置段,对http,mail都有效
#事件驱动相关的配置
event {
...
}
#http/https 协议相关配置段
http {
...
}
#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
...
}
#stream 服务器相关配置段
stream {
...
}
(3)、root 与 alias
server {
listen 80;
server_name lee.lle.org;
location / {
root /webdata/nginx/ll.org/lee/html;
}
location /dirtest { #必须建立/mnt/dirtest才能访问
root /mnt;
}
}
alias:定义路径别名,会把访问的路径重新定义到其指定的路径,文档映射的另一种机制;仅能用于 location上下文,此指令使用较少
server {
listen 80;
server_name lee.ll.org;
location / {
root /webdata/nginx/ll.org/lee/html;
}
location /dirtest {
root /mnt;
}
location /alias { #注意about后不要加/
#使用alias的时候uri后面如果加了斜杠,则下面的路径配置必须加斜杠,否则403
alias /mnt/dirtest; #当访问alias的时候,会显示alias定义的/mnt/dirtest里面的内容
}
}
3、Nginx 高级配置
(1)、Nginx 状态页
location /nginx_status {
stub_status;
auth_basic "auth login";
auth_basic_user_file /apps/nginx/conf/.htpasswd;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
}
#状态页用于输出nginx的基本状态信息:
#输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled,requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections: #当前处于活动状态的客户端连接数
#包括连接等待空闲连接数=reading+writing+waiting
accepts: #统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled: #统计总值,Nginx自启动后已经处理完成的客户端请求连接总数
#通常等于accepts,除非有因worker_connections限制等被拒绝的连接
requests: #统计总值,Nginx自启动后客户端发来的总的请求数
Reading: #当前状态,正在读取客户端请求报文首部的连接的连接数
#数值越大,说明排队现象严重,性能不足
Writing: #当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting: #当前状态,正在等待客户端发出请求的空闲连接数
开启 keep-alive的情况下,这个值等于active –(reading+writing)
(2)、Nginx 压缩功能
#启用或禁用gzip压缩,默认关闭
gzip on | off;
#压缩比由低到高从1到9,默认为1,值越高压缩后文件越小,但是消耗cpu比较高。基本设定未4或者5
gzip_comp_level 4;
#禁用IE6 gzip功能,早期的IE6之前的版本不支持压缩
gzip_disable "MSIE [1-6]\.";
#gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
#启用压缩功能时,协议的最小版本,默认HTTP/1.1
gzip_http_version 1.0 | 1.1;
#指定Nginx服务需要向服务器申请的缓存空间的个数和大小,平台不同,默认:32 4k或者16 8k;
gzip_buffers number size;
#指明仅对哪些类型的资源执行压缩操作;默认为gzip_types text/html,不用显示指定,否则出错
gzip_types mime-type ...;
#如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”,一般建议打开
gzip_vary on | off;
4、内置变量
$remote_addr;
#存放了客户端的地址,注意是客户端的公网IP
$args;
#变量中存放了URL中的所有参数
#例如:https://search.jd.com/Search?keyword=手机&enc=utf-8
#返回结果为: keyword=手机&enc=utf-8
$is_args
#如果有参数为? 否则为空
$document_root;
#保存了针对当前资源的请求的系统根目录,例如:/webdata/nginx/timinglee.org/lee。
$document_uri;
#保存了当前请求中不包含参数的URI,注意是不包含请求的指令
#比如:http://lee.timinglee.org/var?\id=11111会被定义为/var
#返回结果为:/var
$host;
#存放了请求的host名称
limit_rate 10240;
echo $limit_rate;
#如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没有设置, 则显示0
$remote_port;
#客户端请求Nginx服务器时随机打开的端口,这是每个客户端自己的端口
$remote_user;
#已经经过Auth Basic Module验证的用户名
$request_body_file;
#做反向代理时发给后端服务器的本地资源的名称
$request_method;
#请求资源的方式,GET/PUT/DELETE等
$request_filename;
#当前请求的资源文件的磁盘路径,由root或alias指令与URI请求生成的文件绝对路径,
#如:webdata/nginx/timinglee.org/lee/var/index.html
$request_uri;
#包含请求参数的原始URI,不包含主机名,相当于:$document_uri?$args,
#例如:/main/index.do?id=20190221&partner=search
$scheme;
#请求的协议,例如:http,https,ftp等
$server_protocol;
#保存了客户端请求资源使用的协议的版本,例如:HTTP/1.0,HTTP/1.1,HTTP/2.0等
$server_addr;
#保存了服务器的IP地址
$server_name;
#虚拟主机的主机名
$server_port;
#虚拟主机的端口号
$http_user_agent;
#客户端浏览器的详细信息
$http_cookie;
#客户端的所有cookie信息
$cookie_<name>
#name为任意请求报文首部字部cookie的key名
$http_<name>
#name为任意请求报文首部字段,表示记录请求报文的首部字段,ame的对应的首部字段名需要为小写,如果有
横线需要替换为下划线
5、Rewrite
(1)if 指令
if (条件匹配) {
action
}
= #比较变量和字符串是否相等,相等时if指令认为该条件为true,反之为false
!= #比较变量和字符串是否不相等,不相等时if指令认为条件为true,反之为false
~ #区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~ #区分大小写字符,判断是否匹配,不满足匹配条件为真,满足匹配条件为假
~* #不区分大小写字符,可以通过正则表达式匹配,满足匹配条件为真,不满足匹配条件为假
!~* #不区分大小字符,判断是否匹配,满足匹配条件为假,不满足匹配条件为真
-f 和 !-f #判断请求的文件是否存在和是否不存在
-d 和 !-d #判断请求的目录是否存在和是否不存在
-x 和 !-x #判断文件是否可执行和是否不可执行
-e 和 !-e #判断请求的文件或目录是否存在和是否不存在(包括文件,目录,软链接)
(2)set指令
(3)break指令
(4)return 指令
(5)rewrite 指令
(6)nginx防盗链
6、反向代理功能
(1)反向代理配置参数
proxy_pass; #用来设置将客户端请求转发给的后端服务器的主机
#可以是主机名(将转发至后端服务做为主机头首部)、IP地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
url后面
#此行为类似于root
#proxy_pass指定的uri不带斜线将访问的/web
#等于访问后端服务器
uri内容
#此行为类似于alias
#proxy_pass指定的uri带斜线
#等于访问后端服务器的
#http://172.25.254.40:8080/index.html
#内容返回给客户端
proxy_pass_header field; #透传
#默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel等参数
#如果要传递的话则要使用 proxy_pass_header field声明将后端服务器返回的值传递给客户端
#field 首部字段大小不敏感
proxy_pass_request_body on | off;
#是否向后端服务器发送HTTP实体部分,可以设置在http,server或location块,默认即为开启
proxy_pass_request_headers on | off;
#是否将客户端的请求头部转发给后端服务器,可以设置在http,server或location块,默认即为开启
proxy_set_header;#可更改或添加客户端的请求头部信息内容并转发至后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部
proxy_connect_timeout time;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒
用法如下:proxy_connect_timeout 6s;
#60s为自定义nginx与后端服务器建立连接的超时时间,超时会返回客户端504响应码
proxy_read_timeout time;
#配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间,默认60s
proxy_send_timeout time;
#配置nginx项后端服务器或服务器组发起write请求后,等待的超时 时间,默认60s
proxy_http_version 1.0;
#用于设置nginx提供代理服务的HTTP协议的版本,默认http 1.0
proxy_ignore_client_abort off;
#当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。即如果此项设置为on开启,则服务器、
会忽略客户端中断并一直等着代理服务执行返回,如果设置为off,则客户端中断后Nginx也会中断客户端请求
并立即记录499日志,默认为off。
(2)缓存功能
7、实现 FastCGI
什么是PHP-FPM?
(1)php源码配置
8、实验
A、nginx平滑升级和回滚
[root@Nginx nginx-1.24.0]# ./configure --prefix=/usr/local/nginx \
--user=nginx \ # 指定nginx运行用户
--group=nginx \ # 指定nginx运行组
--with-http_ssl_module \ # 支持https://
--with-http_v2_module \ # 支持http版本2
--with-http_realip_module \ # 支持ip透传
--with-http_stub_status_module \ # 支持状态页面
--with-http_gzip_static_module \ # 支持压缩
--with-pcre \ # 支持正则
--with-stream \ # 支持tcp反向代理
--with-stream_ssl_module \ # 支持tcp的ssl加密
--with-stream_realip_module # 支持tcp的透传ip
关闭debug功能
[root@nginx nginx-1.24.0]# vim auto/cc/gcc
[root@nginx nginx-1.24.0]# make && make install
[root@nginx nginx-1.24.0]# vim ~/.bash_profile
把nginx软件的命令执行路径添加到环境变量中
[root@nginx nginx-1.24.0]# source ~/.bash_profile
平滑升级回收旧版本
版本回滚旧的激活,新的需要回收
B、nginx命令参数
----------------------------参数
[root@Nginx ~]# nginx -v
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-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 #发送信号,reload信号 会生成新的worker,但master不会重新生成
-p prefix : set prefix path (default: /etc/nginx/) #指定Nginx 目录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置文件路径
-g directives : set global directives out of configuration file #设置全局指令,注意和配置文件不要同时配置,否则冲突
C、nginx启动文件编写
D、nginx全局参数优化
E、location用法
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射; ngnix会根据用户请求的URI来检查定义的所有location,按一定的优先级找出一个最佳匹配, 而后应用其配置在没有使用正则表达式的时候,nginx会先在server中的多个location选取匹配度最高的一个uri ,uri是用户请求的字符串,即域名后面的web文件路径 ,然后使用该location模块中的正则url和字符串,如果匹配成功就结束搜索,并使用此location处理 此请求。
#语法规则:
location [ = | ~ | ~* | ^~ ] uri { ... }
= #用于标准uri前,需要请求字串与uri精确匹配,大小敏感,如果匹配成功就停止向下匹配并立
即处理请求
^~ #用于标准uri前,表示包含正则表达式,并且匹配以指定的正则表达式开头
#对uri的最左边部分做匹配检查,不区分字符大小写
~ #用于标准uri前,表示包含正则表达式,并且区分大小写
~* #用于标准uri前,表示包含正则表达式,并且不区分大写
不带符号 #匹配起始于此uri的所有的uri
\ #用于标准uri前,表示包含正则表达式并且转义字符。可以将 . * ?等转义为普通符号
#匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
F、nginx的用户认证
[root@nginx ~]# htpasswd -cm /usr/local/nginx/.htpasswd admin
[root@nginx ~]# htpasswd -m /usr/local/nginx/.htpasswd ll
G、自定义错误页面
H、自定义日志
查看生成日志[root@nginx ~]# cat /var/log/ll.org/access.log
[root@nginx ~]# cat /var/log/ll.org/error.log
I、nginx文件检测
J、nginx长链接控制
测试
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# telnet www.ll.org 80
K、nginx下载服务器
autoindex on; #自动索引功能
autoindex_exact_size on; #计算文件确切大小(单位bytes),此为默认值,off只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #on表示显示本机时间而非GMT(格林威治)时间,默为为off显示GMT时间
limit_rate 1024k; #限速,默认不限速
测速
L、nginx的压缩功能
[root@nginx ~]# echo hello grtout > /data/web/html/small.html
[root@nginx ~]# cat /usr/local/nginx/logs/error.log > /data/web/html/small.html
[root@nginx ~]# cat /usr/local/nginx/logs/error.log > /data/web/html/big.html
M、nginx的内建变量
N、nginx的网页重写功能
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl var.ll.org/test2/index.html
[root@nginx conf.d]# mkdir -p /data/web/html/test2/
[root@nginx conf.d]# echo test2 > /data/web/html/test2/index.html
[root@nginx conf.d]# curl var.ll.org/test2/index.html
显示结果为test2
set指令
[root@client20 ~]# curl ll.ll.org/test3
ll
break指令
[root@client20 ~]# curl ll.ll.org/break #当未添加break时
ll
80
[root@client20 ~]# curl ll.ll.org/break #添加break后
lee
return指令
[root@client20~]# curl lee.ll.org/return
/web/data/nginx/ll.org/ll/return is exist
[root@client20 ~]# curl ll.ll.org/return1
/web/data/nginx/ll.org/ll/return1 is not exist
. #匹配除换行符以外的任意字符
\w #匹配字母或数字或下划线或汉字
\s #匹配任意的空白符
\d #匹配数字
\b #匹配单词的开始或结束
^ #匹配字付串的开始
$ #匹配字符串的结束
* #匹配重复零次或更多次
+ #匹配重复一次或更多次
? #匹配重复零次或一次
(n) #匹配重复n次
{n,} #匹配重复n次或更多次
{n,m} #匹配重复n到m次
*? #匹配重复任意次,但尽可能少重复
+? #匹配重复1次或更多次,但尽可能少重复
?? #匹配重复0次或1次,但尽可能少重复
{n,m}? #匹配重复n到m次,但尽可能少重复
{n,}? #匹配重复n次以上,但尽可能少重复
\W #匹配任意不是字母,数字,下划线,汉字的字符
\S #匹配任意不是空白符的字符
\D #匹配任意非数字的字符
\B #匹配不是单词开头或结束的位置
[^x] #匹配除了x以外的任意字符
[^lee] #匹配除了magedu 这几个字母以外的任意字符
flag 说明
redirect;
#临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URL给客户端
#由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
permanent;
#重写完成后以永久重定向方式直接返回重写后生成的新URL给客户端
#由客户端重新发起请求,状态码:301
break;
#重写完成后,停止对当前URL在当前location中后续的其它重写操作
#而后直接跳转至重写规则配置块之后的其它配置,结束循环,建议在location中使用
#适用于一个URL一次重写
last;
#重写完成后,停止对当前URI在当前location中后续的其它重写操作,
#而后对新的URL启动新一轮重写检查,不建议在location中使用
#适用于一个URL多次重写,要注意避免出现超过十次以及URL重写后返回错误的给用户
break 与 last
O、nginx的网页重写实战案例
[root@nginx ~]# cd /usr/local/nginx
[root@nginx nginx]# mkdir certs
[root@nginx nginx]# cd certs/
[root@nginx ~]# cd /usr/local/nginx/certs
[root@nginx certs]# ls
ll.org.crt ll.org.key
[root@nginx certs]# cd ..
[root@nginx nginx]# ls
certs conf fastcgi_temp logs sbin uwsgi_temp
client_body_temp conf.d html proxy_temp scgi_temp
[root@nginx nginx]# cd conf.d
[root@nginx conf.d]#
判断文件是否存在
访问的是www.ll.org/a/b 无 所以定向到index.html
P、防盗链
Q、nginx的反向代理
[root@client10 ~]# echo 172.25.254.10 > /var/www/html/index.html
[root@client20 ~]# echo 172.25.254.20 > /var/www/html/index.html
[root@client20 ~]# vim /etc/httpd/conf/httpd.conf
[root@client20 ~]# systemctl restart httpd
[root@client20 ~]# mkdir -p /var/www/html/static
[root@client20 ~]# echo static 172.25.254.20 > /var/www/html/static/index.html
[root@client10 ~]# yum install php -y
[root@client10 ~]# systemctl restart httpd
[root@client10 ~]# vim /var/www/html/index.php
[root@client10 ~]# cat /var/www/html/index.php
<?php
phpinfo();
?>
Q、反向代理的缓存功能
[root@client10 ~]# ab -n1000 -c100 http://www.ll.org/static/index.html
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@client10 ~]# ab -n1000 -c100 http://www.ll.org/static/index.html
R、反向代理负载均衡
uri
[root@client10 ~]# mkdir -p /var/www/html/static
[root@client10 ~]# echo 172.25.254.10 static > /var/www/html/static/index.html
cookie
四层负载
[root@client10 ~]# dnf install bind -y
[root@client20 ~]# dnf install bind -y
[root@client20 ~]# vim /etc/named.conf
[root@client10 ~]# vim /etc/named.rfc1912.zones
[root@client10 ~]# cd /var/named/
[root@client10 named]# cp named.localhost ll.org.zone -p
[root@client10 named]# vim ll.org.zone
[root@client10 named]# systemctl start named
[root@client10 named]# dig www.ll.org @172.25.254.10
[root@client10 named]# dnf install mariadb-server -y
[root@client20 named]# dnf install mariadb-server -y
[root@nginx conf.d]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx tcpconf.d]# dnf install mariadb -y
[root@nginx tcpconf.d]# mysql -u ll -p -h 172.25.254.100
[root@nginx tcpconf.d]# mysql -u ll -p -h 172.25.254.100
+-------------+
| @@server_id |
+-------------+
| 20 |
+-------------+
S、php源码编译
[root@nginx ~]# cd nginx-1.26.1/
[root@nginx nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --add-module=/root/echo-nginx-module-0.63 --add-module=/root/memc-nginx-module-0.20 --add-module=/root/srcache-nginx-module-0.33 --user=nginx --group=nginx --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-pcre
[root@nginx nginx-1.26.1]# make && make install
[root@nginx php-8.3.9]# dnf install systemd-devel -y
[root@nginx php-8.3.9]# yum install -y bzip2 libxml2-devel sqlite-devellibpng-devel libcurl-devel oniguruma-devel
[root@nginx php-8.3.9]# make && make install
T、PHP配置优化
[root@nginx ~]# cd /usr/local/php/
[root@nginx php]# ls
bin etc include lib php sbin var
[root@nginx php]# cd
[root@nginx ~]# cd /usr/local/php/etc/
[root@nginx etc]# ls
php-fpm.conf.default php-fpm.d
[root@nginx etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@nginx etc]# vim php-fpm.conf
[root@nginx etc]# cd php-fpm.d/
[root@nginx php-fpm.d]# ls
www.conf.default
[root@nginx php-fpm.d]# cp www.conf.default www.conf -p
[root@nginx php-fpm.d]# ls
www.conf www.conf.default
[root@nginx php-fpm.d]# vim www.conf
[root@nginx php-8.3.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@nginx php-8.3.9]# cd
[root@nginx ~]# cd /usr/local/php/etc
[root@nginx etc]# vim php.ini
U、nginx和php的整合
[root@nginx ~]# cd /usr/local/php/
[root@nginx php]# ls
bin etc include lib php sbin var
[root@nginx php]# cd bin/
[root@nginx bin]# ls
phar phar.phar php php-cgi php-config phpdbg phpize
[root@nginx bin]# ped
bash: ped: command not found...
[root@nginx bin]# pwd
/usr/local/php/bin
[root@nginx bin]# vim ~/.bash_profile
[root@nginx bin]# source ~/.bash_profile
[root@nginx nginx]# mkdir conf1.d
[root@nginx nginx]# vim conf/nginx.conf