{"id":3695,"date":"2018-10-31T20:24:00","date_gmt":"2018-10-31T12:24:00","guid":{"rendered":"http:\/\/switch.linesno.com\/?p=3695"},"modified":"2018-10-31T20:24:00","modified_gmt":"2018-10-31T12:24:00","slug":"nginx%e7%9a%84%e6%97%a5%e5%bf%97%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"http:\/\/switch.linesno.com\/?p=3695","title":{"rendered":"nginx\u7684\u65e5\u5fd7\u914d\u7f6e"},"content":{"rendered":"<p>nginx\u6709\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5f0f\u3002\u6bcf\u4e2a\u7ea7\u522b\u7684\u914d\u7f6e\u53ef\u4ee5\u6709\u5404\u81ea\u72ec\u7acb\u7684\u8bbf\u95ee\u65e5\u5fd7\u3002\u65e5\u5fd7\u683c\u5f0f\u901a\u8fc7log_format\u547d\u4ee4\u6765\u5b9a\u4e49\u3002\u00a0ngx_http_log_module \u662f\u7528\u6765\u5b9a\u4e49\u8bf7\u6c42\u65e5\u5fd7\u683c\u5f0f\u7684\u3002<\/p>\n<p>nginx\u65e5\u5fd7\u76f8\u5173\u7684\u914d\u7f6e\u6709log_format\u3001access_log\u3001open_log_file_cache\u3001log_not_found\u3001log_subrequest\u3001rewrite_log\u3001error_log\u3002\u8fd9\u4e9b\u914d\u7f6e\u4e3b\u8981\u52062\u7c7b\uff1a\u65e5\u5fd7\u8bb0\u5f55\u683c\u5f0f\uff0c\u65e5\u5fd7\u7c7b\u578b\u53ca\u4f4d\u7f6e\u3002<\/p>\n<h2><\/h2>\n<ul>\n<li>access_log\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: access_log path [format [buffer=size [flush=time]]];<\/p>\n<div id=\"code362\" class=\"codebody\">access_log path format gzip[=level] [buffer=size] [flush=time];<br \/>\naccess_log syslog:server=address[,parameter=value] [format];<br \/>\naccess_log off;<\/div>\n<p>\u9ed8\u8ba4\u503c: access_log logs\/access.log combined;<br \/>\n\u914d\u7f6e\u6bb5: http, server, location, if in location, limit_except<\/p>\n<p>gzip\u538b\u7f29\u7b49\u7ea7\u3002<br \/>\nbuffer\u8bbe\u7f6e\u5185\u5b58\u7f13\u5b58\u533a\u5927\u5c0f\u3002<br \/>\nflush\u4fdd\u5b58\u5728\u7f13\u5b58\u533a\u4e2d\u7684\u6700\u957f\u65f6\u95f4\u3002<br \/>\n\u4e0d\u8bb0\u5f55\u65e5\u5fd7\uff1aaccess_log off;<br \/>\n\u4f7f\u7528\u9ed8\u8ba4combined\u683c\u5f0f\u8bb0\u5f55\u65e5\u5fd7\uff1aaccess_log logs\/access.log \u6216 access_log logs\/access.log combined;<\/p>\n<h2><\/h2>\n<ul>\n<li>log_format\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: log_format name string \u2026;<br \/>\n\u9ed8\u8ba4\u503c: log_format combined \u201c\u2026\u201d;<br \/>\n\u914d\u7f6e\u6bb5: http<\/p>\n<p>name\u8868\u793a\u683c\u5f0f\u540d\u79f0\uff0cstring\u8868\u793a\u7b49\u4e49\u7684\u683c\u5f0f\u3002log_format\u6709\u4e00\u4e2a\u9ed8\u8ba4\u7684\u65e0\u9700\u8bbe\u7f6e\u7684combined\u65e5\u5fd7\u683c\u5f0f\uff0c\u76f8\u5f53\u4e8eapache\u7684combined\u65e5\u5fd7\u683c\u5f0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<div id=\"code92076\" class=\"codebody\">log_format\u00a0 combined\u00a0 &#8216;$remote_addr &#8211; $remote_user\u00a0 [$time_local]\u00a0 &#8216;<br \/>\n&#8216; &#8220;$request&#8221;\u00a0 $status\u00a0 $body_bytes_sent\u00a0 &#8216;<br \/>\n&#8216; &#8220;$http_referer&#8221;\u00a0 &#8220;$http_user_agent&#8221; &#8216;;<\/div>\n<p><strong>\u5982\u679cnginx\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\u7684IP\u5730\u5740\u4e86<\/strong>\u3002 $remote_addr\u83b7\u53d6\u53cd\u5411\u4ee3\u7406\u7684IP\u5730\u5740\u3002\u53cd\u5411\u4ee3\u7406\u670d\u52a1\u5668\u5728\u8f6c\u53d1\u8bf7\u6c42\u7684http\u5934\u4fe1\u606f\u4e2d\uff0c\u53ef\u4ee5\u589e\u52a0X-Forwarded-For\u4fe1\u606f\uff0c\u7528\u6765\u8bb0\u5f55 \u5ba2\u6237\u7aefIP\u5730\u5740\u548c\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u670d\u52a1\u5668\u5730\u5740\u3002\u5982\u4e0b\u6240\u793a<\/p>\n<div id=\"code70050\" class=\"codebody\">log_format\u00a0 porxy\u00a0 &#8216;$http_x_forwarded_for &#8211; $remote_user\u00a0 [$time_local]\u00a0 &#8216;<br \/>\n&#8216; &#8220;$request&#8221;\u00a0 $status $body_bytes_sent &#8216;<br \/>\n&#8216; &#8220;$http_referer&#8221;\u00a0 &#8220;$http_user_agent&#8221; &#8216;;<\/div>\n<p>\u65e5\u5fd7\u683c\u5f0f\u5141\u8bb8\u5305\u542b\u7684\u53d8\u91cf\u6ce8\u91ca\u5982\u4e0b\uff1a<\/p>\n<div id=\"code9571\">$remote_addr, $http_x_forwarded_for \u8bb0\u5f55\u5ba2\u6237\u7aefIP\u5730\u5740<br \/>\n$remote_user \u8bb0\u5f55\u5ba2\u6237\u7aef\u7528\u6237\u540d\u79f0<br \/>\n$request \u8bb0\u5f55\u8bf7\u6c42\u7684URL\u548cHTTP\u534f\u8bae<br \/>\n$status \u8bb0\u5f55\u8bf7\u6c42\u72b6\u6001<br \/>\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\u201c%B\u201d\u53c2\u6570\u517c\u5bb9\u3002<br \/>\n$bytes_sent \u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u603b\u5b57\u8282\u6570\u3002<br \/>\n$connection \u8fde\u63a5\u7684\u5e8f\u5217\u53f7\u3002<br \/>\n$connection_requests \u5f53\u524d\u901a\u8fc7\u4e00\u4e2a\u8fde\u63a5\u83b7\u5f97\u7684\u8bf7\u6c42\u6570\u91cf\u3002<br \/>\n$msec \u65e5\u5fd7\u5199\u5165\u65f6\u95f4\u3002\u5355\u4f4d\u4e3a\u79d2\uff0c\u7cbe\u5ea6\u662f\u6beb\u79d2\u3002<br \/>\n$pipe \u5982\u679c\u8bf7\u6c42\u662f\u901a\u8fc7HTTP\u6d41\u6c34\u7ebf(pipelined)\u53d1\u9001\uff0cpipe\u503c\u4e3a\u201cp\u201d\uff0c\u5426\u5219\u4e3a\u201c.\u201d\u3002<br \/>\n$http_referer \u8bb0\u5f55\u4ece\u54ea\u4e2a\u9875\u9762\u94fe\u63a5\u8bbf\u95ee\u8fc7\u6765\u7684<br \/>\n$http_user_agent \u8bb0\u5f55\u5ba2\u6237\u7aef\u6d4f\u89c8\u5668\u76f8\u5173\u4fe1\u606f<br \/>\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<br \/>\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<br \/>\n$time_iso8601 ISO8601\u6807\u51c6\u683c\u5f0f\u4e0b\u7684\u672c\u5730\u65f6\u95f4\u3002<br \/>\n$time_local \u901a\u7528\u65e5\u5fd7\u683c\u5f0f\u4e0b\u7684\u672c\u5730\u65f6\u95f4\u3002<\/div>\n<p>\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef\u7684\u54cd\u5e94\u5934\u62e5\u6709\u201csent_http_\u201d\u524d\u7f00\u3002 \u6bd4\u5982$sent_http_content_range\u3002<\/p>\n<p>\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<div id=\"code91008\" class=\"codebody\">http {<br \/>\nlog_format\u00a0 main\u00a0 &#8216;$remote_addr &#8211; $remote_user [$time_local] &#8220;$request&#8221; &#8216;<br \/>\n&#8216;&#8221;$status&#8221; $body_bytes_sent &#8220;$http_referer&#8221; &#8216;<br \/>\n&#8216;&#8221;$http_user_agent&#8221; &#8220;$http_x_forwarded_for&#8221; &#8216;<br \/>\n&#8216;&#8221;$gzip_ratio&#8221; $request_time $bytes_sent $request_length&#8217;;\u00a0log_format srcache_log &#8216;$remote_addr &#8211; $remote_user [$time_local] &#8220;$request&#8221; &#8216;<br \/>\n&#8216;&#8221;$status&#8221; $body_bytes_sent $request_time $bytes_sent $request_length &#8216;<br \/>\n&#8216;[$upstream_response_time] [$srcache_fetch_status] [$srcache_store_status] [$srcache_expire]&#8217;;<\/p>\n<p>open_log_file_cache max=1000 inactive=60s;<\/p>\n<p>server {<br \/>\nserver_name ~^(www\\.)?(.+)$;<br \/>\naccess_log logs\/$2-access.log main;<br \/>\nerror_log logs\/$2-error.log;<\/p>\n<p>location \/srcache {<br \/>\naccess_log logs\/access-srcache.log srcache_log;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<\/div>\n<h2><\/h2>\n<ul>\n<li>open_log_file_cache\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];<br \/>\nopen_log_file_cache off;<br \/>\n\u9ed8\u8ba4\u503c: open_log_file_cache off;<br \/>\n\u914d\u7f6e\u6bb5: http, server, location<\/p>\n<p>\u5bf9\u4e8e\u6bcf\u4e00\u6761\u65e5\u5fd7\u8bb0\u5f55\uff0c\u90fd\u5c06\u662f\u5148\u6253\u5f00\u6587\u4ef6\uff0c\u518d\u5199\u5165\u65e5\u5fd7\uff0c\u7136\u540e\u5173\u95ed\u3002\u53ef\u4ee5\u4f7f\u7528open_log_file_cache\u6765\u8bbe\u7f6e\u65e5\u5fd7\u6587\u4ef6\u7f13\u5b58(\u9ed8\u8ba4\u662foff)\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>\u53c2\u6570\u6ce8\u91ca\u5982\u4e0b\uff1a<br \/>\nmax:\u8bbe\u7f6e\u7f13\u5b58\u4e2d\u7684\u6700\u5927\u6587\u4ef6\u63cf\u8ff0\u7b26\u6570\u91cf\uff0c\u5982\u679c\u7f13\u5b58\u88ab\u5360\u6ee1\uff0c\u91c7\u7528LRU\u7b97\u6cd5\u5c06\u63cf\u8ff0\u7b26\u5173\u95ed\u3002<br \/>\ninactive:\u8bbe\u7f6e\u5b58\u6d3b\u65f6\u95f4\uff0c\u9ed8\u8ba4\u662f10s<br \/>\nmin_uses:\u8bbe\u7f6e\u5728inactive\u65f6\u95f4\u6bb5\u5185\uff0c\u65e5\u5fd7\u6587\u4ef6\u6700\u5c11\u4f7f\u7528\u591a\u5c11\u6b21\u540e\uff0c\u8be5\u65e5\u5fd7\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bb0\u5165\u7f13\u5b58\u4e2d\uff0c\u9ed8\u8ba4\u662f1\u6b21<br \/>\nvalid:\u8bbe\u7f6e\u68c0\u67e5\u9891\u7387\uff0c\u9ed8\u8ba460s<br \/>\noff\uff1a\u7981\u7528\u7f13\u5b58<br \/>\n\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<div id=\"code73254\" class=\"codebody\">open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;<\/div>\n<h2><\/h2>\n<ul>\n<li>log_not_found\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: log_not_found on | off;<br \/>\n\u9ed8\u8ba4\u503c: log_not_found on;<br \/>\n\u914d\u7f6e\u6bb5: http, server, location<br \/>\n\u662f\u5426\u5728error_log\u4e2d\u8bb0\u5f55\u4e0d\u5b58\u5728\u7684\u9519\u8bef\u3002\u9ed8\u8ba4\u662f\u3002<\/p>\n<ul>\n<li>log_subrequest\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: log_subrequest on | off;<br \/>\n\u9ed8\u8ba4\u503c: log_subrequest off;<br \/>\n\u914d\u7f6e\u6bb5: http, server, location<br \/>\n\u662f\u5426\u5728access_log\u4e2d\u8bb0\u5f55\u5b50\u8bf7\u6c42\u7684\u8bbf\u95ee\u65e5\u5fd7\u3002\u9ed8\u8ba4\u4e0d\u8bb0\u5f55\u3002<\/p>\n<h2><\/h2>\n<ul>\n<li>rewrite_log\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u7531ngx_http_rewrite_module\u6a21\u5757\u63d0\u4f9b\u7684\u3002\u7528\u6765\u8bb0\u5f55\u91cd\u5199\u65e5\u5fd7\u7684\u3002\u5bf9\u4e8e\u8c03\u8bd5\u91cd\u5199\u89c4\u5219\u5efa\u8bae\u5f00\u542f\u3002 Nginx\u91cd\u5199\u89c4\u5219\u6307\u5357<br \/>\n\u8bed\u6cd5: rewrite_log on | off;<br \/>\n\u9ed8\u8ba4\u503c: rewrite_log off;<br \/>\n\u914d\u7f6e\u6bb5: http, server, location, if<br \/>\n\u542f\u7528\u65f6\u5c06\u5728error log\u4e2d\u8bb0\u5f55notice\u7ea7\u522b\u7684\u91cd\u5199\u65e5\u5fd7\u3002<\/p>\n<h2><\/h2>\n<ul>\n<li>error_log\u6307\u4ee4<\/li>\n<\/ul>\n<p>\u8bed\u6cd5: error_log file | stderr | syslog:server=address[,parameter=value] [debug | info | notice | warn | error | crit | alert | emerg];<br \/>\n\u9ed8\u8ba4\u503c: error_log logs\/error.log error;<br \/>\n\u914d\u7f6e\u6bb5: main, http, server, location<br \/>\n\u914d\u7f6e\u9519\u8bef\u65e5\u5fd7\u3002<\/p>\n<p><audio style=\"display: none;\" controls=\"controls\"><\/audio><\/p>\n","protected":false},"excerpt":{"rendered":"<p>nginx\u6709\u4e00\u4e2a\u975e\u5e38\u7075\u6d3b\u7684\u65e5\u5fd7\u8bb0\u5f55\u6a21\u5f0f\u3002\u6bcf\u4e2a\u7ea7\u522b\u7684\u914d\u7f6e\u53ef\u4ee5\u6709\u5404\u81ea\u72ec\u7acb\u7684\u8bbf\u95ee\u65e5\u5fd7\u3002\u65e5\u5fd7\u683c\u5f0f\u901a\u8fc7log_forma [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-3695","post","type-post","status-publish","format-standard","hentry","category-day"],"_links":{"self":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3695"}],"version-history":[{"count":1,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3695\/revisions"}],"predecessor-version":[{"id":3696,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/3695\/revisions\/3696"}],"wp:attachment":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3695"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}