Nginx技术深入剖析

Nginx软件功能模块说明

核心功能模块(Core functionality):主要对应配置文件的Main区块和Events区块。

标准的http功能模块:

企业 场景常用的Nginx http功能模块汇总
ngx_http_core_module 包括一些核心的http参数配置,对应Nginx的配置为HTTP区块部分
ngx_http_access_module 访问控制模块,用来控制网站用户对Nginx的访问
ngx_http_gzip_module 压缩模块,对Nginx返回的数据压缩,术语性能优化模块
ngx_http_fastcgi_module FastCGI模块,和动态应用相关的模块,例如PHP
ngx_http_proxy_module proxy代理模块
ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_module URL地址重写模块
ngx_http_limit_conn_module 限制用户并发连接数及请求数模块
ngx_http_limit_req_module 根据定义的key限制Nginx请求过程的速率
ngx_http_log_module 访问日志模块,以指定的格式记录Nginx客户访问日志等信息
ngx_http_auth_basic_module Web认证模块,设置Web用户通过账号、密码访问Nginx
ngx_http_ssl_module ssl模块,用于加密的http连接,如https
ngx_http_stub_status_module 记录Nginx基本访问状态信息等的模块

Nginx目录结构说明

├── client_body_temp
├── conf             #Nginx所有配置文件的目录
│   ├── fastcgi.conf         #fastcgi相关参数的配置文件
│   ├── fastcgi.conf.default     #fastcgi.conf的原始配置文件
│   ├── fastcgi_params                       #fastcgi的参数文件
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types         #媒体类型
│   ├── mime.types.default
│   ├── nginx.conf           #Nginx默认的主配置文件
│   ├── nginx.conf.02
│   ├── nginx.conf.default
│   ├── scgi_params         #scgi参数文件,一般用不到
│   ├── scgi_params.default
│   ├── uwsgi_params         #uwsgi参数文件,一般用不到
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp         #fastcgi临时数据目录
├── html               #编译安装Nginx默认站点目录,类似Apache的默认站点                                                                #htdocs目录
│   ├── 50x.html                                 #错误页面优雅替代显示文件,例如出现502错误时,会调

                                                            #调用此页面,error_page 500 502 503 504 /50x.html
│   ├── index.html                              #默认的首页文件,在实际环境中,大家习惯用(不是必                                                        #须)index.html,index.php,index.jsp来做网站的首页文                                                             #件,首页文件名在nginx.conf中事先定义好的。
│   └── index.html.2017-10-22
├── logs              #Nginx默认的日志路径,包括错误日志及访问日志
│   ├── access.log        #这是Nginx的默认访问日志文件,使用tail -f access.log可以                    #实现观看用网站用户的访问情况信息
│   ├── error.log         #Nginx的错误日志文件,如果Nginx出现启动故障等问题,

                  #可以通过查看这个文件查明原因。
│   └── nginx.pid          #Nginx的pid文件,Nginx进程启动后,会把进程的ID号写入。
├── proxy_temp           #临时目录 
├── sbin             #这是Nginx的命令目录,如Nginx的启动命令nginx
│   └── nginx
├── scgi_temp           #临时目录
└── uwsgi_temp           #临时目录

[root@localhost conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;        #worker进程的数量
events {               #事件区块的开始
worker_connections 1024;      #每个worker进程支持的最大连接数
}                   #事件区块的结束
http {                 #http区块开始
include mime.types;                             #nginx支持的媒体类型库文件
default_type application/octet-stream; #默认的媒体类型
sendfile on;                                          #开启高效传输模式
keepalive_timeout 65;                          #连接超时
server {                                                #第一个Server区块开始,表示一个独立的虚拟主机站点
listen 80;                                              #提供服务的端口,默认80
server_name localhost;                        #提供服务的域名主机名
location / {                                            #第一个location区块的开始
root html;                                              #站点的根目录,相当于Nginx的安装目录
index index.html index.htm;                   #默认的首页文件,多个用空格分开
}                                                            #第一个location区块结束
error_page 500 502 503 504 /50x.html;#出现对应http状态码时,使用50x.html回应客户

location = /50x.html {        #location区块开始,访问50x.html
root html;
}
}
}                  #http区块结束

企业场景中重启Nginx后的检测策略

在企业运维实践场中,每一个配置操作处理完毕后都应该进行快速有效的检查,这是一个合格运维人员的良好习惯。尽量使得在Nginx启动的同时,还会调用脚本通过获取header信息或模拟用户访问指定URL(wget等方式)来自动检查Nginx是否正常,最大限度的保证服务重启后,能迅速确定网站情况,而无须手工敲命令查看。这样如果配置有问题就可以迅速使用上一版本备份配置文件覆盖回来。

[root@localhost conf]# cat check_url.sh
#!/bin/bash
#author:chenfu 2017-10-22 QQ532088799
#--------function split--------
. /etc/rc.d/init.d/functions
function checkURL()
{
checkUrl=$1
echo 'check url start ...'
judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1 | tr " " " "))
if [[ "${judge[1]}" == '200' && "${judge[2]}" == 'OK' ]]
then
action "${checkUrl}" /bin/true
else
action "${checkUrl}" /bin/false
echo -n "retrying again...";sleep 3;
judgeagain=($(curl -I -s --connect-timeout 2 ${checkUrl}| head -1| tr " " " "))
if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}" == 'OK' ]]
then
action "${chekcUrl}, retried again" /bin/true
else
action "${chekcUrl}, retried again" /bin/false
fi
fi
sleep 1;
}
# usage method
checkURL http://www.etiantian.org

Nginx status结果详解

Active connections: 1         #Nginx正处理的活动连接数有1个
server accepts handled requests   #第一个server表示Nginx启动到现在共处理20个连接

                    #第二个accepts表示Nginx启动到现在共成功创建20次握                                                                #手

                   #第三个handled requests表示共处理了23次请求

20 20 23
Reading: 0 Writing: 1 Waiting: 0    #Reading为Nginx读取到客户端的Header信息数

                   #Writing为Nginx返回给客户端的Header数

                                                                #Waiting为Nginx已经处理完正等候下一次请求指令的驻                                                                  #留连接。在开启keepalive的情况下

                                                                #这个值等于active-(reading+writing)

Nginx增加错误日志(error_log)配置

日志记录属于核心功能模块(ngx_core_module)的参数,该参数名字是error.log,针对自己来说最好定义在Main全局区块中,当然也可以放置不同的虚拟主机中单独记录。

error_log    file    level[debug|info|notice|warn|error|crit|alert|emerg];

关键字    日志文件  错误日志级别

error_log默认配置:

#default:error_log logs/error.log error;

可以放置的标签段:

#context:main,http,server,location

Nginx访问日志(access_log)

此功能由ngx_http_log_module模块负责

控制日志的参数
参数 说明
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同的名字即可)
access_log 用来指定日志文件的路径及使用何种日志格式记录日志
Nginx日志变量说明
Nginx日志变量 说明
$remote_addr 记录访问网站的客户端地址

$http_x_forwarded_for

当前端有代理服务器时,设置Web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也进行了相关的x_forwarded_for设置
$remote_user 远程客户端名称
$time_local 记录访问时间与时区
$request 用户的http请求起始行信息
$status http状态码,记录请求返回的状态,例如200,404,301
$body_bytes_sents 服务器发送给客户端的响应body字节数
$http_referer 记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置
$http_user_agent 记录客户端访问信息,例如浏览器手机客户端等

buffer=size            #存放访问日志的缓冲区大小

flush=time             #将缓冲区的日志刷到磁盘的时间

gzip[=level]              #表示压缩级别

[if=condition]           #表示其他条件(一般的场景中这些都无需配置,极端优化                                                              #时才可能会考虑这些参数)

默认的配置:

access_log    logs/access.log     combined;

日志内容配置:

log_format main '$remote_addr - $remote_user [$time_local] "$request"'
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

Nginx访问日志轮询切割

[root@localhost nginx]# cat /server/script/cut_nginx_log.sh
#!/bin/bash
Dateformat=`date +%Y%m%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

Nginx常用的日志收集分析工具有rsyslog、awstats、flume、ELK、storm等

原文地址:https://www.cnblogs.com/cf532088799/p/7710346.html