一、Nginx初探
1、Nginx的功能特性
- Nginx提供基本HTTP服务,可以作为HTTP代理服务器和反向代理服务器,支持通过缓存加速访问,可以完成简单的负载均衡和容错,支持包过滤功能,支持SSL等
- Nginx提供高级HTTP服务,可以进行自定义配置,支持虚拟主机,支持URL重定向,支持网络监控,支持流媒体传输等
- Nginx作为邮件代理服务器,支持IMAP/POP3代理服务功能,支持内部SMTP代理服务功能
2、Nginx常用功能介绍
1)、HTTP代理和反向代理
在提供反向代理服务方面,Nginx服务器转发前端请求性能稳定,并且后端转发与业务配置相互分离,配置相当灵活
2)、负载均衡
负载均衡是指将大量的前端并发访问或数据流量分担到多个后端网络节点上分别处理,这样可以有效减少前端用户等待响应的时间
Nginx服务器的负载均衡策略可以划分为内置策略和扩展策略。内置策略主要包含轮询、加权轮询和IP hash三种,扩展策略主要通过第三方模块实现,常见有url hash和fair等
- 轮询策略:将前端请求按顺序(时间顺序或者排列次序)逐一分配到不同的后端节点上,对于出现问题的后端节点自动排除
- 加权轮询策略:在基本的轮询策略上考略各后端节点接受请求的权重,指定各后端节点被轮询到的几率。加权轮询策略主要用于后端节点性能不均的情况
- IP hash策略:将前端的访问IP进行hash操作,然后根据hash结果将请求分配给不同的后端节点。通过Nginx的实现,每个前端访问IP会固定访问一个后端节点,这样做的好处是避免考虑前端用户的session在后盾多个节点上共享的问题
- url hash策略:是对前端请求的url进行hash操作,优点在于,如果后端有缓存服务器,它能够提高缓存效率,同时也解决了session的问题,其缺点是,如果后端节点出现异常,它不能自动排除该节点
- fair策略:将前端请求转发到一个最近负载最小的后台节点,Nginx通过后端节点对请求的响应时间来判断负载情况,响应时间短的节点负载相对就轻
3)、Web缓存
Nginx服务器的Web缓存服务主要由Proxy Cache相关指令集和FastCGI_Cache相关指令集构成。Proxy Cache主要用于在Nginx服务器提供反向代理服务时,对后端源服务器的返回内容进行URL缓存,FastCGI_Cache主要用于对FastCGI的动态程序进行缓存。第三方模块ngx_cache_purge主要用于清除Nginx服务器上指定的URL缓存
二、Nginx服务器的安装部署
Nginx的官方下载网站:http://nginx.org/en/download.html
1、安装Nginx服务器和基本配置
1)、Windows版本的安装
- conf目录:存放Nginx服务器的配置文件
- docs目录:存放Nginx服务器的文档资料
- html目录:存放两个后缀名为.html的静态网页文件
- logs目录:存放了Nginx服务器的运行日志文件
- nginx.exe:启动Nginx服务器的运行程序
2)、Linux版本的编译和安装
# 安装编译Nginx源代码需要的GCC编译器、自动创建Makefile需要的Automake工具、Nginx的一些模块依赖的其他第三方库:pcre库、zlib库和openssl库等
[root@localhost ~]# yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel
[root@localhost ~]# mkdir /nginx_1158
[root@localhost ~]# cd /nginx_1158/
[root@localhost nginx_1158]# tar xf nginx-1.15.8.tar.gz
[root@localhost nginx_1158]# cd nginx-1.15.8
[root@localhost nginx-1.15.8]# ll
总用量 744
drwxr-xr-x. 6 1001 1001 4096 2月 23 11:07 auto
-rw-r--r--. 1 1001 1001 294414 12月 25 22:53 CHANGES
-rw-r--r--. 1 1001 1001 449169 12月 25 22:53 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 2月 23 11:07 conf
-rwxr-xr-x. 1 1001 1001 2502 12月 25 22:53 configure
drwxr-xr-x. 4 1001 1001 72 2月 23 11:07 contrib
drwxr-xr-x. 2 1001 1001 40 2月 23 11:07 html
-rw-r--r--. 1 1001 1001 1397 12月 25 22:53 LICENSE
drwxr-xr-x. 2 1001 1001 21 2月 23 11:07 man
-rw-r--r--. 1 1001 1001 49 12月 25 22:53 README
drwxr-xr-x. 9 1001 1001 91 2月 23 11:07 src
- src目录:存放了Nginx软件的所有源代码
- man目录:存放了Nginx软件的帮助文档
- html和conf目录:存放的内容和Windows版本的同名目录相同
- auto目录:存放了大量脚本文件,和configure脚本程序有关
- configure文件:Nginx软件的自动脚本程序,运行configure自动脚本一般会完成两项工作:一是检查环境,根据环境检查结果生成C代码;二是生成编译代码需要的Makefile文件
[root@localhost nginx-1.15.8]# mkdir /nginx
# 编译配置
[root@localhost nginx-1.15.8]# ./configure --prefix=/nginx
–prefix指定了Nginx软件的安装路径
其他的设置使用默认设置
生成的Makefile文件就保存在当前的工作目录(/nginx_1158/nginx-1.15.8)
# 编译
[root@localhost nginx-1.15.8]# make
# 安装
[root@localhost nginx-1.15.8]# make install
# 安装vim
[root@localhost nginx-1.15.8]# yum -y install vim*
[root@localhost nginx-1.15.8]# mkdir ~/.vim/
# vim配置Nginx配置语法高亮
[root@localhost nginx-1.15.8]# cp -r contrib/vim/* ~/.vim/
[root@localhost nginx-1.15.8]# cd /nginx
[root@localhost nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 2月 23 11:26 conf
drwxr-xr-x. 2 root root 40 2月 23 11:26 html
drwxr-xr-x. 2 root root 6 2月 23 11:26 logs
drwxr-xr-x. 2 root root 19 2月 23 11:26 sbin
- conf目录:存放了Nginx的所有配置文件。其中,nginx.conf文件是Nginx服务器的主配置文件
- html目录:存放了Nginx服务器在运行过程中调用的一些html文件
- logs目录:用来存放Nginx服务器的日志
- sbin目录:目录下只有nginx一个文件,是Nginx服务器的主程序
2、Nginx服务的启停控制
1)、Nginx服务的信号控制
Nginx服务在运行时,会保持一个主进程和一个或多个worker process工作进程。通过给Nginx服务的主进程发送信号就可以控制服务的启停了
在Nginx服务启动以后,默认在Nginx服务器安装目录下的logs目录中会产生文件名为nginx.pid的文件,此文件保持的就是Nginx服务主进程的PID
[root@localhost logs]# cat nginx.pid
20329
[root@localhost logs]# ps -ef|grep nginx
root 20329 1 0 13:04 ? 00:00:00 nginx: master process ./nginx
nobody 20330 20329 0 13:04 ? 00:00:00 nginx: worker process
root 20363 1689 0 13:26 pts/0 00:00:00 grep --color=auto nginx
Nginx服务主进程能够接收的信号:
信号 | 作用 |
---|---|
TERM或INT | 快速停止Nginx服务 |
QUIT | 平缓停止Nginx服务 |
HUP | 使用新的配置文件启动进程,之后平缓停止原有进程(平滑重启) |
USR1 | 重新打开日志文件,常用于日志切割 |
USR2 | 使用新版本的Nginx文件启动服务,之后平缓停止原有Nginx进程(平滑升级) |
WINCH | 平缓停止worker process,用于Nginx服务器平滑升级 |
向Nginx服务主进程发送信号有两种方法,一种是使用nginx二进制文件,另一种方法是使用kill命令发送信号
kill SIGNAL PID
kill SIGNAL `filepath`
filepath为nginx.pid的路径
2)、Nginx二进制文件
# 启动Nginx服务
[root@localhost sbin]# ./nginx
[root@localhost sbin]# ./nginx -h
nginx version: nginx/1.15.8
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 # 向主进程发送信号
-p prefix : set prefix path (default: /nginx/) # 指定Nginx服务器路径前缀
-c filename : set configuration file (default: conf/nginx.conf) # 指定Nginx配置文件路径
-g directives : set global directives out of configuration file # 指定Nginx附加配置文件路径
# 立刻停止服务
[root@localhost sbin]# ./nginx -s stop
# 平缓停止服务
[root@localhost sbin]# ./nginx -s quit
# 重载配置文件(重载)
[root@localhost sbin]# ./nginx -s reload
# 重新开始记录日志文件(日志切割)
[root@localhost sbin]# ./nginx -s reopen
3)、Nginx服务器的升级
[root@localhost sbin]# ps -ef|grep nginx
root 20602 1 0 14:17 ? 00:00:00 nginx: master process ./nginx
nobody 20613 20602 0 14:19 ? 00:00:00 nginx: worker process
root 20618 1689 0 14:19 pts/0 00:00:00 grep --color=auto nginx
...代替原本的二进制文件nginx
[root@localhost sbin]# kill -USR2 20602
[root@localhost sbin]# ps -ef|grep nginx
root 20602 1 0 14:17 ? 00:00:00 nginx: master process ./nginx
nobody 20613 20602 0 14:19 ? 00:00:00 nginx: worker process
root 20619 20602 0 14:20 ? 00:00:00 nginx: master process ./nginx
nobody 20620 20619 0 14:20 ? 00:00:00 nginx: worker process
root 20622 1689 0 14:20 pts/0 00:00:00 grep --color=auto nginx
[root@localhost sbin]# kill -WINCH 20602
[root@localhost sbin]# ps -ef|grep nginx
root 20602 1 0 14:17 ? 00:00:00 nginx: master process ./nginx
root 20619 20602 0 14:20 ? 00:00:00 nginx: master process ./nginx
nobody 20620 20619 0 14:20 ? 00:00:00 nginx: worker process
root 20624 1689 0 14:20 pts/0 00:00:00 grep --color=auto nginx
# 之前的master进程不会退出,允许我们做版本回退
##3、Nginx.conf文件的结构
... # 全局块
events { # events块
...
}
http { # http块
... # http全局块
server { # server块
... # server全局块
location [PATTERN] { # location块
...
}
location [PATTERN] {
...
}
}
server {
...
}
... # http全局块
}
1)、全局块
主要设置一些影响Nginx服务器整体运行的配置指令,通常包括配置运行Nginx服务器的用户、允许生成的worker process数、Nginx进程PID存放路径、日志的存放路径和类型以及配置文件引入等
2)、events块
events块涉及的指令主要影响Nginx服务器与用户的网络连接。常用到的设置包括是否开启对多worker process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型处理连接请求,每个worker process可以同时支持的最大连接数等
3)、http块
http块配置代理、缓存和日志定义等绝大多数的功能和第三方模块,可以在http全局块中配置的指令包括文件引入、MIME-Type定义、日志自定义、是否使用sendfile传输文件、连接超时事件、单连接请求数上限等
4)、server块
虚拟主机:将一台服务器的某项或者全部服务内容逻辑划分为多个服务单位,对外表现为多个服务器,从而充分利用服务器硬件资源
每一个http块都可以包含多个server块,而每个server块就相当于一台虚拟主机,它内部可有多台主机联合提供服务,一起对外提供在逻辑上关系密切的一组服务
5)、location块
每个server块中可以多个location块,这些location块的主要作用是,基于Nginx服务器接收到的请求字符串,对除虚拟机名称之外的字符串进行匹配。地址定向、数据缓存和应答控制等功能都是在这部分实现