{"id":51085,"date":"2024-10-08T15:58:20","date_gmt":"2024-10-08T07:58:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51085\/"},"modified":"2024-10-08T15:58:20","modified_gmt":"2024-10-08T07:58:20","slug":"%e8%b6%85%e8%a9%b3%e7%b4%b0%ef%bc%81nginx-%e6%97%a5%e8%aa%8c%e9%85%8d%e7%bd%ae%e5%af%a6%e8%b8%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51085\/","title":{"rendered":"\u8d85\u8a73\u7d30\uff01Nginx \u65e5\u8a8c\u914d\u7f6e\u5be6\u8e10"},"content":{"rendered":"<h1 id=\"%e8%b6%85%e8%a9%b3%e7%b4%b0%ef%bc%81nginx-%e6%97%a5%e8%aa%8c%e9%85%8d%e7%bd%ae%e5%af%a6%e8%b8%90-pBAfqQyuCR\">\u8d85\u8a73\u7d30\uff01Nginx \u65e5\u8a8c\u914d\u7f6e\u5be6\u8e10<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u5404\u7a2e\u7db2\u7ad9\u548c\u61c9\u7528\u7a0b\u5f0f\u4e2d\u3002\u5176\u65e5\u8a8c\u529f\u80fd\u4e0d\u50c5\u80fd\u5e6b\u52a9\u7ba1\u7406\u54e1\u76e3\u63a7\u4f3a\u670d\u5668\u7684\u904b\u884c\u72c0\u6cc1\uff0c\u9084\u80fd\u63d0\u4f9b\u6709\u50f9\u503c\u7684\u6578\u64da\u5206\u6790\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Nginx \u7684\u65e5\u8a8c\u914d\u7f6e\u5be6\u8e10\uff0c\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u9019\u4e00\u529f\u80fd\u3002<\/p>\n<h2 id=\"nginx-%e6%97%a5%e8%aa%8c%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-pBAfqQyuCR\">Nginx \u65e5\u8a8c\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>Nginx \u4e3b\u8981\u6709\u5169\u7a2e\u65e5\u8a8c\uff1a\u8a2a\u554f\u65e5\u8a8c\uff08access log\uff09\u548c\u932f\u8aa4\u65e5\u8a8c\uff08error log\uff09\u3002\u8a2a\u554f\u65e5\u8a8c\u8a18\u9304\u6240\u6709\u9032\u5165\u4f3a\u670d\u5668\u7684\u8acb\u6c42\uff0c\u800c\u932f\u8aa4\u65e5\u8a8c\u5247\u8a18\u9304\u4f3a\u670d\u5668\u5728\u8655\u7406\u8acb\u6c42\u6642\u767c\u751f\u7684\u932f\u8aa4\u3002<\/p>\n<h3 id=\"%e8%a8%aa%e5%95%8f%e6%97%a5%e8%aa%8c-pBAfqQyuCR\">\u8a2a\u554f\u65e5\u8a8c<\/h3>\n<p>\u8a2a\u554f\u65e5\u8a8c\u7684\u683c\u5f0f\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u81ea\u5b9a\u7fa9\u3002\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0cNginx \u7684\u8a2a\u554f\u65e5\u8a8c\u4f4d\u65bc \/var\/log\/nginx\/access.log\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u914d\u7f6e\u4f86\u8a2d\u7f6e\u8a2a\u554f\u65e5\u8a8c\u7684\u683c\u5f0f\uff1a<\/p>\n<pre><code>http {\n    log_format main '$remote_addr - $remote_user [$time_local] \"$request\" '\n                  '$status $body_bytes_sent \"$http_referer\" '\n                  '\"$http_user_agent\" \"$http_x_forwarded_for\"';\n    access_log \/var\/log\/nginx\/access.log main;\n}<\/code><\/pre>\n<p>\u5728\u9019\u500b\u914d\u7f6e\u4e2d\uff0clog_format \u6307\u4ee4\u5b9a\u7fa9\u4e86\u65e5\u8a8c\u7684\u683c\u5f0f\uff0caccess_log \u6307\u4ee4\u5247\u6307\u5b9a\u4e86\u65e5\u8a8c\u7684\u5b58\u5132\u4f4d\u7f6e\u3002<\/p>\n<h3 id=\"%e9%8c%af%e8%aa%a4%e6%97%a5%e8%aa%8c-pBAfqQyuCR\">\u932f\u8aa4\u65e5\u8a8c<\/h3>\n<p>\u932f\u8aa4\u65e5\u8a8c\u7684\u914d\u7f6e\u76f8\u5c0d\u7c21\u55ae\uff0c\u9ed8\u8a8d\u60c5\u6cc1\u4e0b\uff0c\u932f\u8aa4\u65e5\u8a8c\u4f4d\u65bc \/var\/log\/nginx\/error.log\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u914d\u7f6e\u4f86\u8a2d\u7f6e\u932f\u8aa4\u65e5\u8a8c\u7684\u7d1a\u5225\uff1a<\/p>\n<pre><code>error_log \/var\/log\/nginx\/error.log warn;<\/code><\/pre>\n<p>\u5728\u9019\u88e1\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u8981\u9078\u64c7\u4e0d\u540c\u7684\u65e5\u8a8c\u7d1a\u5225\uff0c\u5982 debug\u3001info\u3001notice\u3001warn\u3001error \u548c crit\u3002<\/p>\n<h2 id=\"%e6%97%a5%e8%aa%8c%e8%bc%aa%e6%9b%bf%e8%88%87%e7%ae%a1%e7%90%86-pBAfqQyuCR\">\u65e5\u8a8c\u8f2a\u66ff\u8207\u7ba1\u7406<\/h2>\n<p>\u96a8\u8457\u6642\u9593\u7684\u63a8\u79fb\uff0c\u65e5\u8a8c\u6587\u4ef6\u53ef\u80fd\u6703\u8b8a\u5f97\u975e\u5e38\u9f90\u5927\uff0c\u56e0\u6b64\u9700\u8981\u9032\u884c\u65e5\u8a8c\u8f2a\u66ff\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 logrotate \u5de5\u5177\u4f86\u81ea\u52d5\u7ba1\u7406 Nginx \u7684\u65e5\u8a8c\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 logrotate \u914d\u7f6e\u793a\u4f8b\uff1a<\/p>\n<pre><code>\/var\/log\/nginx\/*.log {\n    daily\n    missingok\n    rotate 14\n    compress\n    delaycompress\n    notifempty\n    create 0640 www-data adm\n    sharedscripts\n    postrotate\n        \/usr\/sbin\/nginx -s reopen\n    endscript\n}<\/code><\/pre>\n<p>\u9019\u6bb5\u914d\u7f6e\u5c07\u6bcf\u5929\u8f2a\u66ff Nginx \u7684\u65e5\u8a8c\uff0c\u4e26\u4fdd\u7559\u6700\u8fd1 14 \u5929\u7684\u65e5\u8a8c\u6587\u4ef6\u3002<\/p>\n<h2 id=\"%e6%97%a5%e8%aa%8c%e5%88%86%e6%9e%90%e5%b7%a5%e5%85%b7-pBAfqQyuCR\">\u65e5\u8a8c\u5206\u6790\u5de5\u5177<\/h2>\n<p>\u70ba\u4e86\u66f4\u597d\u5730\u5206\u6790 Nginx \u65e5\u8a8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5c08\u9580\u7684\u5de5\u5177\uff0c\u5982 GoAccess\u3001AWStats \u6216 ELK Stack\u3002\u9019\u4e9b\u5de5\u5177\u80fd\u5920\u5e6b\u52a9\u60a8\u53ef\u8996\u5316\u65e5\u8a8c\u6578\u64da\uff0c\u5f9e\u800c\u66f4\u597d\u5730\u7406\u89e3\u7db2\u7ad9\u7684\u6d41\u91cf\u548c\u7528\u6236\u884c\u70ba\u3002<\/p>\n<h3 id=\"goaccess-%e7%a4%ba%e4%be%8b-pBAfqQyuCR\">GoAccess \u793a\u4f8b<\/h3>\n<p>GoAccess \u662f\u4e00\u6b3e\u958b\u6e90\u7684\u5be6\u6642\u65e5\u8a8c\u5206\u6790\u5de5\u5177\uff0c\u652f\u6301\u591a\u7a2e\u65e5\u8a8c\u683c\u5f0f\u3002\u5b89\u88dd GoAccess \u5f8c\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5206\u6790 Nginx \u7684\u8a2a\u554f\u65e5\u8a8c\uff1a<\/p>\n<pre><code>goaccess \/var\/log\/nginx\/access.log --log-format=COMBINED -o report.html<\/code><\/pre>\n<p>\u9019\u5c07\u751f\u6210\u4e00\u500b HTML \u683c\u5f0f\u7684\u5831\u544a\uff0c\u65b9\u4fbf\u60a8\u67e5\u770b\u548c\u5206\u6790\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-pBAfqQyuCR\">\u7e3d\u7d50<\/h2>\n<p>Nginx \u7684\u65e5\u8a8c\u914d\u7f6e\u662f\u7db2\u7ad9\u7ba1\u7406\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u901a\u904e\u5408\u7406\u7684\u65e5\u8a8c\u683c\u5f0f\u8a2d\u7f6e\u3001\u65e5\u8a8c\u8f2a\u66ff\u7ba1\u7406\u4ee5\u53ca\u4f7f\u7528\u5206\u6790\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u6709\u6548\u5730\u76e3\u63a7\u4f3a\u670d\u5668\u7684\u904b\u884c\u72c0\u6cc1\uff0c\u4e26\u7372\u53d6\u6709\u50f9\u503c\u7684\u6578\u64da\u3002\u82e5\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684\u4f3a\u670d\u5668\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u670d\u52d9\uff0c\u8b93\u60a8\u7684\u7db2\u7ad9\u904b\u884c\u66f4\u52a0\u6d41\u66a2\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u63a2\u8a0eNginx\u65e5\u8a8c\u914d\u7f6e\u7684\u5be6\u8e10\u6280\u5de7\uff0c\u5b78\u7fd2\u5982\u4f55\u512a\u5316\u65e5\u8a8c\u8a18\u9304\u4ee5\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u5b89\u5168\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4924],"tags":[],"class_list":["post-51085","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=51085"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51085\/revisions"}],"predecessor-version":[{"id":51086,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51085\/revisions\/51086"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}