[root@web01 conf]# cd /application/nginx/conf/
[root@web01 conf]# cp nginx.conf nginx.conf.oldboy.20160513V1
[root@web01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
[root@web01 conf]# mkdir ../html/{www,bbs} -p
[root@web01 conf]# echo "www" > ../html/www/index.html
[root@web01 conf]# echo "bbs" > ../html/bbs/index.html
[root@web01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 conf]# /application/nginx/sbin/nginx
[root@web01 conf]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 11067 root 6u IPv4 27473 0t0 TCP *:http (LISTEN)
nginx 11068 www 6u IPv4 27473 0t0 TCP *:http (LISTEN)
[root@web01 conf]# netstat -lntup|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11067/nginx
打开另一台Linux机器 [root@salt-master ~]# vim /etc/hosts
172.16.1.8/24 web01 www.etiantian.org bbs.etiantian.org
[root@salt-master ~]# ping www.etiantian.org
PING www.etiantian.org.localdomain (10.0.0.8) 56(84) bytes of data.
64 bytes from 10.0.0.8: icmp_seq=1 ttl=64 time=0.340 ms
64 bytes from 10.0.0.8: icmp_seq=2 ttl=64 time=0.752 ms
[root@salt-master ~]# ping bbs.etiantian.org
PING bbs.etiantian.org (42.62.5.158) 56(84) bytes of data.
64 bytes from 42.62.5.158: icmp_seq=1 ttl=128 time=55.3 ms
64 bytes from 42.62.5.158: icmp_seq=2 ttl=128 time=53.2 ms
[root@salt-master ~]# curl www.etiantian.org
www
[root@salt-master ~]# curl bbs.etiantian.org
bbs
注意:如果以IP访问的话,默认读取的是第一个server模块里的信息
[root@web01 conf]# cd ../html/bbs/
[root@web01 bbs]# mv index.html index.html.1 # 把bbs对应的首页文件改名
bingbing gongli index.html.1 oldboy
[root@web01 bbs]# touch gongli oldboy bingbing
[root@web01 bbs]# ls
加上 autoindex on; 参数
[root@web01 bbs]# cat /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
autoindex on;
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
再次打开bbs.etiantain.org会显示当前目录下的文件并可下载
Index of /
../
bingbing 12-Feb-2017 09:48 0
gongli 12-Feb-2017 09:48 0
index.html.1 12-Feb-2017 08:19 4
oldboy 12-Feb-2017 09:48 0
[root@web01 html]# mkdir blog
[root@web01 html]# echo blog >blog/index.html
[root@web01 html]# cd bbs/
[root@web01 bbs]# ls
bingbing gongli index.html.1 oldboy
[root@web01 bbs]# mv index.html.1 index.html
[root@web01 bbs]# cd ../
[root@web01 html]# ls
bbs blog www
[root@web01 html]# vim ../conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
[root@web01 html]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 html]# /application/nginx/sbin/nginx -s reload
客户端做解析
echo "10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org" >>/etc/hosts
[root@web01 conf]# cp nginx.conf nginx.conf.basename.1
[root@web01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 81;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 82;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
[root@web01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 conf]# /application/nginx/sbin/nginx -s reload
客户端浏览器输入
即可访问
[root@web01 conf]# cp nginx.conf nginx.conf.baseport.1
[root@web01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 81;
server_name www.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 82;
server_name www.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
[root@web01 conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 conf]# /application/nginx/sbin/nginx -s reload
显示结果:www
显示结果:bbs
显示结果:blog
[root@web01 conf]# cp nginx.conf nginx.conf.baseport.2
[root@web01 conf]# ifconfig eth0:0 10.0.0.101/24 up
#两种方法设置辅助IP或IP别名
[root@web01 conf]# ip addr add 10.0.0.102/24 dev eth0 label eth0:1 #两种方法设置辅助IP或IP别名
[root@web01 conf]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:E2:09:2D
inet addr:10.0.0.8 Bcast:10.0.0.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fee2:92d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:34082 errors:0 dropped:0 overruns:0 frame:0
TX packets:21279 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:32668954 (31.1 MiB) TX bytes:2175131 (2.0 MiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:E2:09:2D
inet addr:10.0.0.101 Bcast:10.0.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
eth0:1 Link encap:Ethernet HWaddr 00:0C:29:E2:09:2D
inet addr:10.0.0.102 Bcast:0.0.0.0 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
[root@web01 conf]# ping 10.0.0.102
PING 10.0.0.102 (10.0.0.102) 56(84) bytes of data.
64 bytes from 10.0.0.102: icmp_seq=1 ttl=64 time=0.044 ms
[root@web01 conf]# ping 10.0.0.101
PING 10.0.0.101 (10.0.0.101) 56(84) bytes of data.
64 bytes from 10.0.0.101: icmp_seq=1 ttl=64 time=0.035 ms
[root@web01 conf]# vim nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 10.0.0.8:80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 10.0.0.101:80;
server_name www.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 10.0.0.102:80;
server_name www.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
[root@web01 conf]# /application/nginx/sbin/nginx -t
[root@web01 conf]# /application/nginx/sbin/nginx -s stop
[root@web01 conf]# /application/nginx/sbin/nginx
客户端上测试
[root@salt-master ~]# curl 10.0.0.8
www
[root@salt-master ~]# curl 10.0.0.101
bbs
[root@salt-master ~]# curl 10.0.0.102
blog
http://study.oldboyedu.com/classModule/video/187795/190705/665052/0/0
1)增加一个完整的server标签段到结尾处,注意要放在http的结束大括号前,也就会将server标签段放入http标签内。
2)更改server_name及对应网页的root根目录,如果需要其他参数,可以增加或修改。
3)创建server_name域名对应的网页的根目录,并且建立测试文件,如果没有index首页,访问会出现403错误。
4)检查Nginx配置文件语法,平滑重启Nginx,快速检查启动结果。
5)在客户端对server_name处配置的域名做host解析和DNS配置,并检查(ping域名看返回的IP对不对)。
6)在mac 浏览器中输入地址访问,或者在Linux客户端做hosts解析,用wget或curl接地址访问。
Nginx虚拟主机的官方帮助网站:http://Nginx.org/en/docs/http/request_processing.html
在运维实战场景中,每一个配置操作处理完毕后都应该进行快速有效的检查,这是一个合格运维人员的良好习惯。在实际工作中,启动Nginx的同时,还会调用脚本通过获取header信息或模拟用户访问指定URL(wget等方式)来自动检查Nginx的启动是否正常,最大限度地保证服务重启后,能迅速确定网站情况,而无须手工敲命令查看。这样,如果配置有问题(非语法问题,语法问题已经用-t参数检查过了),就可以迅速使用上一版备份的配置文件覆盖回来,使得影响用户的时间最短。
[root@web01 bbs]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
[root@web01 conf]# mkdir extra
[root@web01 conf]# vim nginx.conf
[root@web01 conf]# cp nginx.conf.basename.1 extra/a
[root@web01 conf]# cd extra/
[root@web01 extra]# ll
总用量 4
-rw-r--r-- 1 root root 754 2月 12 21:02 a
[root@web01 extra]# sed -n ‘10,17p‘ a
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web01 extra]# sed -n ‘10,17p‘ a >www.conf
[root@web01 extra]# sed -n ‘18,25p‘ a
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web01 extra]# sed -n ‘18,25p‘ a >bbs.conf
[root@web01 extra]# sed -n ‘26,33p‘ a
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web01 extra]# sed -n ‘26,33p‘ a >blog.conf
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
[root@web01 extra]# cat blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
[root@web01 extra]# cat bbs.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
[root@web01 extra]# /application/nginx/sbin/nginx -t
[root@web01 extra]# /application/nginx/sbin/nginx -s stop
[root@web01 extra]# /application/nginx/sbin/nginx
[root@salt-master ~]# tail -1 /etc/hosts
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org
[root@salt-master ~]# curl www.etiantian.org
www
[root@salt-master ~]# curl bbs.etiantian.org
bbs
[root@salt-master ~]# curl blog.etiantian.org
blog
[root@web01 extra]# vim www.conf
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server_name www.etiantian.org etiantian.org;只需要空格加上即可设置别名
客户端做解析
[root@salt-master ~]# vim /etc/hosts
10.0.0.8 etiantian.org www.etiantian.org bbs.etiantian.org blog.etiantian.org
[root@salt-master ~]# curl etiantian.org
www
[root@salt-master ~]# curl www.etiantian.org
www
多数企业网站希望访问www.etiantian.org和etiantian.org时,所浏览的是同一个页面,若有这类需求,就可以让etiantian.org以别名的方式出现,这时两个域名都要解析到服务器的IP地址。
在生产环境中,曾经还利用别名来监控集群下面RS的URL是否正常。如:
server_name www.etiantian.org www1.etiantian.org www2.etiantian.org;
可以在监控服务器里配置hosts来监控RS www1.etiantian.org、www2.etiantian.org等地址是否正常,进而判断每一台机器的www.etiantian.org是否正常。如果不使用别名则很难通过域名URL的方式检测判断节点下面的机器是否正常(因为这些集群节点的域名是同一个)。
Nginx软件的功能模块中有一个ngx_http_stub_status_module模块,这个模块的主要功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工作状态,例如连接数等信息。要使用状态模块,在编译Nginx时必须增加http_stub_status_module模块来支持。 可通过如下方法检查编译安装Nginx时是否设定了上述模块:
[root@web01 extra]# cat >>/application/nginx/conf/extra/status.conf<<EOF
##status
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
}
EOF
[root@web01 extra]# sed -i "13 i include extra/status.conf;" /application/nginx/conf/nginx.conf
[root@web01 extra]# /application/nginx/sbin/nginx -t
[root@web01 extra]# /application/nginx/sbin/nginx -s reload
客户端做解析:
echo "10.0.0.8 status.etiantian.org" >>/etc/hosts
[root@salt-master ~]# curl status.etiantian.org
Active connections: 2
server accepts handled requests
25 25 30
Reading: 0 Writing: 1 Waiting: 1
也可以用location的方式实现状态信息配置,例如在任意一个虚拟主机里,为server标签增加如下配置:
location /nginx_status {
stub_status on;
access_log off;
allow 10.0.0.0/24; #<==设置允许和禁止的IP段访问
deny all; #<==设置允许和禁止的IP段访问
}
通常,打开http://status.etintian.org时会有上面所示的状态信息显示,可以通过压力测试工具获取这些数值,结果说明如下。
Active connections:2872
#<==表示Nginx正处理的活动连接数有2872个。
server accepts handled requests
2943121129431211110298687
Reading:80 Writing:35 Waiting:2757
其中,第一个server表示Nginx启动到现在共处理了29431211个连接;
第二个accepts表示Nginx启动到现在共成功创建了29431211次握手;
请求丢失数=(握手数-连接数),可以看出,本次状态显示没有丢失请求。
第三个handled requests,表示总共处理了110298687次请求;
Reading为Nginx读取到客户端的Header信息数。
Writing为Nginx返回给客户端的Header信息数。
Waiting为Nginx已经处理完正在等候下一次请求指令的驻留连接。在开启keep-alive的情况下,这个值等于active-(reading+writing)
特别提示:为了安全起见,这个状态信息要防止外部用户查看。
Nginx软件会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里。
配置记录Nginx的错误信息是调试Nginx服务的重要手段,
属于核心功能模块(ngx_core_module)的参数,该参数名字为error_log,
可以放在Main区块中全局配置,也可以放置不同的虚拟主机中单独记录。
error_log的语法格式及参数语法说明如下:
error_log file level;
关键字 日志文件 错误日志级别
其中,关键字error_log不能改变,日志文件可以指定任意存放日志的目录,
错误日志级别常见的有[debug|info|notice|warn|error|crit|alert|emerg],级别越高,
记录的信息越少,生产场景一般是warn|error|crit这三个级别之一,
注意不要配置info等较低级别,会带来巨大磁盘I/O消耗。
error_log的默认值为:
#default: error_log logs/error.log error;
[root@web01 extra]# vim /application/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
error_log logs/error.log error;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/*.conf;
# include extra/www.conf;
# include extra/bbs.conf;
# include extra/blog.conf;
# include extra/status.conf;
}
[root@web01 extra]# ../../sbin/nginx -t
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# ll ../../logs/
总用量 32
-rw-r--r-- 1 root root 12504 2月 13 11:05 access.log
-rw-r--r-- 1 root root 11542 2月 13 11:09 error.log
-rw-r--r-- 1 root root 6 2月 12 21:11 nginx.pid
Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责。对应的官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html。
Nginx的访问呢日志主要有以下两个参数控制。
参数 说明
log_format 用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)
access_log 用来指定日志文件的路径及使用的何种日志格式记录日志。
Nginx记录日志的默认参数配置如下:
access_log logs/access.log main;
定义语法 log_format name string ...;
其配置位置在http标签内。
日志格式说明如下:
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
其中,log_format为日志格式关键参数,不能变。
main是为日志格式指定的标签,记录日志时通过这个main标签选择指定的格式。其后所接的所有内容都是可以记录的日志信息,具体见表5-5。注意,所有的日志段以空格分隔,一行可以记录多个,不同列的意义也在表5-5中进行了说明。
表5-5 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:记录客户端访问信息,例如:浏览器,手机客户端等。
在没有特殊要求的情况下,采用默认的配置即可,更多可以设置的记录日志信息的变量见: http://nginx.org/en/docs/http/ngx_http_log_module.html
[root@web01 extra]# vim ../nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
error_log logs/error.log error;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
include extra/*.conf;
# include extra/www.conf;
# include extra/bbs.conf;
# include extra/blog.conf;
# include extra/status.conf;
}
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name etiantian.org www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/www_access.log main;
}
access_log logs/www_access.log 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"‘;
[root@web01 extra]# ll ../../logs/ #这里我们可以看到还没有www的日志文件
总用量 32
-rw-r--r-- 1 root root 12504 2月 13 11:05 access.log
-rw-r--r-- 1 root root 11542 2月 13 11:09 error.log
-rw-r--r-- 1 root root 6 2月 12 21:11 nginx.pid
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload #重启之后就能看到www定义的日志文件了
[root@web01 extra]# ll ../../logs/
总用量 32
-rw-r--r-- 1 root root 12504 2月 13 11:05 access.log
-rw-r--r-- 1 root root 11603 2月 13 12:58 error.log
-rw-r--r-- 1 root root 6 2月 12 21:11 nginx.pid
-rw-r--r-- 1 root root 0 2月 13 12:58 www_access.log
[root@web01 extra]# cat ../../logs/www_access.log #还没有内容
[root@salt-master ~]# wget www.etiantian.org
--2017-02-13 13:01:52-- http://www.etiantian.org/
正在解析主机 www.etiantian.org... 10.0.0.8
正在连接 www.etiantian.org|10.0.0.8|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:4 [text/html]
正在保存至: “index.html.1”
100%[====================================================================>] 4 --.-K/s in 0s
2017-02-13 13:01:52 (410 KB/s) - 已保存 “index.html.1” [4/4])
现在再次切换到服务器端
[root@web01 extra]# cat ../../logs/www_access.log
10.0.0.61 - - [13/Feb/2017:13:01:52 +0800] "GET / HTTP/1.0" 200 4 "-" "Wget/1.12 (linux-gnu)" "-"
与我们在上面配置文件里的main参数进行对应
‘$remote_addr(10.0.0.61) -(-) $remote_user(-)
[$time_local]([13/Feb/2017:13:01:52 +0800])
"$request"("GET / HTTP/1.0") ‘
‘$status(200) $body_bytes_sent(4) "$http_referer"(-) ‘
‘"$http_user_agent"("Wget/1.12 (linux-gnu)" )
"$http_x_forwarded_for"(-)‘;
·$remote_addr对应的是真实日志里的10.0.0.61,即客户端的IP。
·$remote_user对应的是第二个中杠“-”,没有远程用户,所以用“-”填充。
·[$time_local]对应的是[13/Feb/2017:13:01:52 +0800]。
·"$request"对应的是"GET/HTTP/1.1"。
·$status对应的是200状态码,200表示正常访问。
·$body_bytes_sent对应的是4字节,即响应body的大小。
·"$http_referer"对应的是"-",由于是直接打开域名浏览的,因此,referer没有值。
·"$http_user_agent"对应的是"Wget/1.12 (linux-gnu)"。
·"$http_x_forwarded_for"对应的是"-",因为Web服务没有使用代理,因此此处为"-"。
可以在记录日志参数中加上buffer和flush选项,这样可在高并发场景下提升网站访问性能。加该选项的命令如下:
access_log path [format [buffer=size [flush=time]] [if=condition]];
access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];
access_log syslog:server=address[,parameter=value] [format [if=condition]];
access_log off;
具体配置如下: [root@www conf]# cat extra/www.conf
#www virtualhost by oldboy
server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
#access_log logs/access_www.log main;
access_log logs/access_www.log main gzip buffer=32k flush=5s;
#access_log off;
}
更多内容参考:http://nginx.org/en/docs/http/ngx_http_log_module.html。
默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于日志的分析和处理,因此,有必要对Nginx日志、按天或按小时进行切割,使其分成不同的文件保存。这里使用按天切割的方法。 具体切割脚本如下:
[root@web01 extra]# mkdir -p /server/scripts/
[root@web01 extra]# cd /server/scripts/
[root@web01 scripts]# vim cut_nginx_log.sh
cd /application/nginx/logs
/bin/mv www_access.log www_access_$(date +%F -d ‘-1day‘).log
/application/nginx/sbin/nginx -s reload
## rsync to backup server
## del date before 7 day
注意:脚本实现切割Nginx日志的思想为将正在写入的Nginx日志(access_www.log)改名为带日期的格式文件(www_access_2017-02-12.log),然后平滑重新加载Nginx,生成新的Nginx日志(access_www.log)。
下面通过定时任务实现每天00点整定时执行/server/script/cut_nginx_log.sh切割日志。
[root@web01 scripts]# /bin/sh /server/scripts/cut_nginx_log.sh
[root@web01 scripts]# ls /application/nginx/logs/
access.log error.log nginx.pid www_access_2017-02-13.log www_access.log
[root@web01 scripts]# date -s "20170214"
2017年 02月 14日 星期二 00:00:00 CST
[root@web01 scripts]# /bin/sh /server/scripts/cut_nginx_log.sh
[root@web01 scripts]# ls /application/nginx/logs/
access.log error.log nginx.pid www_access_2017-02-13.log www_access_2017-02-14.log www_access.log
[root@web01 scripts]# crontab -l
####
00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
Nginx常用的日志收集及分析工具有rsyslog、awstats、flume、ELK(Elasticsearch logstash Kibana)、storm等。如果读者有需求,可以自行研究或参考老男孩的其他课程、书籍或网上资料。更多常用的开源运维工具见http://oldboy.blog.51cto.com/2561410/775056。
location指令的作用是根据用户请求的URI来执行不同的应用。URI的知识前面章节已经讲解过,其实就是根据用户请求的网站地址URL进行匹配,匹配成功即进行相关的操作。
下面是官方提供的常见的location匹配语法。
ocation语法
location使用的语法为:
location [ = | ~ | ~* | ^~ ] uri {
...
}
表5-6是对location语法的说明。上述语法中的URI部分是关键,这个URI可以是普通的字符串地址路径,或者是正则表达式,匹配成功则执行后面大括号里的相关指令。正则表达式的前面还可以有“”或“*”等特殊的字符。
表5-6 对location语法列表说明
location [ = | ~ | ~* | ^~ |@] uri {...}
指令 匹配标识 匹配的网站网址 匹配URI后要执行的配置段
匹配这两种特殊字符“”或“”的区别为:“”用于区分大小写(大小写敏感)的匹配;“”用于不区分大小写的匹配。还可以用逻辑操作符“!”对上面的匹配取反,即“!”和“!*”。此外,“^~”的作用是在进行常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不做正则表达式的检查。
下面是一组典型的location匹配,是官方的例子。
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
在上述location配置中,当用户请求“/”时,将匹配configuration A,当用户请求“/index.html”时,将匹配configuration B;当用户请求“/documents/document.html”时,将匹配configuration C;当用户请求“/images/1.gif”时,将匹配configuration D;当用户请求“/documents/1.jpg”时,将匹配configuration E。
表5-7描述了不同URI对应的配置情况。
用户请求的URI 完整的URL地址 匹配的配置
/:http://www.etiantian.org/ confinguration A
/index.html:http://www.etiantian.org/ configuration B
/docyments/document.html:http://www.etiantian.org/document.html configuration C
/images/1.gif:http://www.etiantian.org/images/1.gif configuration D
/documents/1.jpg:http://www.etiantian.com/documents/1.jpg configuration E
表5-7 不同URI对应的配置
下面是官方给出的location示例,我们通过该示例来验证不同的location标签生效的顺序。Nginx的配置文件如下:
[root@web01 scripts]# cd /application/nginx/conf/extra/
[root@web01 extra]# cp www.conf www.conf.oldboy.20160517
[root@www extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
#matches any query beginning with /images/ and halts searching,
#so regular expressions will not be checked.
#匹配任何以/images/开头的查询并且停止搜索。任何正则表达式匹配将不会被检查。
#"^~" 这个前缀的作用是在常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不会做正则表达式的检查
}
location ~* \.(gif|jpg|jpeg)$ {
#matches any request ending in gif,jpg,or jpeg. However,all
#requests to the /images/ directory will be handled.
#匹配任何以gif、jpg或jpeg结尾的请求
return 500;
}
access_log logs/access_www.log main gzip buffer=32k flush=5s;
}
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name etiantian.org www.etiantian.org;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
access_log logs/www_access.log main;
}
注意后面的;这个符号要英文的。不然检查会出错。
检查语法并使修改的配置生效,命令如下:
[root@web01 extra]# ../../sbin/nginx -t
[root@web01 extra]# ../../sbin/nginx -s reload
然后以Linux客户端为例对上述location匹配进行真实测试,配置hosts文件如下:
[root@web01 extra]# tail -1 /etc/hosts
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
[root@web01 extra]# ping www.etiantian.org # 测试是否通了
PING www.etiantian.org (42.62.5.158) 56(84) bytes of data.
64 bytes from 42.62.5.158: icmp_seq=1 ttl=128 time=49.4 ms
64 bytes from 42.62.5.158: icmp_seq=2 ttl=128 time=52.3 ms
实验结果如下:
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
402
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
402
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html
401
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/document.html
403
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/1.gif
404
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/1.jpg
500
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/oldboy/
401
[root@web01 extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/abc/
401
表5-8为用户请求的URI相关说明
用户请求额URI相关说明 设置的状态码 说明
当为空或/的时候:返回402,及匹配了:location = / {return 402;}:是精确匹配优先级最高,无论设置的顺序如何都将优先匹配并执行。
/index.html或任意不匹配其他location的字符串:返回401,即匹配了location / {return 401;}:/为默认匹配,即如果没有匹配上其他的location,则最后匹配“默认匹配”的部分。
/documents/document.html;返回了403,即匹配了location/documents/{return 403;}:此部分为路径匹配,即匹配了路径/documents/,注意后面的/1.jpg没有此处的location,而是匹配了结尾的.jpg。
/images/1.gif:返回了404,即匹配了location ^~ /images/ {return 404;}:此部分为路径匹配,但是前面增加了特殊字符^~,所以优先匹配路径,而没有匹配结尾的1.gif
/documents/1.jpg:返回了500,及匹配了location ~* .(gif|jpg|jpeg)$ {return 500;}:此部分匹配了1.jpg,属于扩展名匹配,虽然有/docunents/但是还是匹配了扩展名。
从以上的多个location 的配置可以看出匹配的优先顺序,具体见下表
不用URI及特殊字符组合匹配顺序 匹配说明
第一名:location = /:精确匹配/
第二名:location ^~ /images/:匹配常规字符串,不做正则匹配检查。
第三名:location ~*.(gif|jpg|jpeg)$:正则匹配
第四名:location /documents/:匹配常规字符串,如果有正则则优先匹配正则。
第五名:location /:所有location都不能pi‘pei匹配后的默认匹配。
和Apache等Web服务软件一样,Nginx rewrite的主要功能也是实现URL地址重写。Nginx的rewrite规则需要PCRE软件的支持,即通过Perl兼容正则表达式语法进行规则匹配。前文在安装Nginx软件时就已经安装了这个PCRE软件,同时也让Nginx支持了rewrite的功能,默认参数编译时,Nginx就会安装支持rewrite的模块,但是,也必须要有PCRE软件的支持。
伪静态的功能就是使用rewite实现的。
指令语法:rewrite regex replacement[flag];
默认值:none
应用位置:server、location、if
rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分的内容,重定向到replacement部分,结尾是flag标记。下面是一个简单的URL rewrite跳转的例子:
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
[root@web01 conf]# cd extra/
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name etiantian.org www.etiantian.org;
location / {
rewrite ^/(.*) http://blog.oldboyedu.com/$1 permanent;
}
access_log logs/www_access.log main;
}
[root@web01 extra]# ../../sbin/nginx -t
[root@web01 extra]# ../../sbin/nginx -s reload
[root@salt-master ~]# curl www.etiantian.org/aaromail -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Mon, 13 Feb 2017 09:19:21 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://blog.oldboyedu.com/aaromail
# rewrite ^/(.*) http://blog.oldboyedu.com/$1 permanent;
regex部分是^/(.*),这是一个正则表达式,表示匹配所有,匹配成功后跳转到www.etiantian.org/aaromail/$1。这里的$1是取前面regex部分括号里的内容也就是这里的aaromail,结尾的permanent;是永久301重定向标记,即跳转到后面的http://blog.oldboyedu.com/$1地址上。
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name etiantian.org www.etiantian.org;
location / {
root html/www;
index index.html;
}
location ^~ /images/ {
rewrite ^/(.*) http://blog.oldboyedu.com/$1 permanent;
}
access_log logs/www_access.log main;
}
[root@web01 extra]# ../../sbin/nginx -t
[root@web01 extra]# ../../sbin/nginx -s reload
客户端测试 [root@salt-master ~]# curl www.etiantian.org
www
浏览器输入 http://www.etiantian.org/pelisi
404 Not Found
nginx/1.6.3
[root@salt-master ~]# curl www.etiantian.org/images/pelosi -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Mon, 13 Feb 2017 10:10:59 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://blog.oldboyedu.com/images/pelosi
当含有/images的时候就会跳转了。
在上述指令中,rewrite为固定关键字,表示开启一条rewrite匹配规则,regex部分是^/(.*),这是一个正则表达式,表示匹配所有,匹配成功后跳转到http://www.etiantian.org/$1。这里的$1是取前面regex部分括号里的内容,结尾的permanent;是永久301重定向标记,即跳转到后面的http://www.etiantian.org/$1地址上。
表5-10为regex常用正则表达式字符的一部分。 http://seanlook.com/2015/05/17/nginx-location-rewrite/
rewrite指令的最后一项参数为flag标记,该标记说明见表5-11。
表5-10 regex常用正则表达式字符 http://www.netpc.com.cn/1474.html
flag标记
last 相当于Apache里的[L]标记,表示完成rewrite,本条规则匹配完成后,继续向下匹配新的location URI规则;
break 终止匹配, 不再匹配后面的规则。
redirect 返回302临时重定向 地址栏会显示跳转后的地址。
permanent 返回301永久重定向 地址栏会显示跳转后的地址。
在以上的flag标记中,last和break用来实现URL重写,浏览器地址栏的URL地址不变,但在服务器端访问的程序及路径发生了变化。redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL地址。
last和break标记的实现功能类似,但二者之间有细微的差别,使用alias指令时必须用last标记,使用proxy_pass指令时要使用break标记。last标记在本条rewrite规则执行完毕后,会对其所在的server{......}标签重新发起请求,而break标记则会在本条规则匹配完成后,终止匹配,不再匹配后面的规则。
更多知识请参考后面关于Nginx反向代理负载均衡的章节。
Nginx rewrite的企业应用场景 Nginx的rewrite功能在企业里应用非常广泛: ·可以调整用户浏览的URL,使其看起来更规范,合乎开发及产品人员的需求。 ·为了让搜索引擎收录网站内容,并让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。 ·网站换新域名后,让旧域名的访问跳转到新的域名上,例如:让京东的360buy换成了jd.com。 ·根据特殊变量、目录、客户端的信息进行URL跳转等。
[root@www extra]# cat www.conf
#www virtualhost by oldboy
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*)http://www.etiantian.org/$1 permanent;
#<==当用户访问etiantian.org及下面的任意内容时,都会通过这条rewrite跳转到
www.etiantian.org对应的地址
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main gzip buffer=32k flush=5s;
}
有时,在实际工作企业要求我们为网站设置访问账号和密码权限,这样操作后,只有拥有账号密码的用户才可以访问网站内容
这种使用账号密码才可以访问网站的功能主要应用在企业内部人员访问的地址上,例如:企业网站后台、MySQL客户端phpmyadmin、企业内部的CRM、WIKI网站平台等。下面介绍一个配置示例:
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
其中,有两个参数需要说明。
·auth_basic
语法:auth_basic string|off;
默认值:auth_basic off;
使用位置:http、server、location、limit_except
·auth_basic_user_file
语法:auth_basic_user_file file;
默认值:—
使用位置:http、server、location、limit_except
auth_basic_user_file参数后接认证密码文件,file的内容如下:
# comment
name1:password1
name2:password2:comment
name3:password3
可以使用Apache自带的“htpasswd”或“openssl passwd”命令设置用户和密码到认证文件里,注意,密码是加密的。相关内容见官网:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
下面进行配置实战。选择一个虚拟主机配置在server标签里,配置内容如下: auth_basic "oldboy training"; auth_basic_user_file /application/nginx/conf/htpasswd;
注意:/application/nginx/conf/是认证文件路径,htpasswd是存放账号及密码的文件。
[root@web01 extra]# vim www.conf
server {
listen 80;
server_name etiantian.org www.etiantian.org;
location / {
auth_basic "oldboy training";
auth_basic_user_file /application/nginx/conf/htpasswd;
root html/www;
index index.html index.htm;
}
access_log logs/www_access.log main;
}
说明:
·auth_basic "oldboy training";用于设置认证提示字符串"oldboy training"。
·auth_basic_user_file/application/nginx/conf/htpasswd;用于设置认证的密码文件,即用户输入账户密码后,Nginx会到这个文件中对比用户的输入是否正确,进而决定是否允许用户访问网站。
/usr/bin/which: no htpasswd in(/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@www extra]# yum install httpd -y
[root@www extra]# which htpasswd
/usr/bin/htpasswd
操作命令如下:
htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456
chmod 400 /application/nginx/conf/htpasswd
chown nginx /application/nginx/conf/htpasswd
操作过程如下: [root@www extra]# htpasswd -bc /application/nginx/conf/htpasswd oldboy 123456 Adding password for user oldboy
[root@www extra]# chmod 400 /application/nginx/conf/htpasswd
[root@www extra]# chown nginx /application/nginx/conf/htpasswd
[root@www extra]# cat /application/nginx/conf/htpasswd oldboy:HWSXCFixaoiZ6 #<==看到了吧,密码是加密的。
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s relaod
http://study.oldboyedu.com/classModule/video/187795/190705/665115/0/0
http://oldboy.blog.51cto.com/2561410/581383
1)Nginx的特性优点。
2)主流Web动态静态性能对比。
3)Apache select和Nginx epoll模型的区别(面试常考)。
4)虚拟主机概念及类型分类详解。
5)基于域名和端口虚拟主机的介绍及搭建。
6)Nginx错误、访问日志,以及访问日志切割。
7)Nginx访问状态信息介绍及配置实践。
8)Nginx location介绍及配置实践。
9)Nginx rewrite介绍及配置实践。
10)Nginx Web访问认证介绍及配置实践。
原文:http://www.cnblogs.com/aofo/p/6413431.html