好几天不写nginx的东西了,这两天忙实习的事,也没有多看,今天腾出时间,再写一篇吧
一:nginx配置段
这里我们先来看一下nginx的配置文件,然后针对其中的主要部分进行详细解析,使用如下命令查看nginx的配置文件nginx.conf:
[master@localhost nginx]$ sudo vim conf/nginx.conf
nginx.conf在nginx的安装目录下,具体与安装时的目录有关。
//(1) 全局区 worker_processes 1; // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
//(2)
Event {
// 一般是配置nginx连接的特性
// 如1个word能同时允许多少连接
worker_connections 1024; // 这是指一个子进程最大允许连1024个连接
}
//(3)
http { //这是配置http服务器的主要段
Server1 { // 这是虚拟主机段
Location { //定位,把特殊的路径或文件再次定位,如image目录单独处理
} /// 如.php单独处理
}
Server2 {
}
}
这是常规的配置文件的格式,在这个配置文件中,我们可以进行一系列的优化,以增强nginx的性能,但这不是这次的重点,因此强调这个文件本身。
(1)这是nginx的全局配置区
(2)这是配置nginx的连接特性的配置区。在这里可以配置一个worker允许连接的文件数,使用了什么通信模型(select,epoll等)
(3)这是http服务器的配置段。其中server是虚拟主机配置,虚拟主机可以配置多个。
二:虚拟主机配置
我们来看两个虚拟主机的配置示例:基于域名和基于端口的虚拟主机
//例子1: 基于域名的虚拟主机
server {
listen 80; #监听端口
server_name a.com; #监听域名
location / {
root/var/www/a.com; #根目录定位
indexindex.html;
}
}
//例子2: 基于端口的虚拟主机配置
server {
listen 8080;
server_name 192.168.1.204;
location / {
root/var/www/html8080;
indexindex.html;
}
}
三:日志管理
我们观察nginx的server段,可以看到如下类似信息
#access_log logs/host.access.log main;
这说明 该server, 它的访问日志的文件是 logs/host.access.log ,
使用的格式”main”格式.
除了main格式,你可以自定义其他格式.
main格式是什么?
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
# '$status $body_bytes_sent"$http_referer" '
# '"$http_user_agent""$http_x_forwarded_for"';
main格式是我们定义好一种日志的格式,并起个名字,便于引用.
以上面的例子, main类型的日志,记录的 remote_addr.... http_x_forwarded_for等选项.
1: 日志格式 是指记录哪些选项
默认的日志格式: main
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent""$http_x_forwarded_for"';
如默认的main日志格式,记录这么几项
远程IP- 远程用户/用户时间 请求方法(如GET/POST) 请求体body长度 referer来源信息
http-user-agent用户代理/蜘蛛 ,被转发的请求的原始IP
http_x_forwarded_for:在经过代理时,代理把你的本来IP加在此头信息中,传输你的原始IP
2: 声明一个独特的log_format并命名
log_format mylog '$remote_addr- "$request" '
'$status$body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
在下面的server/location,我们就可以引用 mylog
在server段中,这样来声明
Nginx允许针对不同的server做不同的Log ,(有的web服务器不支持,如lighttp)
access_log logs/access_8080.log mylog;
声明log log位置 log格式;实际应用:shell+定时任务+nginx信号管理,完成日志按日期存储
分析思路: 凌晨00:00:01,把昨天的日志重命名,放在相应的目录下,再USR1信息号控制nginx重新生成新的日志文件
具体脚本:
#!/bin/bash
base_path='/usr/local/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
#echo $base_path/$log_path/access_$day.log
kill-USR1 `cat /usr/local/nginx/logs/nginx.pid`
定时任务
Crontab 编辑定时任务
01 00 * * * /xxx/path/b.sh 每天0时1分(建议在02-04点之间,系统负载小)
四:location语法
location 有”定位”的意思, 根据Uri来进行不同的定位.在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.比如, 碰到.php, 如何调用PHP解释器? --这时就需要locationlocation 的语法location [=|~|~*|^~] patt {}中括号可以不写任何参数,此时称为一般匹配也可以写参数因此,大类型可以分为3种location = patt {} [精准匹配]location patt{} [一般匹配]location ~ patt{} [正则匹配]
如何发挥作用?:
首先看有没有精准匹配,如果有,则停止匹配过程.
location = patt {
config A
}
如果 $uri ==patt,匹配成功,使用configA
location = / {
root /var/www/html/;
index index.htm index.html;
}
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
如果访问 http://xxx.com/
定位流程是
1: 精准匹配中 ”/” ,得到index页为 index.htm
2: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
3: 最终结果,访问了/usr/local/nginx/html/index.htm
再来看,正则也来参与.
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location ~ image {
root /var/www/image;
index index.html;
}
如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png”匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用.
图片真正会访问 /var/www/image/logo.png
location = / {
root /var/www/html/;
index index.htm index.html;
}
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
如果访问 http://xxx.com/