12.10 Nginx访问日志
时间:2022-04-27
本文章向大家介绍12.10 Nginx访问日志,主要内容包括Nginx访问日志目录概要、Nginx访问日志、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
Nginx访问日志目录概要
- 日志格式
- vim /usr/local/nginx/conf/nginx.conf //搜索log_format
$remote_addr |
客户端IP(公网IP) |
---|---|
$http_x_forwarded_for |
代理服务器的IP |
$time_local |
服务器本地时间 |
$host |
访问主机名(域名) |
$request_uri |
访问的url地址 |
$status |
状态码 |
$http_referer |
referer |
$http_user_agent |
user_agent |
- 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件中增加
- access_log /tmp/1.log combined_realip;
- 这里的combined_realip就是在nginx.conf中定义的日志格式名字 -t && -s reload
- curl -x127.0.0.1:80 test.com -I
- cat /tmp/1.log
Nginx访问日志
- 日志的文件也是在主配置文件中
- 打开主配置文件vim /usr/local/nginx/conf/nginx.conf
[root@hanfeng vhost]# vim /usr/local/nginx/conf/nginx.conf
搜索/log_format 找到以下内容,就是来定义日志格式的
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
- combined_realip 日志格式的名字,可以随便定义,这里定义成什么名字,后面就引用成什么名字,决定了虚拟主机引用日志的类型
- nginx配置文件,有一个特点,以 “ ; ” 分号结尾,配置文件一段如果没有 分号结尾,表示这一段还没有结束,就算中间执行了换行。
$remote_addr |
客户端IP(公网IP) |
---|---|
$http_x_forwarded_for |
代理服务器的IP |
$time_local |
服务器本地时间 |
$host |
访问主机名(域名) |
$request_uri |
访问的url地址 |
$status |
状态码 |
$http_referer |
referer(跳转页) |
$http_user_agent |
user_agent(标识) |
- 若想自己的公网IP,可以直接百度IP,就会出来自己上网的IP地址
- 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件去定义access_log /tmp/1.log combined_realip; 来定义访问日志路径
[root@hanfeng vhost]# vim test.com.conf
在第一个括号中添加access_log /tmp/1.log combined_realip;即可
server
{
listen 80;
server_name test.com test1.com test2.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
if ($host != 'test.com' ) {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
access_log /tmp/test.com.log combined_realip;
}
保存退出
- 如果不写日志格式,那就会走默认的日志格式
- 然后检查配置文件是否存在语法错误,并重新加载配置文件
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hanfeng vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng vhost]#
- 测试
[root@hanfeng vhost]# curl -x127.0.0.1:80 test1.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:18 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/
[root@hanfeng vhost]# curl -x127.0.0.1:80 test2.com -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Thu, 04 Jan 2018 14:15:25 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/
[root@hanfeng vhost]#
- 查看日志cat /tmp/test.com.log
[root@hanfeng vhost]# cat /tmp/test.com.log
127.0.0.1 - [04/Jan/2018:22:15:18 +0800] test1.com "/" 301 "-" "curl/7.29.0"
127.0.0.1 - [04/Jan/2018:22:15:25 +0800] test2.com "/" 301 "-" "curl/7.29.0"
[root@hanfeng vhost]#
- 传统数据库也能实现区块链存储
- golang(Go语言) byte/[]byte 与 二进制形式字符串 互转
- Sublime Text 最新注册码分享
- Lua table之弱引用
- 看吧,这就是现代化 PHP 该有的样子
- 从web图片裁剪出发:了解H5中的Blob
- Android子线程更新UI主线程方法之Handler
- Drawable.Bitmap.Canvas.Paint.Matrix
- 关于JSON.stringify和Unicode编码,需要注意的几点
- 用 PHP 的方式实现的各类算法合集
- Nginx 反向代理解决前后端联调跨域问题
- JavaScript对象length
- Go1.8.4和Go1.9.1版本发布
- Javascript数组操作
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用keras实现BiLSTM+CNN+CRF文字标记NER
- 图解MyBatis的SQL执行流程
- Keras: model实现固定部分layer,训练部分layer操作
- 在Keras中CNN联合LSTM进行分类实例
- DeepWalk:图网络与NLP的巧妙融合
- PHP扩展mcrypt实现的AES加密功能示例
- PHP PDOStatement::bindColumn讲解
- PHP PDOStatement::fetchAll讲解
- PHP PDOStatement::bindParam讲解
- PHP date()格式MySQL中插入datetime方法
- Python sklearn中的.fit与.predict的用法说明
- 基于python实现ROC曲线绘制广场解析
- Python建造者模式案例运行原理解析
- PHP PDOStatement::errorInfo讲解
- PDO::setAttribute讲解