Nginx主机及日志配置(三)

第1章 Nginx虚拟主机配置

1.1 虚拟主机的概念和类型介绍

1.1.1 虚拟主机概念

虚拟主机在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。

这个独立的站点在配置里是由一定的格式的标签段标记的,对于Apache软件来说一个虚拟主机的标签段通常被包含在<VirtualHost></VirtualHost>内,而Nginx软件则使用一个server{}标签来标识一个虚拟主机,一个web服务里可以有多个虚拟主机标签对,即可以支持多个虚拟主机站点。

1.1.2 虚拟主机的类型

  • 基于域名的虚拟主机

基于域名的虚拟主机是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业里应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机。例如:blog.leon.com

  • 基于端口的虚拟主机

基于端口的虚拟主机即通过不同的端口来区分不同的虚拟主机,此类虚拟主机主要应用在公司内部网络。例如:一些不希望直接对外提供用户访问的网站后台等,blog.leon.com:8080

  • 基于IP的虚拟主机

基于IP的虚拟主机即通过不同的IP区分不同的虚拟主机,此类虚拟主机在企业应用中较少,一般不同的业务如果需要使用不同的IP都会在负载均衡器上进行绑定,而不是在web上绑定IP。

1.2 基于域名的主机配置

1.2.1 配置nginx.conf文件

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes 3;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

server { #创建www虚拟主机
listen 80;
server_name www.leonshadow.com; #设置www虚拟主机域名
location / {
root html/www; #设置www虚拟主机对应的目录
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server { #创建blog虚拟主机
listen 80;
server_name blog.leonshadow.com; #设置blog虚拟主机域名
location / {
root html/blog; #设置blog虚拟主机对应的目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server { #创建bbs虚拟主机
listen 80;
server_name bbs.leonshadow.com; #设置bbs虚拟主机域名
location / {
root html/bbs; #设置bbs虚拟主机对应的目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
提示:vim可以利用%在server一对花括号之间进行快速切换。

1.2.2 创建对应的站点目录和文件

[root@web01 conf]# mkdir -p /application/nginx/html/{www,blog,bbs} #创建不同虚拟主机的目录
[root@web01 conf]# for n in www blog bbs;do cd /application/nginx/html/$n;echo "$n `hostname`" > index.html;done #制作不同的主页
[root@web01 ~]# tree /application/nginx/html/
/application/nginx/html/
├── 50x.html
├── bbs
│ └── index.html
├── blog
│ └── index.html
├── index.html
└── www
└── index.html

3 directories, 5 files

1.2.3 检查语法并重新加载Nginx

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 ~]# nginx -s reload
[root@web01 ~]# ps -ef | grep nginx
root 26911 1 0 03:47 00:00:00 nginx: master process nginx
#由于配置文件中配置了worker_processes为3,所以此处有3个worker process线程
www 27383 26911 0 09:06 00:00:00 nginx: worker process
www 27384 26911 0 09:06 00:00:00 nginx: worker process
www 27385 26911 0 09:06 00:00:00 nginx: worker process
root 27394 27299 0 09:10 pts/0 00:00:00 grep nginx
[root@web01 ~]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 26911/nginx
提示:出现“syntax is OK”和“successful”则说明语法正确。

1.2.4 配置测试客户端hosts文件

  • Linux客户端配置:
[root@web01 ~]# echo "10.0.0.8 www.leonshadow.com blog.leonshadow.com bbs.leonshadow.com" >> /etc/hosts
[root@web01 ~]# tail -1 /etc/hosts
10.0.0.8 www.leonshadow.com blog.leonshadow.com bbs.leonshadow.com
[root@web01 ~]# ping www.leonshadow.com
PING www.leon.com (10.0.0.8) 56(84) bytes of data.
64 bytes from www.leon.com (10.0.0.8): icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from www.leon.com (10.0.0.8): icmp_seq=2 ttl=64 time=0.063 ms
64 bytes from www.leon.com (10.0.0.8): icmp_seq=3 ttl=64 time=0.050 ms
^C
--- www.leonshadow.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2105ms
rtt min/avg/max/mdev = 0.050/0.073/0.107/0.025 ms
  • Windows客户端配置:

在hosts文件中添加一行“10.0.0.8 www.leonshadow.com blog.leonshadow.com bbs.leonshadow.com”

图片[1]|Nginx主机及日志配置(三)|leon的博客

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:

# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost

10.0.0.8 www.leonshadow.com blog.leonshadow.com bbs.leonshadow.com #添加此行

1.2.5 开始进行测试

  • www端测试:
[root@web01 ~]# curl www.leon.com
www web01
  • blog端测试:
[root@web01 ~]# curl blog.leon.com
blog web01
  • bbs端测试:
[root@web01 ~]# curl bbs.leon.com
bbs web01

1.3 基于端口的虚拟主机配置

主要用于内部网络人员访问虚拟主机使用,不想提供直接对外访问的虚拟主机。

1.3.1 配置nginx.conf文件

[root@web01 ~]# cp /application/nginx/conf/nginx.conf{,.bak-name} #备份基于域名的虚拟主机配置
[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8001; #配置www虚拟主机端口为8001
server_name www.leonshadow.com; #域名是www.leon.com
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 8002; #配置www虚拟主机端口为8001
server_name www.leonshadow.com; #域名是www.leon.com
location / {
root html/blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 8003; #配置www虚拟主机端口为8001
server_name www.leonshadow.com; #域名是www.leon.com
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

1.3.2 检查语法并重新加载Nginx

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 ~]# nginx -s reload
#打开了3个端口
[root@web01 ~]# netstat -lntup | grep nginx
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 26911/nginx
tcp 0 0 0.0.0.0:8002 0.0.0.0:* LISTEN 26911/nginx
tcp 0 0 0.0.0.0:8003 0.0.0.0:* LISTEN 26911/nginx

1.3.3 开始进行测试

  • www端测试:
[root@web01 ~]# curl www.leon.com:8001
www web01
  • blog端测试:
[root@web01 ~]# curl www.leon.com:8002
blog web01
  • bbs端测试:
[root@web01 ~]# curl www.leon.com:8003
bbs web01

1.3.4 查看访问网站的过程

[root@web01 ~]# curl -v www.leon.com
* About to connect() to www.leon.com port 80 (#0)
* Trying 10.0.0.8... connected
* Connected to www.leon.com (10.0.0.8) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.leon.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.10.3
< Date: Tue, 12 Sep 2017 12:59:00 GMT
< Content-Type: text/html
< Content-Length: 10
< Last-Modified: Tue, 12 Sep 2017 11:23:10 GMT
< Connection: keep-alive
< ETag: "59b7c39e-a"
< Accept-Ranges: bytes
<
www web01
* Connection #0 to host www.leon.com left intact
* Closing connection #0
  • 访问网站流程:
  1. 首先需要根据域名解析获取到网站的ip地址,找寻网站的ip地址对应的服务器;
  2. 其次需要根据请求建立连接的目标端口信息,找寻网站的相应服务端口是否存在;
  3. 再次需要根据请求域名信息获悉相应的站点,找寻网站的相应站点目录下的资源信息;
  4. 最后如果ip地址加端口信息都已找到,但没有找到对应的域名信息,会按照默认原则使用第一个虚拟主机作为默认访问的虚拟站点目录

1.4 基于IP的虚拟主机配置

1.4.1 在服务器网卡上增加多个IP

[root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0 #在eth0上增加辅助IP:10.0.0.9
[root@web01 ~]# ip addr add 10.0.0.10/24 dev eth0 #在eth0上增加辅助IP:10.0.0.10
[root@web01 ~]# ip addr | grep 10.0.0
inet 10.0.0.8/24 brd 10.0.0.255 scope global eth0
inet 10.0.0.9/24 scope global secondary eth0
inet 10.0.0.10/24 scope global secondary eth0
[root@web01 ~]# ping 10.0.0.9
PING 10.0.0.9 (10.0.0.9) 56(84) bytes of data.
64 bytes from 10.0.0.9: icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from 10.0.0.9: icmp_seq=2 ttl=64 time=0.017 ms
...
[root@web01 ~]# ping 10.0.0.10
PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data.
64 bytes from 10.0.0.10: icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from 10.0.0.10: icmp_seq=2 ttl=64 time=0.017 ms
...

1.4.2 配置nginx.conf文件

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 10.0.0.8:80; #此处IP后端口可不写,默认为80端口
server_name <a href="http://www.leon.com">www.leonshadow.com</a>; #此处也可改为10.0.0.8
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 10.0.0.9; #默认为80端口
server_name www.leonshadow.com; #此处也可改为10.0.0.9
location / {
root html/blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

server {
listen 80;
server_name 10.0.0.10; #此处修改为10.0.0.10
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
提示:Vim批量修改文件方法:
  1. ctrl+v:进入可视化模块模式,实现批量编辑操作,按方向键,上下选择多行内容
  2. shift+i:编辑选中的第一行
  3. esc:退出编辑,完成多行内容统一设置
  4. r:直接替换字符信息
  5. x:删除当前光标所在位置字符信息

1.4.3 检查语法并重新加载Nginx

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 ~]# nginx -s reload
#显示3个IP
[root@web01 ~]# netstat -lntup | grep nginx
tcp 0 0 10.0.0.10:80 0.0.0.0:* LISTEN 26911/nginx
tcp 0 0 10.0.0.9:80 0.0.0.0:* LISTEN 26911/nginx
tcp 0 0 10.0.0.8:80 0.0.0.0:* LISTEN 26911/nginx
注意:nginx配置文件如果有和IP地址相关的配置变化,nginx服务需要重新启动生效,不能采用平滑重启方式(nginx-1.12.1可以直接平滑重启了)。
提示:更多nginx命令行参数说明:http://nginx.org/en/docs/switches.html

1.4.4 开始进行测试

  • www端测试:
[root@web01 ~]# curl 10.0.0.8
www web01
  • blog端测试:
[root@web01 ~]# curl 10.0.0.9
blog web01
  • bbs端测试:
[root@web01 ~]# curl 10.0.0.10
bbs web01

1.5 Nginx配置虚拟主机小结

1.5.1 配置虚拟主机的步骤

  • 增加一个完整的server标签段到结尾处。注意要放在http的结束大括号前,也就是将server标签段放入http标签
  • 更改server_name及对应网页的root更目录,如果需要其他参数可以增加或修改
  • 创建server_name域名对应网页的更目录并且建立测试文件,如果没有html首页访问时会出现403错误(apache没有首页文件时默认会把站点目录下面的信息显示出来)
  • 检查Nginx配置文件语法,平滑重启Nginx服务,快速检查启动结果
  • 在客户端对server_name处配置的域名做host解析或DNS配置,并检查(ping域名看返回的IP是否正确)
  • 在浏览器中输入地址访问或在linux客户端做hosts解析,用wget或curl接地址访问
提示:Nginx虚拟主机帮助:http://nginx.org/en/docs/http/request_processing.html

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

启动Nginx的同时调用脚本通过获取header信息或模拟用户访问指定URL(wget等方式)来自动检查Nginx的启动是否正常,最大限度地办证服务重启后,能迅速确定网站情况而无需手工敲命令查看,如果配置文件有问题(非语法问题)就可以迅速使用上一版本备份文件进行恢复,使得影响用户的时间最短。

  • 编写脚本示例(不可用):
[root@web01 scripts]# vim check_url.sh
#!/bin/bash

. /etc/init.d/functions
function checkURL()
{
checkUrl=$1
echo 'check url start ...'
judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1|tr " " "\n"))
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 " " "\n"))
if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}" == 'OK' ]];then
action "${checkUrl},retried again" /bin/true
else
action "${checkUrl},retried again" /bin/false
fi
fi
sleep 1;
}
  • 可测试的脚本:
[root@web01 scripts]# vim check_url.sh
#!/bin/bash
#. /etc/init.d/functions
#function checkURL()
#{
checkUrl=$1
echo 'check url start ...'
judge=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1|xargs -n1|tr "\r" " "))
if [[ "${judge[1]}" == '200' && "${judge[2]}" == 'OK' ]];then
#action "${checkUrl}" /bin/true
echo "${checkUrl} is OK"
else
#action "${checkUrl}" /bin/false
echo -n "retrying again...";sleep 3;
judgeagain=($(curl -I -s --connect-timeout 2 ${checkUrl}|head -1|xargs -n1|tr "\r" " "))
if [[ "${judgeagain[1]}" == '200' && "${judgeagain[2]}" == 'OK' ]];then
#action "${checkUrl},retried again" /bin/true
echo "${checkUrl} is OK"
else
#action "${checkUrl},retried again" /bin/false
echo "${checkUrl} is not OK"
fi
fi
sleep 1;
#}
  • 脚本说明:

此脚本检测访问www网站时的响应header是否返回200,以及状态是否为OK,当检测失败时,3秒后再检测一次,然后格式化输出结果并输出,如果有多个域名可以对上述命令传参的不同域名循环检查。检测方法可以是端口、URI、响应header等,具体根据业务需求进行选择。

  • 使用function函数里面的内容进行测试
[root@web01 scripts]# sh check_url.sh www.leon.com
check url start ...
www.leon.com is OK

第2章 Nginx常用功能配置

2.1 规范优化Nginx配置文件介绍

Nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www.conf、bbs.conf、blog.
conf
等。如果虚拟主机不是很多也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和Nginx的主配置文件nginx.conf分离即可。

2.1.1 配置语法:

include file | mask;

2.1.2 用法示例:

include mime.types;
include www.conf; #包含单个文件
include vhosts/*.conf; #包含vhosts下所有以conf结尾的文件

2.2 优化配置实战

2.2.1 制作子配置文件:

[root@web01 ~]# mkdir -p /application/nginx/conf/extra #创建子配置文件目录
[root@web01 ~]# cd /application/nginx/conf/
[root@web01 conf]# cat -n nginx.conf.bak-name #查看配置文件行信息
1 worker_processes 3;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 sendfile on;
9 keepalive_timeout 65;
10 server {
11 listen 80;
12 server_name www.leonshadow.com;
13 location / {
14 root html/www;
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 server {
23 listen 80;
24 server_name blog.leonshadow.com;
25 location / {
26 root html/blog;
27 index index.html index.htm;
28 }
29 error_page 500 502 503 504 /50x.html;
30 location = /50x.html {
31 root html;
32 }
33 }
34 server {
35 listen 80;
36 server_name bbs.leonshadow.com;
37 location / {
38 root html/bbs;
39 index index.html index.htm;
40 }
41 error_page 500 502 503 504 /50x.html;
42 location = /50x.html {
43 root html;
44 }
45 }
46 }

#制作子配置文件
[root@web01 conf]# sed -n '10,21p' nginx.conf.bak-name > extra/www.conf #将10到21行写入www.conf
[root@web01 conf]# sed -n '22,33p' nginx.conf.bak-name > extra/blog.conf#将22到33行写入blog.conf
[root@web01 conf]# sed -n '34,45p' nginx.conf.bak-name > extra/bbs.conf #将34,45行写入bbs.conf

#查看子配置文件目录结构
[root@web01 conf]# cd extra/
[root@web01 extra]# tree
.
├── bbs.conf
├── blog.conf
└── www.conf

0 directories, 3 files

#查看子配置文件内容
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.leonshadow.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

[root@web01 extra]# cat blog.conf
server {
listen 80;
server_name blog.leonshadow.com;
location / {
root html/blog;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

[root@web01 extra]# cat bbs.conf
server {
listen 80;
server_name bbs.leonshadow.com;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

2.2.2 制作主配置文件:

[root@web01 conf]# sed '10,45d' nginx.conf.bak-name > nginx.conf
[root@web01 conf]# vim nginx.conf
worker_processes 3;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
}
注意:nginx配置文件每一行参数结尾都需要输入分号“;”。

2.2.3 检查并重新加载配置文件

[root@web01 conf]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 conf]# nginx -s reload
[root@web01 html]# netstat -lntup | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 32279/nginx

2.2.4 测试结果

最终测试结果同基于域名的虚拟主机相同。

2.3 虚拟主机的别名介绍

虚拟主机别名就是为虚拟主机设置除了主域名以外的一个或多个域名名字,这样就能实现用户访问的多个域名对应同一个虚拟主机网站的功能。工作中可以通过别名的配置实现多域名访问一个站点,例如:baidu.com和www.baidu.com就是一样的内容(也可以通过rewrite 301跳转的配置思路)。

提示:还可以使用别名进行监控集群下面RS的URL是否正常,因为集群节点的域名都是同一个,所以如果不使用别名则很难通过URL的方式检测判断节点下面的机器是否正常。《跟老男孩学Linux运维:web集群实战》P180

2.4 虚拟主机别名配置

2.4.1 编辑虚拟主机配置文件

原始虚拟主机配置(修改前www.conf内容) 带别名虚拟主机配置(修改后www.conf内容
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.leonshadow.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.leonshadow.com www.leon.shadow.com;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

说明:仅在server_name所在行尾增加别名即可完成虚拟主机别名设置。

[root@web01 extra]# diff www.conf www.conf.bak
3c3
< server_name www.leonshadow.com www.leon.shadow.com;
---
> server_name www.leonshadow.com;

2.4.2 检查并重启nginx服务

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 ~]# nginx -s reload

2.4.3 更改hosts文件进行域名解析

[root@web01 ~]# tail -1 /etc/hosts
10.0.0.8 www.leonshadow.com blog.leonshadow.com bbs.leonshadow.com www.leon.shadow.com

2.4.4 测试结果

[root@web01 ~]# curl www.leonshadow.com
www web01
[root@web01 ~]# curl www.leon.shadow.com
www web01

2.5 Nginx状态信息功能

Nginx软件功能模块中有一个ngx_http_stub_status_module模块,这个模块主要功能就是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态,例如连接数等信息。要使用此模块在编译时必须增加http_stub_status_module模块。

  • 检查是否安装了http_stub_status_module模块:
[root@web01 ~]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.10.3 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module

2.6 配置Nginx 状态信息介绍

2.6.1 编写状态配置文件

[root@web01 ~]# vim /application/nginx/conf/extra/status.conf
server{
listen 80;
server_name status.leon.com;
location / {
stub_status on;
access_log off;
}
}

2.6.2 引用状态配置文件

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes 3;
error_log logs/error.log;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
include extra/status.conf;
}

2.6.3 检查并重启服务

[root@web01 ~]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 ~]# nginx -s reload

2.6.4 配置hosts解析

  • Windows客户端设置:
10.0.0.8 www.leon.com blog.leon.com bbs.leon.com status.leon.com
  • Linux客户端设置:
[root@web01 ~]# tail -1 /etc/hosts
10.0.0.8 www.leon.com blog.leon.com bbs.leon.com www.leon.shadow.com status.leon.com

2.6.5 测试结果

  • Windows客户端测试结果:

图片[2]|Nginx主机及日志配置(三)|leon的博客

  • Linux客户端测试结果:
[root@web01 ~]# curl status.leon.com
Active connections: 1
server accepts handled requests
14 14 24
Reading: 0 Writing: 1 Waiting: 0

2.6.6 Nginx状态模块显示信息详解

这个模块默认不存在,需要在安装编译的时候启用相应配置参数–with-http_stub_status_module。

  • 基本情况:
Syntax: stub_status; #语法格式
Default: — #无默认值
Context: server, location #可应用于的区块
  • 配置示例:
location /basic_status {
stub_status;
}
  • 参数信息说明:
参数信息 说明
Active connections 当前活动客户端连接数量(包含Waiting连接数量)
accepts 接收客户端连接的总数量
handled 处理连接的总数量
requests 客户端请求的总数
Reading 当前nginx正在读取请求头的连接数
Writing 当前nginx将响应写回客户机的连接数量
Waiting 当前空闲客户端连接等待请求的数量

图片[3]|Nginx主机及日志配置(三)|leon的博客

  • 变量信息:
变量信息 说明
$connections_active 等价于Active connections valus
$connections_reading 等价于Reading数值
$connections_writing 等价于Writing数值
$connections_waiting 等价于waiting数值
  • 显示示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
参考资料:http://nginx.org/en/docs/http/ngx_http_stub_status_module.html#stub_status

2.6.7 【扩展】对状态模块访问设置限制,只允许部分网段主机可以访问状态模块页面信息:

location /nginx_status {
stub_status on;
access_log off;
allow 10.0.0.0/24; #设置允许和禁止的IP段访问
deny all; #设置允许和禁止的IP段访问
}

2.7 Nginx错误日志介绍

Nginx错误日志信息属于核心功能模块(ngx_core_module)的参数,该参数名字为error_log,可以放在Main区块中全局配置,也可以放置在不同的虚拟主机中单独记录。

2.7.1 error_log的语法格式及参数说明

error_log <em>file </em>[<em>level</em>];

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

  • 参数说明:
  1. 关键字error_log不能改变
  2. 日志文件可以指定任意存放日志的目录
  3. 错误日志级别常见的有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少,生产场景一般是warn|error|crit这三个级别之一,注意不要配置info等较低级别,否则会消耗巨大的磁盘I/O

2.7.2 error_log默认值

#default: error_log logs/error.log error;

2.7.3 可以放置的标签段

#context: main, http, mail, stream, server, location

2.8 Nginx错误日志的配置

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes 3;
error_log logs/error.log; #增加此行即可
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
}
参考资料:http://nginx.org/en/docs/ngx_core_module.html#error_log

第3章 Nginx访问日志

3.1 Nginx访问日志介绍

Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责。

对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html

3.2 访问日志参数

参数 说明
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同的名字即可)
access_log 用来指定日志对文件的路径及使用何种日志格式记录日志

3.2.1 Nginx日志格式默认参数配置

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
  • 日志格式定义:

配置位置在http标签内:

[root@web01 ~]# vim /application/nginx/conf/nginx.conf.default
...
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 #tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65;
32
33 #gzip on;
34
35 server {
36 listen 80;
37 server_name localhost;
...

main是为日志格式指定的标签(可自定义),记录日志时通过main标签选择指定的格式,其后所接的所有内容都是可以记录的日志信息。

  • 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_semts 服务器发送给客户端的相应body字节数
$http_referer 记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置
$http_user_agent 记录客户端访问信息,例如:浏览器、手机客户端等

提示:更多设置可参看:http://nginx.org/en/docs/http/ngx_http_log_module.html

3.2.2 Nginx记录日志默认参数配置

#access_log logs/access.log main;
  • 配置语法:
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except
  • 参数说明:
  1. buffer=size:存放访问日志的缓冲区大小
  2. flush=time:将缓冲区的日志存储到磁盘的时间
  3. gzip[=level]:表示压缩级别
  4. [if=condition]:表示其他条件
  5. access_log off:表示不记录访问日志
  • 默认配置:assess_log logs/access.log combined
  • 放置位置:http、server、location、if in location、limit_except中

3.3 访问日志配置

3.3.1 编辑主配置文件nginx.conf

[root@web01 ~]# vim /application/nginx/conf/nginx.conf
worker_processes 3;
error_log logs/error.log;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

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

sendfile on;
keepalive_timeout 65;

include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
include extra/status.conf;
}

3.3.2 在每个虚拟主机中配置

[root@web01 conf]# cd /application/nginx/conf/extra/
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name www.leonshadow.com www.leon.shadow.com;
location / {
root html/www;
index index.html index.htm;
autoindex on;
}

access_log logs/access_www.log main;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root html;
}
}

[root@web01 extra]# vim blog.conf
server {
listen 80;
server_name blog.leonshadow.com;
location / {
root html/blog;
index index.html index.htm;
}

access_log logs/access_blog.log main;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root html;
}
}

[root@web01 extra]# vim bbs.conf
server {
listen 80;
server_name bbs.leonshadow.com;
location / {
root html/bbs;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
access_log logs/access_bbs.log main;

location = /50x.html {
root html;
}
}

3.3.3 检查并重载配置

[root@web01 extra]# nginx -t
nginx: the configuration file /application/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.3/conf/nginx.conf test is successful
[root@web01 extra]# nginx -s reload

3.3.4 查看日志记录

[root@web01 ~]# cd /application/nginx/logs/
[root@web01 logs]# ls
access_bbs.log access_blog.log access.log access_www.log error.log nginx.pid
[root@web01 logs]# tail -f access_www.log
10.0.0.253 - - [12/Sep/2017:19:25:46 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36" "-"
10.0.0.253 - - [12/Sep/2017:19:25:56 +0800] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36" "-"
  • 日志信息详细说明

图片[4]|Nginx主机及日志配置(三)|leon的博客

$remote_addr 10.0.0.253 #访问端(客户端)IP地址信息
- - #分隔符
$remote_user - #显示远程访问者用户信息
[$time_local] [12/Sep/2017:19:25:46 +0800] #显示访问时间信息
$request GET / HTTP/1.1 #请求行信息
$status 304 #状态码信息
$body_bytes_sent 0 #响应报文主体内容大小
$http_referer - #记录链接从哪里来的
$http_user_agent Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
#客户端浏览网页工具信息
$http_x_forwarded_for - #与nginx反向代理相关
提示:谷歌隐身模式:不会留下缓存
  1. 缓存信息抓包后显示的状态码为304
  2. nginx里面看缓存304的访问记录,访问nginx日志信息,访问尺寸为0

3.4 Nginx访问日志轮询切割

脚本实现切割Nginx日志的思想是将正在写入的Nginx日志改名为带日期的格式文件,然后平滑重新加载Nginx,生成新的Nginx日志。

3.4.1 编写切割日志脚本:

[root@web01 scripts]# vim cut_nginx_log.sh
#!/bin/bash
Dateformat=`date +%F`
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

3.4.2 定时任务实现按时切割日志

[root@web01 logs]# crontab -e
#cut nginx access log
00 00 * * * /bin/bash /server/scripts/cut_nginx_log.sh &> /dev/null

3.4.3 日志切割效果

[root@web01 logs]# ll | grep www
-rw-r--r-- 1 root root 577 Sep 12 19:33 2017-09-12_access_www.log
-rw-r--r-- 1 root root 0 Sep 12 20:31 access_www.log
提示:Nginx常用日志收集及分析工具:rsyslog、awstats、flume、ELK、storm等。
温馨提示:本文最后更新于2022-12-20 20:57:52,已超过487天没有更新。某些文章具有时效性,若文章内容或图片资源有错误或已失效,请联系站长。谢谢!
转载请注明本文链接:https://blog.leonshadow.cn/763482/660.html
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享