{"id":578,"date":"2022-02-17T10:44:27","date_gmt":"2022-02-17T02:44:27","guid":{"rendered":"https:\/\/www.buyao007.icu\/?p=578"},"modified":"2022-02-17T10:44:27","modified_gmt":"2022-02-17T02:44:27","slug":"3-nginx%e7%9a%84%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/www.buyao007.icu\/?p=578","title":{"rendered":"3.nginx\u7684\u65e5\u5fd7"},"content":{"rendered":"\n<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6\u7fa4\u67b6\u6784\u4e0e\u81ea\u52a8\u5316\u8fd0\u7ef4\uff0c\u66fe\u8d1f\u8d23\u56fd\u5185\u67d0\u5927\u578b\u535a\u5ba2\u7f51\u7ad9\u8fd0\u7ef4\u5de5\u4f5c\u3002\u5fae\u4fe1\uff1as2795802440<\/p>\n\n\n\n<p>\u8bbf\u95ee\u65e5\u5fd7: \/var\/log\/nginx\/access.log<br>\u9519\u8bef\u65e5\u5fd7: \/var\/log\/nginx\/error.log<br>\u8bbf\u95ee\u65e5\u5fd7\u7684\u683c\u5f0f: log_format \u8bbe\u7f6e nginx\u8bbf\u95ee\u65e5\u5fd7\u7684\u683c\u5f0f<br>\u8bbe\u7f6e(\u5f00\u542f)\u8bbf\u95ee\u65e5\u5fd7 access_log<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">1.log_format\u65e5\u5fd7\u683c\u5f0f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>log_format  main  '$remote_addr - $remote_user &#91;$time_local] \"$request\" '\n                  '$status $body_bytes_sent \"$http_referer\" '\n                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n\n\n$remote_addr #\u5ba2\u6237\u7aefip\u5730\u5740\r\n$remote_user #\u8fdc\u7a0b\u7528\u6237(\u7a7a)\r\n$time_local #\u65f6\u95f4 11\/May\/2021:10:26:41\r +0800\n$request #\u7528\u6237\u8bf7\u6c42\u62a5\u6587\u7684\u8d77\u59cb\u884c \"GET\r \/index.html HTTP\/1.1\"\r\n$request_uri #\u7528\u6237\u8bf7\u6c42\u7684uri\r\n$status #\u72b6\u6001\u7801\r\n$body_bytes_sent #http\u54cd\u5e94\u62a5\u6587\u7684\u4e3b\u4f53\u5927\u5c0f(\u6587\u4ef6\u5927\u5c0f) \u5b57\r\u8282 (\u670d\u52a1\u5668\u7ed9\u4f60\u53d1\u9001\u4e861\u4e2a\u591a\u5927\u7684\u6587\u4ef6)\r\n$http_referer #\u4ece\u54ea\u91cc\u8df3\u8f6c\u5230\u4f60\u7684\u7f51\u7ad9 (\u4ece\u54ea\u91cc\u8df3\u8f6c)\r\u5206\u6790\u7528\u6237\u7684\u6765\u6e90,\u7cbe\u786e\u6295\u653e\u5e7f\u544a(sem)\r\n$http_user_agent #\u7528\u6237\u7684\u4ee3\u7406(\u6d4f\u89c8\u5668)\r\n$http_x_forwarded_for #\u8bb0\u5f55\u7528\u6237\u771f\u5b9eip\u5730\u5740<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">2.access_log\u8bbf\u95ee\u65e5\u5fd7\u8bbe\u7f6e<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Syntax:<\/th><td><code><strong>access_log<\/strong>&nbsp;<em>path<\/em>&nbsp;[<em>format<\/em>&nbsp;[buffer=<em>size<\/em>] [gzip[=<em>level<\/em>]] [flush=<em>time<\/em>] [if=<em>condition<\/em>]];<\/code><br><code><strong>access_log<\/strong>&nbsp;off;<\/code><\/td><\/tr><tr><th>Default:<\/th><td>access_log logs\/access.log combined;<\/td><\/tr><tr><th>Context:<\/th><td><code>http<\/code>,&nbsp;<code>server<\/code>,&nbsp;<code>location<\/code>,&nbsp;<code>if in location<\/code>,&nbsp;<code>limit_except<\/code><\/td><\/tr><\/tbody><\/table><figcaption>path\u662f\u8def\u5f84<br>format \u65e5\u5fd7\u683c\u5f0f<br>gzip\u662f\u5426\u538b\u7f29 \u6ce8\u538b\u7f29\u540e\u65e5\u5fd7\u6700\u597d\u547d\u540d\u4e3aaccess.log.gz<br>fush \u5b9a\u65f6\u66f4\u65b0<\/figcaption><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5e94\u7528\u6848\u4f8b: \u6307\u5b9a\u4f4d\u7f6e\u53ca\u683c\u5f0f\r\naccess_log \/var\/log\/nginx\/access.log main;\r\n#\u5e94\u7528\u6848\u4f8b: \u538b\u7f29 \u65e5\u5fd7\u5148\u5199\u5165\u5230\u7f13\u5b58 \u6bcf\u96943s\u5199\u5165\u5230\u78c1\u76d8\r\n&#91;root@web01 \/etc\/nginx\/conf.d]# cat www.sfy.conf\r\nserver {\r\nlisten 80;\r\nserver_name www.sfy.com;\r\naccess_log \/var\/log\/nginx\/www_access.log.gz main\r gzip buffer=128 flush=3;\r\nlocation \/ {\r\nroot \/code\/www;\r\nindex index.html;\r\n}\r\n}\r<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">3.\u9519\u8bef\u65e5\u5fd7\u683c\u5f0f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Syntax:<\/th><td><code><strong>error_log file [level];<\/strong><\/code><\/td><\/tr><tr><th>Default:<\/th><td>error_log logs\/error.log error;<\/td><\/tr><tr><th>Context:<\/th><td><code>main , http , mail , stream , server , location<\/code><\/td><\/tr><\/tbody><\/table><figcaption>level #\u65e5\u5fd7\u683c\u5f0f<br>debug #\u6700\u8be6\u7ec6<br>notice #<br>error #<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc_0\">4.nginx\u65e5\u5fd7\u5207\u5272<\/h2>\n\n\n\n<p>\u65e5\u5fd7\u5207\u5272: \u5b9a\u671f\u628a\u5207\u5272\u6210\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6(\u52a0\u4e0a\u65e5\u671f). \u907f\u514d\u65e5\u5fd7\u8fc7\u5927<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#\u5982\u4f55\u5b9e\u73b0:\r\n##logrotate \u547d\u4ee4 + \/etc\/logroate.d\/\u914d\u7f6e\u6587\u4ef6\r\nlogroate -f \/etc\/logroate.d\/nginx\r\n&#91;root@web01 \/etc\/nginx\/conf.d]# cat\r \/etc\/logrotate.d\/nginx\r\n\/var\/log\/nginx\/*.log { #\u6307\u5b9a\u4f60\u8981\u5207\u5272\u7684\u6587\u4ef6\r\ndaily #\u6bcf\u5929\r\nmissingok #\u5982\u679c\u5bf9\u5e94\u65e5\u5fd7\u4e0d\u5b58\u5728,\u8df3\u8fc7,\u4e0d\r\u663e\u793a\u9519\u8bef\u4fe1\u606f\r\nrotate 52 #\u6700\u591a\u4fdd\u7559\u591a\u5c11\u4e2a\u5207\u5272\u540e\u7684\u65e5\r\u5fd7.\r\ncompress #\u65e5\u5fd7\u662f\u5426\u538b\u7f29 gzip\r\ndelaycompress #\u5ef6\u8fdf\u4e00\u4e2a\u5468\u671f,\u7136\u540e\u5728\u8fdb\u884c\u538b\r\u7f29\r\nnotifempty #not if empty \u5982\u679c\u65e5\u5fd7\u662f\r\u7a7a\u7684\u8df3\u8fc7.\r\ncreate 640 nginx adm #\u65e5\u5fd7\u6743\u9650,\u6240\u6709\u8005\r\nsharedscripts #\npostrotate #\u5728\u65e5\u5fd7\u8f6e\u8be2(\u5207\u5272)\u4e4b\u540e,\u6267\u884c\r\u91cc\u9762\u7684\u547d\u4ee4\r\nif &#91; -f \/var\/run\/nginx.pid ]; then\r\nkill -USR1 `cat\r\n\/var\/run\/nginx.pid` #systemctl reload nginx\r\nfi\r\nendscript\r\n}\r\n#nginx \u5b9a\u671f\u5207\u5272\u539f\u7406\r\n&#91;root@web01 \/var\/log\/nginx]# ll \/etc\/cron.daily\/\r\ntotal 8\r\n-rwx------. 1 root root 219 Apr 1 2020 logrotate<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b59\u5bcc\u9633\uff0c \u6c5f\u6e56\u4eba\u79f0\u6ca1\u4eba\u79f0\u3002\u591a\u5e74\u4e92\u8054\u7f51\u8fd0\u7ef4\u5de5\u4f5c\u7ecf\u9a8c\uff0c\u66fe\u8d1f\u8d23\u8fc7\u5b59\u5e03\u65af\u5927\u89c4\u6a21\u96c6\u7fa4\u67b6\u6784\u81ea\u52a8\u5316\u8fd0\u7ef4\u7ba1\u7406\u5de5\u4f5c\u3002\u64c5\u957fWeb\u96c6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/578"}],"collection":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=578"}],"version-history":[{"count":1,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions"}],"predecessor-version":[{"id":579,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/posts\/578\/revisions\/579"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.buyao007.icu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}