{"id":3852,"date":"2019-02-12T18:20:45","date_gmt":"2019-02-12T10:20:45","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3852"},"modified":"2019-02-12T18:20:45","modified_gmt":"2019-02-12T10:20:45","slug":"nginx-%e7%9a%84-log_format-%e6%97%a5%e5%bf%97%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3852","title":{"rendered":"Nginx \u7684 log_format \u65e5\u5fd7\u683c\u5f0f"},"content":{"rendered":"<p>\n\t\u57fa\u672c\u4e0a\u4efb\u4f55\u4e00\u4e2a\u7cfb\u7edf\u6216\u8005\u6846\u67b6\u90fd\u6709\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0cNginx \u4e5f\u4e0d\u4f8b\u5916\u3002\n<\/p>\n<p>\n\t\u524d\u9762\u6211\u5728\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3570\">\u6628\u5929\uff0c\u6211\u548c\u9ed1\u5ba2\u4e4b\u95f4\u7684\u4e00\u6b21\u653b\u9632\u6f14\u7ec3\uff01<\/a>\u300b\u8fd9\u7bc7\u6587\u7ae0\u4e2d\u4e5f\u4f7f\u7528\u5230\u4e86 Nginx \u7684&nbsp;access_log \u65e5\u5fd7\u3002\u4eca\u5929\u6211\u4eec\u91cd\u70b9\u6765\u8bf4\u4e00\u4e0b\uff0cNginx \u4e2d\u7684 log_format \u65e5\u5fd7\u683c\u5f0f\u7684\u914d\u7f6e\u8bbe\u7f6e\u3002\n<\/p>\n<p>\n\t\u9996\u5148\u8bf4\u660e\u4e00\u70b9\uff0cNginx \u4e2d\u7684\u65e5\u5fd7\u76f8\u5173\u7684\u914d\u7f6e\u6307\u4ee4\uff0c\u90fd\u5728 ngx_http_log_module \u6a21\u5757\u4e2d\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"log_format \u914d\u7f6e\u6848\u4f8b\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/wx1.sinaimg.cn\/large\/8fa5dcfcgy1g03snxcj5dj20jo09kn0y.jpg\" style=\"width: 708px;height: 344px\" \/>\n<\/p>\n<h2>\n\tlog_format \u7684\u8bed\u6cd5<br \/>\n<\/h2>\n<pre class=\"brush:bash;\">\r\nlog_format name string &hellip;;<\/pre>\n<p>\n\t\u53c2\u6570\u89e3\u91ca\u5982\u4e0b\uff1a\n<\/p>\n<ul>\n<li>\n\t\tname \u8868\u793a\u683c\u5f0f\u540d\u79f0\n\t<\/li>\n<li>\n\t\tstring \u8868\u793a\u7b49\u4e49\u7684\u683c\u5f0f\n\t<\/li>\n<\/ul>\n<h2>\n\tlog_format \u7684\u9ed8\u8ba4\u503c<br \/>\n<\/h2>\n<p>\n\tlog_format \u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u65e0\u9700\u8bbe\u7f6e\u7684 combined \u65e5\u5fd7\u683c\u5f0f\uff0c\u76f8\u5f53\u4e8e apache \u7684 combined \u65e5\u5fd7\u683c\u5f0f\u3002\u683c\u5f0f\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\nlog_format  combined  &#039;$remote_addr - $remote_user  [$time_local]  &#039;\r\n &#039; &quot;$request&quot;  $status  $body_bytes_sent  &#039;\r\n &#039; &quot;$http_referer&quot;  &quot;$http_user_agent&quot; &#039;;<\/pre>\n<p>\n\t\u4f46\u662f\u9ed8\u8ba4\u7684\u65e5\u5fd7\u5e76\u4e0d\u5b8c\u7f8e\u3002\u4f8b\u5982\uff0c\u5f53 nginx \u4f4d\u4e8e\u8d1f\u8f7d\u5747\u8861\u5668\uff0csquid\uff0cnginx \u53cd\u5411\u4ee3\u7406\u4e4b\u540e\uff0cweb \u670d\u52a1\u5668\u65e0\u6cd5\u76f4\u63a5\u83b7\u53d6\u5230\u5ba2\u6237\u7aef\u771f\u5b9e\u7684 IP \u5730\u5740\u4e86\u3002$remote_addr \u83b7\u53d6\u53cd\u5411\u4ee3\u7406\u7684 IP \u5730\u5740\u3002\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u5728\u8f6c\u53d1\u8bf7\u6c42\u7684 http \u5934\u4fe1\u606f\u4e2d\uff0c\u53ef\u4ee5\u589e\u52a0 X-Forwarded-For \u4fe1\u606f\uff0c\u7528\u6765\u8bb0\u5f55 \u5ba2\u6237\u7aef IP \u5730\u5740\u548c\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u5730\u5740\u3002\n<\/p>\n<h2>\n\tlog_format \u6307\u4ee4\u7684\u914d\u7f6e\u6bb5<br \/>\n<\/h2>\n<p>\n\tlog_format \u6307\u4ee4\u7684\u914d\u7f6e\u6bb5\u662f\u5728 http \u6307\u4ee4\u5757\u4e2d\u3002\n<\/p>\n<h2>\n\tlog_format \u5e38\u7528\u7684\u53d8\u91cf\u53c2\u6570<br \/>\n<\/h2>\n<p>\n\t<img decoding=\"async\" alt=\"log_format \u5e38\u7528\u7684\u53d8\u91cf\u53c2\u6570\" src=\"http:\/\/ww3.sinaimg.cn\/large\/72c913fbgw1f2l1htuh5yj20om0f3aac.jpg\" style=\"width: 886px;height: 543px\" \/>\n<\/p>\n<pre class=\"brush:bash;\">\r\n$remote_addr, $http_x_forwarded_for\uff08\u53cd\u5411\uff09 \u8bb0\u5f55\u5ba2\u6237\u7aefIP\u5730\u5740\r\n$remote_user \u8bb0\u5f55\u5ba2\u6237\u7aef\u7528\u6237\u540d\u79f0\r\n$request \u8bb0\u5f55\u8bf7\u6c42\u7684URL\u548cHTTP\u534f\u8bae\r\n$status \u8bb0\u5f55\u8bf7\u6c42\u72b6\u6001\r\n$body_bytes_sent \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u5b57\u8282\u6570\uff0c\u4e0d\u5305\u62ec\u54cd\u5e94\u5934\u7684\u5927\u5c0f\uff1b \u8be5\u53d8\u91cf\u4e0eApache\u6a21\u5757mod_log_config\u91cc\u7684&ldquo;%B&rdquo;\u53c2\u6570\u517c\u5bb9\u3002\r\n$bytes_sent \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u603b\u5b57\u8282\u6570\u3002\r\n$connection \u8fde\u63a5\u7684\u5e8f\u5217\u53f7\u3002\r\n$connection_requests \u5f53\u524d\u901a\u8fc7\u4e00\u4e2a\u8fde\u63a5\u83b7\u5f97\u7684\u8bf7\u6c42\u6570\u91cf\u3002\r\n$msec \u65e5\u5fd7\u5199\u5165\u65f6\u95f4\u3002\u5355\u4f4d\u4e3a\u79d2\uff0c\u7cbe\u5ea6\u662f\u6beb\u79d2\u3002\r\n$pipe \u5982\u679c\u8bf7\u6c42\u662f\u901a\u8fc7HTTP\u6d41\u6c34\u7ebf(pipelined)\u53d1\u9001\uff0cpipe\u503c\u4e3a&ldquo;p&rdquo;\uff0c\u5426\u5219\u4e3a&ldquo;.&rdquo;\u3002\r\n$http_referer \u8bb0\u5f55\u4ece\u54ea\u4e2a\u9875\u9762\u94fe\u63a5\u8bbf\u95ee\u8fc7\u6765\u7684\r\n$http_user_agent \u8bb0\u5f55\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u76f8\u5173\u4fe1\u606f\r\n$request_length \u8bf7\u6c42\u7684\u957f\u5ea6\uff08\u5305\u62ec\u8bf7\u6c42\u884c\uff0c\u8bf7\u6c42\u5934\u548c\u8bf7\u6c42\u6b63\u6587\uff09\u3002\r\n$request_time \u8bf7\u6c42\u5904\u7406\u65f6\u95f4\uff0c\u5355\u4f4d\u4e3a\u79d2\uff0c\u7cbe\u5ea6\u6beb\u79d2\uff1b \u4ece\u8bfb\u5165\u5ba2\u6237\u7aef\u7684\u7b2c\u4e00\u4e2a\u5b57\u8282\u5f00\u59cb\uff0c\u76f4\u5230\u628a\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u540e\u8fdb\u884c\u65e5\u5fd7\u5199\u5165\u4e3a\u6b62\u3002\r\n$time_iso8601 ISO8601\u6807\u51c6\u683c\u5f0f\u4e0b\u7684\u672c\u5730\u65f6\u95f4\u3002\r\n$time_local \u901a\u7528\u65e5\u5fd7\u683c\u5f0f\u4e0b\u7684\u672c\u5730\u65f6\u95f4\u3002<\/pre>\n<p>\n\t\u4e0b\u9762\u770b\u4e00\u4e2a\u7b80\u5355\u7684\u914d\u7f6e\u6848\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\nhttp {\r\n  log_format  main  &#039;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#039;\r\n    &#039;&quot;$status&quot; $body_bytes_sent &quot;$http_referer&quot; &#039;\r\n    &#039;&quot;$http_user_agent&quot; &quot;$http_x_forwarded_for&quot; &#039;\r\n    &#039;&quot;$gzip_ratio&quot; $request_time $bytes_sent $request_length&#039;;\r\n\r\n  log_format  porxy  &#039;$http_x_forwarded_for - $remote_user  [$time_local]  &#039;\r\n    &#039; &quot;$request&quot;  $status $body_bytes_sent &#039;\r\n    &#039; &quot;$http_referer&quot;  &quot;$http_user_agent&quot; &#039;;\r\n\r\n  log_format srcache_log &#039;$remote_addr - $remote_user [$time_local] &quot;$request&quot; &#039;\r\n    &#039;&quot;$status&quot; $body_bytes_sent $request_time $bytes_sent $request_length &#039;\r\n    &#039;[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]&#039;;\r\n\r\n  open_log_file_cache max=1000 inactive=60s;\r\n\r\n  server {\r\n    server_name ~^(www\\.)?(.+)$;\r\n    access_log logs\/$2-access.log main;\r\n    error_log logs\/$2-error.log;\r\n    location \/srcache {\r\n      access_log logs\/access-srcache.log srcache_log;\r\n    }\r\n  }\r\n}<\/pre>\n<p>\n\t\u5173\u4e8e nginx \u65e5\u5fd7\u7684\u5176\u4ed6\u914d\u7f6e\u6307\u4ee4\uff1aaccess_log(\u8bbf\u95ee\u65e5\u5fd7)\u3001log_format\uff08\u65e5\u5fd7\u683c\u5f0f\uff09\u3001open_log_file_cache\u3001log_not_found\u3001log_subrequest\u3001rewrite_log\u3001error_log \u7b49\u6211\u4eec\u540e\u9762\u518d\u8bb2\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3852\">Nginx \u7684 log_format \u65e5\u5fd7\u683c\u5f0f<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u4e0a\u4efb\u4f55\u4e00\u4e2a\u7cfb\u7edf\u6216\u8005\u6846\u67b6\u90fd\u6709\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\uff0cNginx \u4e5f\u4e0d\u4f8b\u5916\u3002 \u524d\u9762\u6211\u5728\u300a\u6628\u5929\uff0c\u6211\u548c\u9ed1\u5ba2\u4e4b\u95f4\u7684\u4e00\u6b21\u653b\u9632\u6f14\u7ec3 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[1827,1921,1152,313,1154],"class_list":["post-3852","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-access_log","tag-log_format","tag-nginx","tag-nginx-tfs"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3852"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3852\/revisions"}],"predecessor-version":[{"id":3854,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3852\/revisions\/3854"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}