Nginx软件将错误的故障信息及用户的访问的日志信息记录到指定的日志文件
1. Nginx错误日志信息介绍 核心功能模块:ngx_core_module的参数名:error_log, 配置位置:Main区域中 全局配置 或者 虚拟主机单独配置 放置的标签段:main、http、server、location 语法格式:error.log(关键字) file(日志文件) level(错误日志级别) 日志级别:[debug|info|notice|warn|error|crit|alert|emerg] 级别一次增高,级别越高记录的信息越少 注意:不要配置info等较低的级别,会带来巨大的磁盘I/O消耗 eg:#default:error.log logs/error.log error2.Nginx错误日志配置worker_processes 1;error_log logs/error.log; #<==增加这一行即可events { worker_connections 1024;}3.访问日志的参数log_format语法:log_format name string ...;格式:log_format main 'remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_uer_agent" "$http_x_forwarded_for" ';位置:http标签内access_log语法:access_log path [format [buffer=size [flush = time]] [if=condition]] access_log path format gzip [=size] [buffer = size] [flush = time] [if = condition]; access_log syslog:server=address[,parameter=value] [format [if=condition]];access_log off 表示不记录访问日志4.访问日志配置[root@instance-yf0xzby9 conf]# sed -n '21,23 s/#//gp' nginx.conf.default log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';将上述配置放入nginx.conf 中[root@instance-yf0xzby9 conf]# vi nginx.conf[root@instance-yf0xzby9 conf]# cat -n nginx.conf 1 worker_processes 1; 2 error_log logs/error.log; 3 events { 4 worker_connections 1024; 5 } 6 http { 7 include mime.types; 8 default_type application/octet-stream; 9 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 10 '$status $body_bytes_sent "$http_referer" ' 11 '"$http_user_agent" "$http_x_forwarded_for"'; 12 sendfile on; 13 keepalive_timeout 65; 14 include extra/www.conf; 15 include extra/bbs.conf; 16 include extra/blog.conf; 17 include extra/status.conf; 18 19 } 然后在每个虚拟主机进行配置,并检查[root@instance-yf0xzby9 conf]# vi -n extra/www.conf[root@instance-yf0xzby9 conf]# cat extra/www.conf server { listen 80; server_name www.etiantian.org etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } access_log logs/access_www.log main; }[root@instance-yf0xzby9 conf]# ../sbin/nginx -tnginx: the configuration file /application/nginx/conf/nginx.conf syntax is oknginx: configuration file /application/nginx/conf/nginx.conf test is successful[root@instance-yf0xzby9 conf]# ../sbin/nginx -s reload[root@instance-yf0xzby9 conf]# curl www.etiantian.orghttp://www.etiantian.org[root@instance-yf0xzby9 conf]# ls -l ../logs/access_www.log-rw-r--r-- 1 root root 187 Sep 27 09:53 ../logs/access_www.log[root@instance-yf0xzby9 conf]# tail -l ../logs/access_www.log172.16.0.4 - - [27/Sep/2018:09:53:07 +0800] "GET / HTTP/1.1" 200 25 "-" "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" "-"日志深入加上buffer和flush选项,在高并发场景下提高网站的性能server { listen 80; server_name www.etiantian.org etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #access_log logs/access_www.log main; access_log logs/access_www.log main gzip buffer=32k flush=5s; #access_log off; }5.访问日志轮询切割按天切割脚本: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日志文件(access_www.log)改名为 带日期的格式文件(20180927_access.log) 然后重新加载Nginx,生成新的Nginx日志(access_www.log)6.Nginx location语法:location [ = | ~ | ~* | ^~ ] uri { ...}说明:指令 匹配标识 匹配的网站网址 匹配URI后要执行的配置段~:用于区分大小写~*:不区分大小写(还可以使用!来进行取反操作)^~:进行常规的字符串匹配检查后,不做正则表达式的检查[root@instance-yf0xzby9 extra]# cat www.conf server { listen 80; server_name www.etiantian.org etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images { return 404; } #location ~* \.(gif|jpg|jpeg)$ { # return 500; # } #access_log logs/access_www.log main; access_log logs/access_www.log main gzip buffer=32k flush=5s; #access_log off; }然后检查语法,并平滑启动测试结果: curl -l -o /dev/null -I -w "%{http_code}" http://www.etiantian.org % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 182 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0402其他测试依次类推7.Nginx rewrite功能:实现URL地址重写前提:需要pcre的支持,即通过perl兼容正则表达式语法进行规则匹配语法:rewrite regex replacement[flag]默认值:none位置:server、location、ifeg: rewrite ^/(.*) http://www.etiantian.org/$1 permanent;$1:表示去正则表达式前面的内容permanent:表示永久301定向标记应用:01.调整用户浏览的URL,看以来更规范02.为了让搜索引擎收录网站内容,提高用户体验,将动态URL地址伪装成静态地址03.更换新的域名后,让旧域名跳转到新的域名上04.根据特殊变量、目录、客户端的信息进行URL跳转等实战:301跳转:[root@instance-yf0xzby9 extra]# cat www.conf server{ listen 80; server_name etiantian.org; rewrite ^/(.*) http://www.etiantian.org/$1 permanent; } server { listen 80; server_name www.etiantian.org; #server_name www.etiantian.org etiantian.org; root html/www; error_page 500 502 503 504 /50x.html; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images { return 404; } #location ~* \.(gif|jpg|jpeg)$ { # return 500; # } #access_log logs/access_www.log main; access_log logs/access_www.log main gzip buffer=32k flush=5s; #access_log off; }则 可以实现在访问etiantian.org 时,自动跳转到www.etiantain.org 与设置虚拟主机别名起到同样的效果。实现不同域名的URL跳转:(外部跳转常用方法)8.Nginx访问认证更改www.conf配置文件server { listen 80; server_name www.etiantian.org etiantian.org; error_page 500 502 503 504 /50x.html; location / { root html/www; index index.html index.htm; auth_basic "hty training"; auth_basic_user_file /application/nginx/conf/htpasswd; } #access_log logs/access_www.log main; access_log logs/access_www.log main gzip buffer=32k flush=5s; #access_log off; }安装httpd后设置认证账号和密码htpasswd -bc /application/nginx/conf/htpasswd hty 123456chmod 400 /application/nginx/conf/htpasswd chown nginx /application/nginx/conf/htpasswd 浏览器访问时注意带接口访问,防止自动跳转到老男孩网站。9.访问Nginx时出现“403 forbidden”状态码原因: 01.配置文件没有默认首页参数,或者首页文件在站点目录下没有index、index.php、index.html、index.htm等 02.站点目录下没有配置文件里指定的首页文件 03.站点目录或内部的程序文件没有Nginx用户访问权限 04.Nginx配置文件中设置了allow、deny等权限控制