4.8 nginx的日志功能
nginx的两种日志种类
错误日志:记录nginx运行错误情况信息
访问日志:记录用户访问日志信息
官方说明
http://nginx.org/en/docs/ngx_core_module.html#error_log
4.8.1 定义错误日志信息
系统默认配置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
配置错误日志,修改主配置文件
[root@web01 logs]# vim ../conf/nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
}
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
重启服务
[root@web01 logs]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
[root@web01 logs]# /application/nginx/sbin/nginx -s reload
查看错误日志信息
[root@web01 logs]# tail error.log
2017/10/25 11:41:55 [error] 40842#0: *7 open() "/application/nginx-1.10.2/html/www/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: www.etiantian.org, request: "GET /favicon.ico HTTP/1.1", host: "www.etiantian.org", referrer: "http://www.etiantian.org/"
4.8.2 访问日志配置
系统默认配置
#log_format main 'remoteaddr−'> remoteaddr− remote_user [timelocal]"'> timelocal]" request" '
# 'status'> status body_bytes_sent "$http_referer" '
# '"httpuseragent""'> httpuseragent"" http_x_forwarded_for"';
#access_log logs/access.log main;
修改访问日志配置--修改主配置文件
[root@web01 www]# cat ../../conf/nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main 'remoteaddr−'> remoteaddr− remote_user [timelocal]"'> timelocal]" request" '
'status'> status body_bytes_sent "$http_referer" '
'"httpuseragent""'> httpuseragent"" http_x_forwarded_for"';
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
}
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
修改访问日志配置--修改虚拟主机配置文件
[root@web01 www]# cat /application/nginx/conf/extra/www.conf
server {
listen 80;
server_name www.etiantian.org ett.org;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
4.8.3 【重要】访问日志信息说明
日志内容
10.0.0.1 - - [22/Oct/2017:16:04:54 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"
配置文件
'remoteaddr−'> remoteaddr− remote_user [timelocal]"'> timelocal]" request" '
'status'> status body_bytes_sent "$http_referer" '
'"httpuseragent""'> httpuseragent"" http_x_forwarded_for"';
日志信息说明
|
参数 |
日志内容 |
含义 |
|
$remote_addr |
10.0.0.1 |
客户端ip地址 |
|
- |
- |
|
|
$remote_user |
- |
显示远程访问者用户信息 |
|
[$time_local] |
[22/Oct/2017:16:04:54 +0800] |
显示访问时间 |
|
$request |
GET / HTTP/1.1" |
请求行信息 |
|
$status |
304 |
状态码 |
|
$body_bytes_sent |
0 |
响应报文主体内容大小 |
|
$http_referer |
- |
|
|
$http_user_agent |
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 |
客户端浏览网页工具信息 |
|
$http_x_forwarded_for |
- |
反向代理转发 |
详细日志详细说明
|
Nginx日志变量 |
说明 |
|
$remote_addr |
记录访问网站的客户端地址;即源IP地址 |
|
$http_x_forwarded_for |
当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的 前提是代理服务器上也进行了相关的x_forwarded_for设置 可以记录用户真实的IP地址信息 |
|
$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_httpJog_module.html1
4.8.4 日志的切割
切割日志方式
01:利用脚本实现日志切割
简单脚本
[root@web01 logs]# cat /server/scripts/log_cut.sh
#!/bin/bash
mv /application/nginx/logs/access_www.log /application/nginx/logs/access_www_`date +%F`.log
/application/nginx/sbin/nginx -s reload
包含判断的脚本
[root@web01 logs]# cat /server/script/cut_nginx_log.sh
# ! /bin/sh
Dateformat='date +%Y%m%d'
Basedir= "/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[-d Nginxlogdir ] && cd'>Nginxlogdir ] && cdNginxlogdir ] && cd Nginxlogdir || exit 1
[-f ${Logname}.log ] || exit 1
/bin/mv Logname.log'> Logname.log {Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
02.利用系统自带切割软件进行切割
cat /etc/logrotate.conf
4.9 企业需求解决(location应用)
1、搭建好一台nginx的web服务器,配置好内网网卡地址与外网网卡地址
2、web服务器的网站域名为www.etiantian.org 站点目录为 html/www
3、要求内网可以访问 www.etiantian.org/AV 资源
4、要求外网不可以访问 www.etiantian.org/AV 资源
4.9.1 需求处理 --location的应用
1、定位需要控制的资源
location == if
修改配置文件
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
location /AV {
root html/www;
index index.html index.htm;
allow 172.16.1.0/24;
deny all;
}
}
4.9.2 location 语法
location 指令的作用是根据用户请求的URI来执行不同的应用。
locationn使用的语法为
location [=|~|~*|^~] uri {
....
}
location 语法说明表
|
location |
[=|~|~*|^~] |
uri |
{....} |
|
指令 |
匹配标识 |
匹配的网站地址 |
匹配URI后要执行的配置段 |
~ 与~* 的区别
u ~ 匹配内容区分大小写
u ~* 匹配内容不区分的小写
u !~ 取反
u ^~ 但多个匹配同时存在,优先匹配 ^~匹配的内容;不做正则表达式的检查 (优先处理)
4.9.3 官方配置示例
location = / {
[ configuration A ]
}
location / {
[ configuration B ]
}
location /documents/ {
[ configuration C ]
}
location ^~ /images/ {
[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}
说明:
"/"请求将匹配配置A,
"/index.html"请求将匹配配置B,
"/documents/document.html"请求将匹配配置C,
"/images/1.gif"请求将匹配配置D,
"/documents/1.jpg"请求将匹配配置E.
不同uri及特殊字符组合匹配的顺序说明
|
顺序 |
不用URI及特殊字符组合匹配 |
匹配说明 |
|
1 |
location = / {} |
精确匹配 / |
|
2 |
location ^~ /image/{ |
匹配常规字符串,不做正则表达式匹配检查 |
|
3 |
location ~* \.(gif|jpg|jpeg)$ { |
正则匹配 |
|
4 |
location /documents/ { |
匹配常规字符串,如果有正则,则优先匹配正则 |
|
5 |
location / { |
所有location 都不能匹配后的默认匹配 |
4.9.4 【测试】测试location的访问
4.9.4.1 修改配置文件
定义不同的location返回不同的数值
[root@web01 extra]# vim 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;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
access_log logs/access_www.log main;
}
4.9.4.2 访问测试
根据请求不同uri的返回值验证 location的配置。
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents
401
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org
402
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents/ss.jpg
500
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents
401
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/documents/
403
[root@nfs01 ~]# curl -I -w "%{http_code}\n" -o /dev/null -s www.etiantian.org/images/1.jpg
404
4.10 rewrite 模块的使用--地址重写
4.10.1 rewrite 重写模块
将地址信息进行重写
rewrite 语法格式
rewrite regex replacement [flag]
rewrite应用标签:server、location、if
4.10.2 rewrite模块两个功能
1. 实现网站地址信息跳转
2. 实现伪静态
4.10.3 方法一 使用if判断
[root@web01 extra]# cat www.conf
server {
listen 80;
server_name www.etiantian.org ett.org;
if (host ∗"etiantian.org'> host ∗"etiantian.org ") {
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
测试
[root@web01 www]# curl etiantian.org -L
web01 www
4.10.4 方法二 再添加上一个区块
server {
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
4.10.5 nginx 的rwite重写企业应用场景
可以调整用户浏览的URL,使其看起来更规范,合乎开发及产品人员的需求。
为了让搜索引擎收录网站内容,并让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务。
网站换新域名后,让旧域名的访问跳转到新的域名上,例如:让京东的360buy换成了jd.com。
根据特殊变量、目录、客户端的信息进行URL跳转等。
说明:开源软件类似wordpress的,官方都会对伪静态配置进行说明
4.11 nginx 的访问认证
4.11.1 修改nginx的相关配置文件
vim extra/www.conf
location / {
root html/www;
index index.html index.htm;
auth_basic "oldboy training";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
4.11.2 创建密码文件
注: 这里使用的htpasswd命令默认是没有的,需要通过yum install httpd-tools -y 安装
[root@web01 extra]# htpasswd -c /application/nginx/conf/htpasswd oldboy
New password:
Re-type new password:
Adding password for user oldboy
参数说明:
-c Create a new file.
创建一个新的密码文件
-b Use the password from the command line rather than prompting for it.
采用免交互的方式输入用户的密码信息
htpasswd参数说明
|
参数 |
参数说明 |
|
-c |
创建一个新文件。 |
|
-n |
不更新文件; 显示结果。 |
|
-m |
强制MD5密码加密。 |
|
-d |
强制CRYPT加密密码(默认)。 |
|
-p |
不加密密码(明文)。 |
|
-s |
强制SHA加密密码。 |
|
-b |
使用命令行中的密码,而不是提示。(免交互) |
|
-D |
删除指定的用户。 |
4.11.3 更改密码文件权限
[root@web01 extra]# chmod 400 /application/nginx/conf/htpasswd
[root@web01 extra]# chown -R www.www /application/nginx/conf/htpasswd
[root@web01 extra]# cat /application/nginx/conf/htpasswd
oldboy:e3OfMiJThE0Qg
4.11.4 重启服务: 配置修改后要重启服务
[root@web01 extra]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
[root@web01 extra]# /application/nginx/sbin/nginx -s reload
4.11.5 访问测试
使用交互式输入密码
[root@web01 www]# curl www.etiantian.org -uoldboy
Enter host password for user 'oldboy':123456
web01 www
使用免交互输入密码
[root@web01 www]# curl www.etiantian.org -uoldboy:123456
web01 www
401错误说明: 需要认证,但是没有认证
下面关于Nginx的文章您也可能喜欢,不妨参考下:
Nginx 403 forbidden的解决办法 http://www.linuxidc.com/Linux/2017-08/146084.htm
CentOS 7下Nginx服务器的安装配置 http://www.linuxidc.com/Linux/2017-04/142986.htm
CentOS上安装Nginx服务器实现虚拟主机和域名重定向 http://www.linuxidc.com/Linux/2017-04/142642.htm
CentOS 6.8 安装LNMP环境(Linux+Nginx+MySQL+PHP) http://www.linuxidc.com/Linux/2017-04/142880.htm
Linux下安装PHP环境并配置Nginx支持php-fpm模块 http://www.linuxidc.com/Linux/2017-05/144333.htm
Nginx服务的SSL认证和htpasswd认证 http://www.linuxidc.com/Linux/2017-04/142478.htm
Ubuntu 16.04上启用加密安全的Nginx Web服务器 http://www.linuxidc.com/Linux/2017-07/145522.htm
Linux中安装配置Nginx及参数详解 http://www.linuxidc.com/Linux/2017-05/143853.htm
Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
CentOS 7.2下Nginx+PHP+MySQL+Memcache缓存服务器安装配置 http://www.linuxidc.com/Linux/2017-03/142168.htm
CentOS6.9编译安装Nginx1.4.7 http://www.linuxidc.com/Linux/2017-06/144473.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-10/148128.htm
