log_format access '$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" ';
access_log logs/access.log access;
error_log logs/error.log error;1.2)日志相关参数含义:日志相关参数官网给的解释是:Module
ngx_http_log_module$remote_addr,
$http_x_forwarded_for 记录客户端 IP$remote_user
记录客户端用户名称$time_local
通用日志格式下的本地时间$request 记录请求的 URL 和 HTTP
Protocol$status
记录请求状态$body_bytes_sent 发送给客户端的
Bytes,不包括 Header 的大小;该变数与 Apache mod_log_config 的 "%B" 相容$bytes_sent 发送给客户端的
总Bytes数$connection
连接的序列号$connection_requests
当前通过一个连接获得的请求数量$msec
日志写入时间。单位为秒,精度是毫秒$pipe
如果请求是通过HTTP流水线(pipelined)发送,pipe值为"p",否则为"."$http_referer
记录从哪个页面链接访问过来的$http_user_agent
记录客户端浏览器相关信息$request_length
请求的长度(包括请求行,请求头和请求正文)$request_time
请求处理时间,单位为秒,精度毫秒; 从读入客户端的第一个位元组开始,直到把最后一个字元发送给客户端后进行日志写入为止$time_iso8601
ISO8601标准格式下的本地时间1.3)查看Nginx日志:[root@nginx logs]# cat access.log
192.168.1.131 - -[29/Jan/2016:04:40:31 +0800] "GET / HTTP/1.1" 200 612"-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.163.400 QQBrowser/9.3.7175.400" "-"
192.168.1.131 - -[29/Jan/2016:04:40:31 +0800] "GET /favicon.ico HTTP/1.1" 302 160"http://192.168.1.220/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.163.400 QQBrowser/9.3.7175.400" "-"
192.168.1.131 - -[29/Jan/2016:04:41:20 +0800] "GET / HTTP/1.1" 304 0"-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.163.400 QQBrowser/9.3.7175.400" "-"
192.168.1.131 - -[29/Jan/2016:04:41:47 +0800] "GET / HTTP/1.1" 304 0"-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36 Core/1.47.163.400 QQBrowser/9.3.7175.400" "-"
192.168.1.243 - -[29/Jan/2016:11:49:26 +0800] "GET /favicon.ico HTTP/1.1" 302 160"-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-"
192.168.1.243 - -[29/Jan/2016:11:49:28 +0800] "GET / HTTP/1.1" 200 612"-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-"
192.168.1.243 - -[29/Jan/2016:11:49:28 +0800] "GET /favicon.ico HTTP/1.1" 302 160"http://192.168.1.220/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" "-"
192.168.1.172 - -[29/Jan/2016:11:56:43 +0800] "GET / HTTP/1.0" 200 612"-" "-" "-"
[root@nginx logs]#1.4)根据需求过滤:1.4.1)根据访问IP统计UVawk '{print $1}' access.log|sort | uniq -c |wc -l1.4.2)统计访问URL统计PVawk '{print $7}' access.log|wc -l1.4.3)查询访问最频繁的URLawk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more1.4.4)查询访问最频繁的IPawk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
awk '{print $1}' access.log | sort | uniq -c | sort -n -k 1 -r | head -n 51.4.5)根据时间段统计查看日志cat access.log| sed -n "/2016:04:40/,/2016:11:00/"p access.log|awk '{print $1,$3,$4}'1.6)效果查看[root@nginx logs]# awk '{print $1}' access.log|sort | uniq -c |wc -l
3
[root@nginx logs]# awk '{print $7}' access.log|wc -l
8
[root@nginx logs]# awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
7 HTTP/1.1"
1 HTTP/1.0"
[root@nginx logs]# awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
4 192.168.1.131
3 192.168.1.243
1 192.168.1.172
[root@nginx logs]# cat access.log| sed -n "/2016:04:40/,/2016:11:00/"p access.log|awk '{print $1,$3,$4}'
192.168.1.131 -[29/Jan/2016:04:40:31 +0800]
192.168.1.131 -[29/Jan/2016:04:40:31 +0800]
192.168.1.131 -[29/Jan/2016:04:41:20 +0800]
192.168.1.131 -[29/Jan/2016:04:41:47 +0800]
192.168.1.243 -[29/Jan/2016:11:49:26 +0800]
192.168.1.243 -[29/Jan/2016:11:49:28 +0800]
192.168.1.243 -[29/Jan/2016:11:49:28 +0800]
192.168.1.172 -[29/Jan/2016:11:56:43 +0800]
[root@nginx logs]#1.7.1)压测统计,测试等性能评估1.7.2)根据访问过滤403,404等代码状态1.7.3)封ip1.8)功能评估:1.8.1)可用于临时需求,但是并不智能,需要会一些sed awk sed等工具知识1.8.2)没有图形化,看起来不方便方式二、用AWStats来分析Nginx日志#!/bin/bash
logs_path="/usr/local/nginx/logs/"
pid_path="/usr/local/logs/nginx.pid"
mv ${logs_path}access.log ${logs_path}access_`date +%Y%m%d`.log.log
kill -USR1 `cat ${pid_path}2.1.1)计划任务59 23 * * * /root/nginxcut.sh2.2)awstas7.0后新特性:(目前出到7.5)[root@nginx ~]# wget --no-check-certificate http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
[root@nginx ~]# tar xf awstats-7.0.tar.gz
[root@nginx ~]# mkdir /var/lib/awstats
[root@nginx logs]# mkdir /usr/local/awstats
[root@nginx ~]# mv awstats-7.0/* /usr/local/awstats/
[root@nginx ~]# cd /usr/local/awstats/tools/
[root@nginx tools]# ./awstats_configure.pl
----- AWStats awstats_configure 1.0 (build 1.9) (c) Laurent Destailleur -----
This tool will help you to configure AWStats to analyze statistics for
one web server. You can try to use it to let it do all that is possible
in AWStats setup, however following the step by step manual setup
documentation (docs/index.html) is often a better idea. Above all if:
- You are not an administrator user,
- You want to analyze downloaded log files without web server,
- You want to analyze mail or ftp log files instead of web log files,
- You need to analyze load balanced servers log files,
- You want to 'understand' all possible ways to use AWStats...
Read the AWStats documentation (docs/index.html).
-----> Running OS detected: Linux, BSD or Unix
-----> Check for web server install
Enter full config file path of your Web server.
Example: /etc/httpd/httpd.conf
Example: /usr/local/apache2/conf/httpd.conf
Example: c:\Program files\apache group\apache\conf\httpd.conf
Config file path ('none' to skip web server setup):
> none #创建个新的站点
Your web server config file(s) could not be found.
You will need to setup your web server manually to declare AWStats
script as a CGI, if you want to build reports dynamically.
See AWStats setup documentation (file docs/index.html)
-----> Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf'
File awstats.model.conf updated.
-----> Need to create a new config file ?
Do you want me to build a new AWStats config/profile
file (required if first install) [y/N] ? y #首次配置
-----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Your web site, virtual server or profile name:
> #站点名称
-----> Define config file path
In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directory path to store config file(s) (Enter for default):
>
-----> Create config file '/etc/awstats/awstats.www.renzhiyuan.com.conf'
Config file /etc/awstats/awstats.www.renzhiyuan.com.conf created.
-----> Add update process inside a scheduler
Sorry, configure.pl does not support automatic add to cron yet.
You can do it manually by adding the following command to your cron:
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=www.renzhiyuan.com
Or if you have several config files and prefer having only one command:
/usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...
A SIMPLE config file has been created: /etc/awstats/awstats.www.renzhiyuan.com.conf
You should have a look inside to check and change manually main parameters.
You can then manually update your statistics for 'www.renzhiyuan.com' with command:
> perl awstats.pl -update -config=www.renzhiyuan.com
You can also build static report pages for 'www.renzhiyuan.com' with command:
> perl awstats.pl -output=pagetype -config=www.renzhiyuan.com
Press ENTER to finish...
[root@nginx tools]#2.3.2)配置awstats.www.renzhiyuan.com.conf 程序执行结束后,会在/etc/awstats/目录下生成你的配置文件。然后编辑配置文件,修改LogFile参数,跟日志切割脚本中的日志路径对应起来:LogFile="/usr/local/nginx/logs/access_%YYYY-24%MM-24%DD-24.log"
#LogFile="/usr/local/nginx/logs/access.log"注意:根据自己日志切割方式,决定是监控昨天的还是先监控,然后再切割。[root@nginx ~]# /usr/local/awstats/wwwroot/cgi-bin/awstats.pl --config=www.renzhiyuan.com
Create/Update database for config "/etc/awstats/awstats.www.renzhiyuan.com.conf" by AWStats version 7.0 (build 1.971)
From data in log file "/usr/local/nginx/logs/access.log"...
Phase 1 : First bypass old records, searching new record...
Searching new records from beginning of log file...
Jumped lines in file: 0
Parsed lines in file: 25
Found 0 dropped records,
Found 0 comments,
Found 0 blank records,
Found 25 corrupted records,
Found 0 old records,
Found 0 new qualified records.
[root@nginx ~]#2.4)配置awstats生产静态页面/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.renzhiyuan.com -lang=cn -dir=/usr/local/nginx/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl注意:-config=后面的参数是你在执行awstats_configure.pl时输入的站点域名。2.4.1)计划任务[root@nginx ~]# crontab -l
59 23 * * * /root/nginxcut.sh
1 0 * * * /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=www.renzhiyuan.com -lang=cn -dir=/usr/local/nginx/html/awstats -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl/usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具 -update -config=akii.org 更新配置项 -lang=cn 语言为中文 -dir= 统计结果输出目录 -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径。2.4.2)加密[root@nginx ~]# yum install httpd-tools -y
[root@nginx nginx]# pwd
/usr/local/nginx
[root@nginx nginx]# htpasswd -cd renzhiyuan.pass renzhiyuan
New password:
Re-type new password:
Adding password for user renzhiyuan
[root@nginx nginx]#2.4.3)nginx配置文件配置[root@nginx vhost]# cat awstats.conf
server {
listen 80;
server_name 192.168.1.220;
location ~ ^/awstats/ { # html 静态页面目录
root html;
index index.html;
access_log off;
error_log off;
charset gb2312;
# auth_basic "renzhiyuan";
# auth_basic_user_file /usr/local/nginx/.renzhiyuan.pass;
}
location ~ ^/icon/ { # 图标目录
root /usr/local/awstats/wwwroot;
index index.html;
access_log off;
error_log off;
}
location / {
return 403;
}
}
[root@nginx vhost]#2.5)访问:
2.6)功能评估:2.6.1)功能强大,但是要注意awstats的安全,比如漏洞,平时做好安全工作。方式三、用ELK统计Nginx日志:[root@elk config]# cat catalinalog.conf
input {
file {
path => "/usr/local/nginx/log/access.log" #日志路径
start_position => "beginning"
}
}
filter {
if [path] =~ "access" {
mutate { replace => { "type" => "nginx access.log" } }
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
}
date {
match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
}
}
output {
elasticsearch {
hosts => ["192.168.1.225:9200"] #elasticsearch地址
}
stdout { codec => rubydebug }
}
[root@elk config]#
3.4)安装可视化工具(后期详细讲解)我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这可能是个愚蠢的问题。但是,我是一个新手......你怎么能在交互式rubyshell中有多行代码?好像你只能有一条长线。按回车键运行代码。无论如何我可以在不运行代码的情况下跳到下一行吗?再次抱歉,如果这是一个愚蠢的问题。谢谢。 最佳答案 这是一个例子:2.1.2:053>a=1=>12.1.2:054>b=2=>22.1.2:055>a+b=>32.1.2:056>ifa>b#Thecode‘if..."startsthedefinitionoftheconditionalstatement.2.1.2:057?>puts"f
我从Ubuntu服务器上的RVM转移到rbenv。当我使用RVM时,使用bundle没有问题。转移到rbenv后,我在Jenkins的执行shell中收到“找不到命令”错误。我内爆并删除了RVM,并从~/.bashrc'中删除了所有与RVM相关的行。使用后我仍然收到此错误:rvmimploderm~/.rvm-rfrm~/.rvmrcgeminstallbundlerecho'exportPATH="$HOME/.rbenv/bin:$PATH"'>>~/.bashrcecho'eval"$(rbenvinit-)"'>>~/.bashrc.~/.bashrcrbenvversions
我有一个问题。我想从另一个ruby脚本运行一个ruby脚本并捕获它的输出信息,同时让它也输出到屏幕。亚军#!/usr/bin/envrubyprint"Enteryourpassword:"password=gets.chompputs"Hereisyourpassword:#{password}"我运行的脚本文件:开始.rboutput=`runner`putsoutput.match(/Hereisyour(password:.*)/).captures[0].to_s正如您在此处看到的那样,存在问题。在start.rb的第一行,屏幕是空的。我在运行程序中看不到“输入您的密
A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(
在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa
我想在heroku.com上查看我的应用程序日志的内容,所以我关注了thisexcellentadvice并拥有我所有的日志内容。但是我现在很想知道我的日志文件实际在哪里,因为“log/production.log”似乎是空的:C:\>herokuconsoleRubyconsoleforajpbrevx.heroku.com>>files=Dir.glob("*")=>["public","tmp","spec","Rakefile","doc","config.ru","app","config","lib","README","Gemfile.lock","vendor","sc
我们如何从ruby脚本返回值?#!/usr/bin/envrubya="test"a我们如何在Ubuntu终端或java或c中访问'a'的值? 最佳答案 在ruby/python脚本中打印你的变量,然后可以通过示例从shell脚本中读取它:#!/bin/bashruby_var=$(rubymyrubyscript.rb)python_var=$(pythonmypythonscript.py)echo"$ruby_var"echo"$python_var"注意你的ruby/python脚本只打印这个变量(有更多复杂的方
我在跑Fastlane(适用于iOS的持续构建工具)以执行用于解密文件的自定义shell脚本。这是命令。sh"./decrypt.shENV['ENCRYPTION_P12']"我想不出将环境变量传递给该脚本的方法。显然,如果我将密码硬编码到脚本中,它就可以正常工作。sh"./decrypt.shmypwd"有什么建议吗? 最佳答案 从直接Shell中扩展假设这里的sh是一个faSTLane命令,它以给定的参数作为脚本文本调用shell命令:#asafastlanedirectivesh'./decrypt.sh"$ENCRYPTI
我希望这些值匹配。当shell脚本由于某些错误条件而退出时(因此返回非零值),它们不匹配。壳$?返回1,ruby$?返回256。>>%x[lskkr]ls:kkr:Nosuchfileordirectory=>"">>puts$?256=>nil>>exitHadoop:~Madcap$lskkrls:kkr:NosuchfileordirectoryHadoop:~Madcap$echo$?1 最佳答案 在Ruby中$?是一个Process::Status实例。打印$?等同于调用$?.to_s,这等同于$?.to_i.to_s(来