【Day3】Nginx实战训练营
7/8/9 Nginx架构分析
1、Nginx模块化
- Nginx基于模块化设计,每个模块是一个功能实现,分布式开发,团队协作
- 模块分类: 核心模块、标准HTTP模块、可选HTTP模块、邮件模块、第三方模块
- 编译后的源码目录 objs/ngx_modules.c
[root@localhost ~]# cat /usr/local/src/nginx-1.16.1/objs/ngx_modules.c
- 详细资料: https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/module.md
2、Nginx的web请求机制
- 并行处理:多进程、多线程、异步
多线程相较多进程节省资源,但是相对欠缺稳定性 - 同步机制:客户端发送请求后,等服务端(内核)返回信息,再发送下一请求,即发送方和接收方对请求的处理步调一致
- 异步机制:客户端发出请求后,不等服务端(内核)返回信息,就继续发送下一请求,所有发送方请求形成一个队列,接收方处理完后再通知发送方
- 阻塞和非阻塞:结合同步、异步机制,同步时阻塞,异步时非阻塞,效率更高
- 详细资料: https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md
3、Nginx事件驱动模型
- 事件驱动模型是实现异步非阻塞的一个手段。事件驱动模型中,一个进程(线程)就可以了。
最常见的三种驱动模型:select、pool、epoll - https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/event.md
4、设计架构概览
- Nginx基于模块化设计、基于事件驱动模型处理请求、主进程和工作进程
- Nginx服务器使用 master/worker 多进程模式。
- 主进程(Master process)启动后,会接收和处理外部信号;
主进程启动后通过fork() 函数产生一个或多个子进程(work process),每个子进程会进行进程初始化、
模块调用以及对事件的接收和处理等工作。
- https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/jg.md
10/11 Nginx虚拟主机配置
一台机器上跑多个站点,即多个域名
1、Nginx默认虚拟主机
不管什么域名解析到该服务器,都会访问到默认虚拟主机
nginx.conf中第一个server { }
或 listen 80 default_server;
泛解析 *.aming.com ,二级域名
2、Nginx虚拟主机配置规范
主配置文件 include 虚拟主机配置文件
在conf目录下创建 vhost 目录,在 vhost 目录下面定义虚拟主机配置,注释掉主配置文件中的 server
3、Nginx基于端口的虚拟主机
listen 端口不同,server_name 可以相同
1、更改主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
增加一行内容,并注释掉 server { } 内容
include vhost/*.conf;
[root@localhost ~]# /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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
2、创建各类目录
[root@localhost ~]# mkdir /usr/local/nginx/conf/vhost/
[root@localhost ~]# cd !$
cd /usr/local/nginx/conf/vhost/
[root@localhost vhost]# mkdir -p /data/wwwroot/www.1.com
[root@localhost vhost]# mkdir /data/wwwroot/www.2.com/
[root@localhost vhost]# ls /data/wwwroot/
www.1.com www.2.com
3、创建虚拟主机配置文件
[root@localhost vhost]# vim www.1.com.conf
server {
listen 80;
server_name www.1.com;
root /data/wwwroot/www.1.com;
}
[root@localhost vhost]# cp www.1.com.conf www.2.com.conf
[root@localhost vhost]# vim www.2.com.conf
server {
listen 80;
server_name www.2.com;
root /data/wwwroot/www.2.com;
}
4、创建默认页
[root@localhost vhost]# cd /data/wwwroot/www.1.com/
[root@localhost www.1.com]# vim index.html
www.1.com
[root@localhost www.1.com]# cd ../www.2.com/
[root@localhost www.2.com]# vim index.html
www.2.com
5、重启
[root@localhost www.2.com]# /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@localhost www.2.com]# /usr/local/nginx/sbin/nginx -s reload
6、访问测试
[root@localhost www.2.com]# curl -x127.0.0.1:80 www.1.com
www.1.com
[root@localhost www.2.com]# curl -x127.0.0.1:80 www.1.com -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Tue, 02 Feb 2021 04:01:06 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 02 Feb 2021 03:54:58 GMT
Connection: keep-alive
ETag: "6018cd12-a"
Accept-Ranges: bytes
[root@localhost www.2.com]# curl -x127.0.0.1:80 www.2.com
www.2.com
[root@localhost www.2.com]# curl -x127.0.0.1:80 www.2.com -I
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Tue, 02 Feb 2021 04:01:23 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 02 Feb 2021 03:55:56 GMT
Connection: keep-alive
ETag: "6018cd4c-a"
Accept-Ranges: bytes
[root@localhost www.2.com]# curl -x127.0.0.1:80 www.3.com
www.1.com
访问的域名不存在于虚拟主机中时,会访问默认虚拟主机
7、指定默认虚拟主机
[root@localhost vhost]# vim default.conf
server {
listen 80 default_server;
root /data/wwwroot/www.1.com;
}
如果不指定默认虚拟主机的话,就会以 vhost 下第一个虚拟主机为默认虚拟主机
在主配置文件 nginx.conf 中,include 在 server {} 上,那么默认虚拟主机在 include 定义的目录中,反之,默认虚拟主机就是 server {} 定义的,除非注释掉不生效
8、拒绝所有对默认虚拟主机的访问
[root@localhost vhost]# vim default.conf
server {
listen 80 default_server;
deny all;
root /data/wwwroot/www.1.com;
}
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# !curl
curl -x127.0.0.1:80 www.3.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
找不到索引页,就会返回 403
9、改变索引页,看差别
[root@localhost vhost]# vim www.1.com.conf
server {
listen 80;
server_name www.1.com;
index 1.html;
root /data/wwwroot/www.1.com;
}
[root@localhost 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@localhost vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@localhost vhost]# curl -x127.0.0.1:80 www.1.com
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@localhost vhost]# echo 111111 > /data/wwwroot/www.1.com/1.html
[root@localhost vhost]# curl -x127.0.0.1:80 www.1.com
111111