前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >12.10 Nginx访问日志

12.10 Nginx访问日志

作者头像
运维小白
发布2018-02-06 15:55:44
7790
发布2018-02-06 15:55:44
举报
文章被收录于专栏:运维小白运维小白

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访问日志

  • 日志的文件也是在主配置文件中
  1. 打开主配置文件vim /usr/local/nginx/conf/nginx.conf
代码语言:javascript
复制
[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地址
  1. 除了在主配置文件nginx.conf里定义日志格式外,还需要在虚拟主机配置文件去定义access_log /tmp/1.log combined_realip; 来定义访问日志路径
代码语言:javascript
复制
[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;
}
保存退出
  • 如果不写日志格式,那就会走默认的日志格式
  1. 然后检查配置文件是否存在语法错误,并重新加载配置文件
代码语言:javascript
复制
[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]# 
  1. 测试
代码语言:javascript
复制
[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]# 
  1. 查看日志cat /tmp/test.com.log
代码语言:javascript
复制
[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]# 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Nginx访问日志目录概要
  • Nginx访问日志
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门开源软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档