﻿{"id":1634,"date":"2017-07-20T15:41:48","date_gmt":"2017-07-20T07:41:48","guid":{"rendered":"http:\/\/nick.txtcc.com\/?p=1634"},"modified":"2017-07-20T15:41:48","modified_gmt":"2017-07-20T07:41:48","slug":"nginx-access-log%e6%97%a5%e5%bf%97%e7%bb%9f%e8%ae%a1%e5%88%86%e6%9e%90%e5%b8%b8%e7%94%a8%e5%91%bd%e4%bb%a4","status":"publish","type":"post","link":"http:\/\/nick.txtcc.com\/index.php\/linux\/1634","title":{"rendered":"Nginx Access Log\u65e5\u5fd7\u7edf\u8ba1\u5206\u6790\u5e38\u7528\u547d\u4ee4"},"content":{"rendered":"<h2 id=\"ip\u76f8\u5173\u7edf\u8ba1\">IP\u76f8\u5173\u7edf\u8ba1<\/h2>\n<p><strong>\u7edf\u8ba1IP\u8bbf\u95ee\u91cf<\/strong><\/p>\n<pre><code class=\"hljs perl\">awk &lt;span class=&quot;hljs-string&quot;&gt;'{print $1}'&lt;\/span&gt; access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; | &lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; -n | uni&lt;span class=&quot;hljs-string&quot;&gt;q | wc -l&lt;\/span&gt;<\/code><\/pre>\n<p><strong>\u67e5\u770b\u67d0\u4e00\u65f6\u95f4\u6bb5\u7684IP\u8bbf\u95ee\u91cf(4-5\u70b9)<\/strong><\/p>\n<pre><code class=\"hljs perl\">&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;07\/Apr\/2017:0[4-5]&quot;&lt;\/span&gt; access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; | awk &lt;span class=&quot;hljs-string&quot;&gt;'{print $1}'&lt;\/span&gt; | &lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; | uniq -c| &lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; -nr | wc -l   <\/code><\/pre>\n<p><strong>\u67e5\u770b\u8bbf\u95ee\u6700\u9891\u7e41\u7684\u524d100\u4e2aIP<\/strong><\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt;}' access.log | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -n |uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -rn | head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<p><strong>\u67e5\u770b\u8bbf\u95ee100\u6b21\u4ee5\u4e0a\u7684IP<\/strong><\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt;}' access.log | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -n |uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; |awk '{&lt;span class=&quot;hljs-keyword&quot;&gt;if&lt;\/span&gt;($&lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt; &gt;&lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;) &lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;0&lt;\/span&gt;}'|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -rn<\/code><\/pre>\n<p><strong>\u67e5\u8be2\u67d0\u4e2aIP\u7684\u8be6\u7ec6\u8bbf\u95ee\u60c5\u51b5,\u6309\u8bbf\u95ee\u9891\u7387\u6392\u5e8f<\/strong><\/p>\n<pre><code class=\"hljs perl\">&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;'104.217.108.66'&lt;\/span&gt; access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; |awk &lt;span class=&quot;hljs-string&quot;&gt;'{print $7}'&lt;\/span&gt;|&lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; |uniq -c |&lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; -rn |head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;   <\/code><\/pre>\n<h2 id=\"\u9875\u9762\u8bbf\u95ee\u7edf\u8ba1\">\u9875\u9762\u8bbf\u95ee\u7edf\u8ba1<\/h2>\n<p><strong>\u67e5\u770b\u8bbf\u95ee\u6700\u9891\u7684\u9875\u9762(TOP100)<\/strong><\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;7&lt;\/span&gt;}' access.log | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; |uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -rn | head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<p><strong>\u67e5\u770b\u8bbf\u95ee\u6700\u9891\u7684\u9875\u9762([\u6392\u9664php\u9875\u9762\u3011(TOP100)<\/strong><\/p>\n<pre><code class=\"hljs perl\">&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; -v &lt;span class=&quot;hljs-string&quot;&gt;&quot;.php&quot;&lt;\/span&gt;  access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; | awk &lt;span class=&quot;hljs-string&quot;&gt;'{print $7}'&lt;\/span&gt; | &lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; |uniq -c | &lt;span class=&quot;hljs-keyword&quot;&gt;sort&lt;\/span&gt; -rn | head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;          <\/code><\/pre>\n<p><strong>\u67e5\u770b\u9875\u9762\u8bbf\u95ee\u6b21\u6570\u8d85\u8fc7100\u6b21\u7684\u9875\u9762<\/strong><\/p>\n<pre><code class=\"hljs nginx\">&lt;span class=&quot;hljs-attribute&quot;&gt;cat&lt;\/span&gt; access.log | cut -d &lt;span class=&quot;hljs-string&quot;&gt;' '&lt;\/span&gt; -f &lt;span class=&quot;hljs-number&quot;&gt;7&lt;\/span&gt; | sort |uniq -c | awk &lt;span class=&quot;hljs-string&quot;&gt;'{if (&lt;span class=&quot;hljs-variable&quot;&gt;$1&lt;\/span&gt; &gt; 100) print &lt;span class=&quot;hljs-variable&quot;&gt;$0&lt;\/span&gt;}'&lt;\/span&gt; | less<\/code><\/pre>\n<p><strong>\u67e5\u770b\u6700\u8fd11000\u6761\u8bb0\u5f55\uff0c\u8bbf\u95ee\u91cf\u6700\u9ad8\u7684\u9875\u9762<\/strong><\/p>\n<pre><code class=\"hljs swift\">tail -&lt;span class=&quot;hljs-number&quot;&gt;1000&lt;\/span&gt; access.log |awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;7&lt;\/span&gt;}'|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt;|uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr|less<\/code><\/pre>\n<h2 id=\"\u6bcf\u79d2\u8bf7\u6c42\u91cf\u7edf\u8ba1\">\u6bcf\u79d2\u8bf7\u6c42\u91cf\u7edf\u8ba1<\/h2>\n<p>\u7edf\u8ba1\u6bcf\u79d2\u7684\u8bf7\u6c42\u6570,top100\u7684\u65f6\u95f4\u70b9(\u7cbe\u786e\u5230\u79d2)<\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;4&lt;\/span&gt;}' access.log |cut -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;14&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;21&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt;|uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr|head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<h2 id=\"\u6bcf\u5206\u949f\u8bf7\u6c42\u91cf\u7edf\u8ba1\">\u6bcf\u5206\u949f\u8bf7\u6c42\u91cf\u7edf\u8ba1<\/h2>\n<p>\u7edf\u8ba1\u6bcf\u5206\u949f\u7684\u8bf7\u6c42\u6570,top100\u7684\u65f6\u95f4\u70b9(\u7cbe\u786e\u5230\u5206\u949f)<\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;4&lt;\/span&gt;}' access.log |cut -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;14&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;18&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt;|uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr|head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<h2 id=\"\u6bcf\u5c0f\u65f6\u8bf7\u6c42\u91cf\u7edf\u8ba1\">\u6bcf\u5c0f\u65f6\u8bf7\u6c42\u91cf\u7edf\u8ba1<\/h2>\n<p>\u7edf\u8ba1\u6bcf\u5c0f\u65f6\u7684\u8bf7\u6c42\u6570,top100\u7684\u65f6\u95f4\u70b9(\u7cbe\u786e\u5230\u5c0f\u65f6)<\/p>\n<pre><code class=\"hljs swift\">awk '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;4&lt;\/span&gt;}' access.log |cut -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;14&lt;\/span&gt;-&lt;span class=&quot;hljs-number&quot;&gt;15&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt;|uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr|head -n &lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<h2 id=\"\u6027\u80fd\u5206\u6790\">\u6027\u80fd\u5206\u6790<\/h2>\n<p>\u5728nginx log\u4e2d\u6700\u540e\u4e00\u4e2a\u5b57\u6bb5\u52a0\u5165$request_time<\/p>\n<p><strong>\u5217\u51fa\u4f20\u8f93\u65f6\u95f4\u8d85\u8fc7 3 \u79d2\u7684\u9875\u9762\uff0c\u663e\u793a\u524d20\u6761<\/strong><\/p>\n<pre><code class=\"hljs swift\">cat access.log|awk '($&lt;span class=&quot;hljs-type&quot;&gt;NF&lt;\/span&gt; &gt; &lt;span class=&quot;hljs-number&quot;&gt;3&lt;\/span&gt;){&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;7&lt;\/span&gt;}'|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -n|uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt;|&lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr|head -&lt;span class=&quot;hljs-number&quot;&gt;20&lt;\/span&gt;<\/code><\/pre>\n<p><strong>\u5217\u51faphp\u9875\u9762\u8bf7\u6c42\u65f6\u95f4\u8d85\u8fc73\u79d2\u7684\u9875\u9762\uff0c\u5e76\u7edf\u8ba1\u5176\u51fa\u73b0\u7684\u6b21\u6570\uff0c\u663e\u793a\u524d100\u6761<\/strong><\/p>\n<pre><code class=\"hljs nginx\">&lt;span class=&quot;hljs-attribute&quot;&gt;cat&lt;\/span&gt; access.log|awk &lt;span class=&quot;hljs-string&quot;&gt;'(&lt;span class=&quot;hljs-variable&quot;&gt;$NF&lt;\/span&gt; &gt; 1 &amp;&amp;  &lt;span class=&quot;hljs-variable&quot;&gt;$7&lt;\/span&gt;~\/\\.php\/){print &lt;span class=&quot;hljs-variable&quot;&gt;$7&lt;\/span&gt;}'&lt;\/span&gt;|sort -n|uniq -c|sort -nr|head -&lt;span class=&quot;hljs-number&quot;&gt;100&lt;\/span&gt;<\/code><\/pre>\n<h2 id=\"\u8718\u86db\u6293\u53d6\u7edf\u8ba1\">\u8718\u86db\u6293\u53d6\u7edf\u8ba1<\/h2>\n<p><strong>\u7edf\u8ba1\u8718\u86db\u6293\u53d6\u6b21\u6570<\/strong><\/p>\n<pre><code class=\"hljs perl\">&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;'Baiduspider'&lt;\/span&gt; access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; |wc -l<\/code><\/pre>\n<p><strong>\u7edf\u8ba1\u8718\u86db\u6293\u53d6404\u7684\u6b21\u6570<\/strong><\/p>\n<pre><code class=\"hljs perl\">&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;'Baiduspider'&lt;\/span&gt; access.&lt;span class=&quot;hljs-keyword&quot;&gt;log&lt;\/span&gt; |&lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;'404'&lt;\/span&gt; | wc -l<\/code><\/pre>\n<h2 id=\"tcp\u8fde\u63a5\u7edf\u8ba1\">TCP\u8fde\u63a5\u7edf\u8ba1<\/h2>\n<p>\u67e5\u770b\u5f53\u524dTCP\u8fde\u63a5\u6570<\/p>\n<pre><code class=\"hljs perl\">netstat -tan | &lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;ESTABLISHED&quot;&lt;\/span&gt; | &lt;span class=&quot;hljs-keyword&quot;&gt;grep&lt;\/span&gt; &lt;span class=&quot;hljs-string&quot;&gt;&quot;:80&quot;&lt;\/span&gt; | wc -l<\/code><\/pre>\n<p>\u7528tcpdump\u55c5\u63a280\u7aef\u53e3\u7684\u8bbf\u95ee\u770b\u770b\u8c01\u6700\u9ad8<\/p>\n<pre><code class=\"hljs swift\">tcpdump -i eth0 -tnn dst port &lt;span class=&quot;hljs-number&quot;&gt;80&lt;\/span&gt; -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; &lt;span class=&quot;hljs-number&quot;&gt;1000&lt;\/span&gt; | awk -&lt;span class=&quot;hljs-type&quot;&gt;F&lt;\/span&gt;\n&lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt; '{&lt;span class=&quot;hljs-built_in&quot;&gt;print&lt;\/span&gt; $&lt;span class=&quot;hljs-number&quot;&gt;1&lt;\/span&gt;\n&lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt;$&lt;span class=&quot;hljs-number&quot;&gt;2&lt;\/span&gt;\n&lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt;$&lt;span class=&quot;hljs-number&quot;&gt;3&lt;\/span&gt;\n&lt;span class=&quot;hljs-string&quot;&gt;&quot;.&quot;&lt;\/span&gt;$&lt;span class=&quot;hljs-number&quot;&gt;4&lt;\/span&gt;}' | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; | uniq -&lt;span class=&quot;hljs-built_in&quot;&gt;c&lt;\/span&gt; | &lt;span class=&quot;hljs-built_in&quot;&gt;sort&lt;\/span&gt; -nr<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>IP\u76f8\u5173\u7edf\u8ba1 \u7edf\u8ba1IP\u8bbf\u95ee\u91cf awk <span class=\"hljs-string\">&#8216;{print $1}&#8217;<\/span> access.<sp&#46;&#46;&#46;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1634","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":1,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"predecessor-version":[{"id":1635,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/posts\/1634\/revisions\/1635"}],"wp:attachment":[{"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nick.txtcc.com\/index.php\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}