nginx架构分析
1.nginx模块化
Nginx涉及到的模块分为核心模块、标准HTTP模块、可选HTTP模块、邮件服务模块以及第三方模块等五大类。
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.md
[root@centos-03 objs]# ls ngx_modules.c ngx_modules.c [root@centos-03 objs]#
2.模块目录
[root@centos-03 objs]# cd src [root@centos-03 src]# ls core event http mail misc os stream [root@centos-03 src]#
3.nginx WEB请求机制
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md
线程是进程的一个子单元,线程比进程好的地方是可以节省更多的资源,假如我开一个进程耗费的资源是20兆,那我开10个线程也是占用20兆,这样我同样的内存可以开更多的线程出来,每个线程也是处理一个请求,线程也有弊端,需要和其他的线程共享内存,稳定性不是很好
同步:同步、异步发生在当客户端发起请求后,服务端处理客户端的请求时。 同步机制,是指客户端发送请求后,需要等待服务端(内核)返回信息后,再继续发送下一个请求。 在同步机制中,所有的请求在服务器端得到同步,即发送方和接收方对请求的处理步调是一致的。
异步:异步机制,是指客户端发出一个请求后,不等待服务端(内核)返回信息,就继续发送下一个请求。 在异步机制中,所有来自发送方的请求形成一个队列,接收方处理完后再通知发送方。
阻塞:阻塞与非阻塞发生在IO调度中,比如内核到磁盘IO。 阻塞方式下,进程/线程在获取最终结果之前,被系统挂起了,也就是所谓的阻塞了,在阻塞过程中该进程什么都干不了, 直到最终结果反馈给它时,它才恢复运行状态。
非阻塞:非阻塞方式和阻塞相反,进程/线程在获取最终结果之前,并没有进入被挂起的状态,而是该进程可以继续执行新的任务。 当有最终结果反馈给该进程时,它再把结果交给客户端。
4.nginx事件驱动模型
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/event.md
事件驱动模型是实现异步非阻塞的一个手段。事件驱动模型中,一个进程(线程)就可以了。 对于web服务器来说,客户端A的请求连接到服务端时,服务端的某个进程(Nginx worker process)会处理该请求, 此进程在没有返回给客户端A结果时,它又去处理了客户端B的请求。 服务端把客户端A以及客户端B发来的请求作为事件交给了“事件收集器”, 而“事件收集器”再把收集到的事件交由“事件发送器”发送给“事件处理器”进行处理。 最后“事件处理器”处理完该事件后,通知服务端进程,服务端进程再把结果返回给客户端A、客户端B。 在这个过程中,服务端进程做的事情属于用户级别的,而事件处理这部分工作属于内核级别的。 也就是说这个事件驱动模型是需要操作系统内核来作为支撑的。
select模型、poll模型、epoll模型
5.Nginx架构
Nginx服务器使用 master/worker 多进程模式。 主进程(Master process)启动后,会接收和处理外部信号; 主进程启动后通过fork() 函数产生一个或多个子进程(work process),每个子进程会进行进程初始化、 模块调用以及对事件的接收和处理等工作。
nginx虚拟主机配置
1.nginx配置文件包括三部分:events上面的是全局部分、events、http(http里面有server,每一个server就是一个虚拟主机,一个http里面有多个server,可以跑多个站点,一般我们都是在http最下面加一条include vhost/*.conf,把所有的虚拟主机配置写到这个目录里面(http里面默认的server也注释掉))
[root@centos-03 conf]# ls /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf [root@centos-03 conf]#
2.创建vhost目录,然后在vhost目录下创建.conf文件,我们这里所有的站点文件都放到data/wwwroot/目录下
[root@centos-03 conf]# mkdir vhost [root@centos-03 conf]#
[root@centos-03 conf]# mkdir -p /data/wwwroot/ [root@centos-03 conf]#
[root@centos-03 conf]# mkdir /data/wwwroot/www.1.com [root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/
[root@centos-03 vhost]# vim 1.conf^C [root@centos-03 vhost]# cat 1.conf server { listen 80; server_name www.1.com; root /data/wwwroot/www.1.com; } [root@centos-03 vhost]#
[root@centos-03 www.1.com]# cd /data/wwwroot/www.1.com/
[root@centos-03 www.1.com]# vim index.html^C [root@centos-03 www.1.com]# cat index.html www.1.com [root@centos-03 www.1.com]#
3.检查下配置文件是否有错
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]#
4.重新加载配置文件
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]#
5.测试(相当于我们的www.1.com解析到了127.0.0.1上面了)
[root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 conf]#
6.这里我们用任何域名访问nginx都指向了www.1.com,这是因为nginx有一个默认虚拟主机的说法(访问没有配置的域名会指向其中一个虚拟主机),我们需要配置一个默认虚拟主机做一个限制,拒绝访问。
[root@centos-03 conf]# curl -x127.0.0.1:80 www.a.com www.1.com [root@centos-03 conf]# curl -x127.0.0.1:80 www.b.com www.1.com [root@centos-03 conf]#
[root@centos-03 vhost]# cd vhost/^C [root@centos-03 vhost]# cp 1.conf default.conf^C [root@centos-03 vhost]# vim default.conf ^C [root@centos-03 vhost]# cat default.conf server { listen 80 default_server; deny all; } [root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]# curl -x127.0.0.1:80 www.a.com <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]# curl -x127.0.0.1:80 www.b.com <html> <head><title>403 Forbidden</title></head> <body bgcolor="white"> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.14.0</center> </body> </html> [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 vhost]#
7.泛解析server配置所有的xxx.1.com类型访问都会到1.com/index.html上
server {
listen 80;
server_name *.1.com;
root /data/wwwroot/1.com;
}
8.基于端口的虚拟主机
[root@centos-03 vhost]# cp 1.conf 2.conf
[root@centos-03 vhost]# vim 2.conf ^C [root@centos-03 vhost]# cat 2.conf server { listen 8080; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com_8080; } [root@centos-03 vhost]#
[root@centos-03 vhost]# mkdir /data/wwwroot/www.1.com_8080
[root@centos-03 vhost]# vim /data/wwwroot/www.1.com_8080/index.html^C [root@centos-03 vhost]# cat !$ cat /data/wwwroot/www.1.com_8080/index.html 8080 [root@centos-03 vhost]#
[root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 vhost]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 vhost]#
[root@centos-03 vhost]# curl -x127.0.0.1:8080 www.1.com 8080 [root@centos-03 vhost]# curl -x127.0.0.1:80 www.1.com www.1.com [root@centos-03 vhost]#
nginx的rewrite配置-if
https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md
1.if指令条件判断语句由Nginx内置变量、逻辑判断符号和目标字符串三部分组成。 其中,内置变量是Nginx固定的非自定义的变量,如,$request_method, $request_uri等。 逻辑判断符号,有=, !=, ~, ~*, !~, !~* !表示相反的意思,~为匹配符号,它右侧为正则表达式,区分大小写,而~*为不区分大小写匹配。 目标字符串可以是正则表达式,通常不用加引号,但表达式中有特殊符号时,比如空格、花括号、分号等,需要用单引号引起来。
if ($request_method = POST) //当请求的方法为POST时,直接返回405状态码 { return 405; //在该示例中并未用到rewrite规则,if中支持用return指令。 } if ($http_user_agent ~ MSIE) //user_agent带有MSIE字符的请求,直接返回403状态码 { return 403; } 如果想同时限制多个user_agent,还可以写成这样 if ($http_user_agent ~ "MSIE|firefox|spider") { return 403; } if(!-f $request_filename) //当请求的文件不存在,将会执行下面的rewrite规则 { rewrite 语句; } if($request_uri ~* 'gid=\d{9,12}/') //\d表示数字,{9,12}表示数字出现的次数是9到12次,如gid=123456789/就是符合条件的。 { rewrite 语句; }
rewrite中的break和last
两个指令用法相同,但含义不同,需要放到rewrite规则的末尾,用来控制重写后的链接是否继续被nginx配置执行(主要是rewrite、return指令)。 示例1(连续两条rewrite规则): server{ listen 80; server_name test.com; root /tmp/123.com; rewrite /1.html /2.html ; rewrite /2.html /3.html ; } 当我们请求1.html时,最终访问到的是3.html,两条rewrite规则先后执行。
1.我们用1.conf虚拟机做实验,创建1.html、2.html、3.html
[root@centos-03 vhost]# cd /data/wwwroot/www.1.com [root@centos-03 www.1.com]# ls index.html [root@centos-03 www.1.com]# touch 1.html 2.html 3.html [root@centos-03 www.1.com]# ls 1.html 2.html 3.html index.html [root@centos-03 www.1.com]# echo 111 > 1.html [root@centos-03 www.1.com]# echo 222 > 2.html [root@centos-03 www.1.com]# echo 333 > 3.html [root@centos-03 www.1.com]#
2.配置1.conf,开启rewrite日志添加rewrite规则,通过日志记录我们查看rewrite执行过程,开启error_log notice级别
[root@centos-03 www.1.com]# cd /usr/local/nginx/conf/ [root@centos-03 conf]# vim vhost/1.conf [root@centos-03 conf]# cat vhost/1.conf server { listen 80; server_name www.1.com; index index.html; root /data/wwwroot/www.1.com; rewrite_log on; rewrite /1.html /2.html; rewrite /2.html /3.html; } [root@centos-03 conf]#
[root@centos-03 conf]# vim nginx.conf
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
[root@centos-03 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@centos-03 conf]# /usr/local/nginx/sbin/nginx -s reload [root@centos-03 conf]# curl -x127.0.0.1:80 www.1.com/1.html 333 [root@centos-03 conf]#
[root@centos-03 conf]# less ../logs/error.log 2018/07/26 17:33:21 [notice] 11371#0: *27 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" 2018/07/26 17:33:21 [notice] 11371#0: *27 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com" (END)