{"id":1384,"date":"2025-07-31T17:30:02","date_gmt":"2025-07-31T09:30:02","guid":{"rendered":"https:\/\/www.hgfwq.com\/blog\/?p=1384"},"modified":"2025-07-31T17:30:02","modified_gmt":"2025-07-31T09:30:02","slug":"nginx%e6%97%a5%e5%bf%97%e5%ae%9a%e6%9c%9f%e6%b8%85%e7%90%86%e5%a6%82%e4%bd%95%e6%9c%89%e6%95%88%e7%ae%a1%e7%90%86%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%80%a7%e8%83%bd%e4%b8%8e%e7%a3%81%e7%9b%98%e7%a9%ba","status":"publish","type":"post","link":"https:\/\/www.hgfwq.com\/blog\/1384.html","title":{"rendered":"Nginx\u65e5\u5fd7\u5b9a\u671f\u6e05\u7406\u5982\u4f55\u6709\u6548\u7ba1\u7406\u670d\u52a1\u5668\u6027\u80fd\u4e0e\u78c1\u76d8\u7a7a\u95f4\uff1f"},"content":{"rendered":"<p><h>\u5f15\u8a00<\/h><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.hgfwq.com\/blog\/wp-content\/uploads\/2025\/07\/O6iz3.jpg\" alt=\"Nginx\u65e5\u5fd7\u5b9a\u671f\u6e05\u7406\u5982\u4f55\u6709\u6548\u7ba1\u7406\u670d\u52a1\u5668\u6027\u80fd\u4e0e\u78c1\u76d8\u7a7a\u95f4\uff1f\" title=\"Nginx\u65e5\u5fd7\u5b9a\u671f\u6e05\u7406\u5982\u4f55\u6709\u6548\u7ba1\u7406\u670d\u52a1\u5668\u6027\u80fd\u4e0e\u78c1\u76d8\u7a7a\u95f4\uff1f\"><\/p>\n<p><p>\u5728\u670d\u52a1\u5668\u6216VPS\u7ba1\u7406\u4e2d\uff0c\u65e5\u5fd7\u6587\u4ef6\u7684\u5b9a\u671f\u6e05\u7406\u662f\u4e00\u9879\u91cd\u8981\u7684\u7ef4\u62a4\u5de5\u4f5c\u3002Nginx\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u5176\u65e5\u5fd7\u6587\u4ef6\u4f1a\u968f\u7740\u65f6\u95f4\u79ef\u7d2f\u5927\u91cf\u6570\u636e\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u6e05\u7406\uff0c\u53ef\u80fd\u4f1a\u5360\u7528\u8fc7\u591a\u78c1\u76d8\u7a7a\u95f4\uff0c\u5f71\u54cd\u670d\u52a1\u5668\u6027\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9a\u671f\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u6307\u5357\u3002<\/p>\n<\/p>\n<p><h>\u4e00\u3001\u4e86\u89e3Nginx\u65e5\u5fd7\u6587\u4ef6<\/h><\/p>\n<p><p>Nginx\u65e5\u5fd7\u6587\u4ef6\u4e3b\u8981\u5305\u62ec\u56db\u79cd\u7c7b\u578b\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>access.log\uff1a\u8bb0\u5f55\u5ba2\u6237\u7aef\u8bf7\u6c42\u7684\u5904\u7406\u60c5\u51b5\u3002<\/p>\n<\/li>\n<li>\n<p><p>error.log\uff1a\u8bb0\u5f55\u670d\u52a1\u5668\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><p>slowlog.log\uff1a\u8bb0\u5f55\u6267\u884c\u65f6\u95f4\u8d85\u8fc7\u6307\u5b9a\u9608\u503c\u7684\u8bf7\u6c42\u3002<\/p>\n<\/li>\n<li>\n<p><p>other custom logs\uff1a\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/li>\n<\/ul>\n<p><h>\u4e8c\u3001\u624b\u52a8\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6<\/h><\/p>\n<p><p>\u624b\u52a8\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u767b\u5f55\u5230\u670d\u52a1\u5668\u6216VPS\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u8fdb\u5165Nginx\u65e5\u5fd7\u6587\u4ef6\u6240\u5728\u7684\u76ee\u5f55\uff0c\u901a\u5e38\u4f4d\u4e8e\/srv\/nginx\/logs\/\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u4f7f\u7528`cat \/dev\/null > access.log`\u547d\u4ee4\u6e05\u7a7aaccess.log\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u91cd\u590d\u6b65\u9aa43\uff0c\u6e05\u7a7a\u5176\u4ed6\u9700\u8981\u6e05\u7406\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5982\u679c\u9700\u8981\u4fdd\u7559\u5386\u53f2\u65e5\u5fd7\uff0c\u53ef\u4ee5\u5c06\u5f53\u524d\u65e5\u5fd7\u6587\u4ef6\u91cd\u547d\u540d\u6216\u79fb\u52a8\u5230\u5176\u4ed6\u76ee\u5f55\u3002<\/p>\n<\/li>\n<\/ol>\n<p><h>\u4e09\u3001\u4f7f\u7528cron\u4efb\u52a1\u81ea\u52a8\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6<\/h><\/p>\n<p><p>\u4e3a\u4e86\u81ea\u52a8\u5316\u65e5\u5fd7\u6e05\u7406\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u4f7f\u7528cron\u4efb\u52a1\u5b9a\u671f\u6267\u884c\u6e05\u7406\u811a\u672c\u3002\u4ee5\u4e0b\u662f\u521b\u5efa\u548c\u914d\u7f6ecron\u4efb\u52a1\u7684\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><p>\u7f16\u5199\u4e00\u4e2ashell\u811a\u672c\uff0c\u7528\u4e8e\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u811a\u672c\uff1a<\/p>\n<\/p>\n<p>    &#8220;`bash<\/p>\n<p>    !\/bin\/bash<\/p>\n<p>    LOG_DIR=&#8221;\/srv\/nginx\/logs&#8221;<\/p>\n<p>   mv $LOG_DIR\/access.log $LOG_DIR\/access_$(date +%Y%m%d%H%M%S).log<\/p>\n<p>   mv $LOG_DIR\/error.log $LOG_DIR\/error_$(date +%Y%m%d%H%M%S).log<\/p>\n<p>     \u6e05\u7a7a\u65e5\u5fd7\u6587\u4ef6<\/p>\n<p>    cat \/dev\/null > $LOG_DIR\/access.log<\/p>\n<p>    cat \/dev\/null > $LOG_DIR\/error.log<\/p>\n<p>    &#8220;`<\/p>\n<\/li>\n<li>\n<p><p>\u4fdd\u5b58\u811a\u672c\u5230\u670d\u52a1\u5668\u4e0a\u7684\u67d0\u4e2a\u76ee\u5f55\uff0c\u4f8b\u5982\/home\/user\/nginx-log-clean.sh\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u7ed9\u811a\u672c\u6dfb\u52a0\u6267\u884c\u6743\u9650\uff0c\u4f7f\u7528\u547d\u4ee4`chmod +x \/home\/user\/nginx-log-clean.sh`\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u7f16\u8f91crontab\u6587\u4ef6\uff0c\u6dfb\u52a0\u4e00\u6761\u5b9a\u65f6\u4efb\u52a1\u3002\u4f7f\u7528\u547d\u4ee4`crontab -e`\u7f16\u8f91crontab\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u884c\uff1a<\/p>\n<\/p>\n<p>    &#8220;`bash<\/p>\n<p>    0 0    \/home\/user\/nginx-log-clean.sh<\/p>\n<p>    &#8220;`<\/p>\n<p>    \u8fd9\u6761\u547d\u4ee4\u8868\u793a\u6bcf\u5929\u51cc\u66680\u70b9\u6267\u884c\u811a\u672c\u3002<\/li>\n<\/p>\n<\/ol>\n<p><h>\u56db\u3001\u6ce8\u610f\u4e8b\u9879<\/h4>\n<\/p>\n<p><p>\u5728\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\n<p><p>\u786e\u4fdd\u6709\u8db3\u591f\u7684\u78c1\u76d8\u7a7a\u95f4\u5b58\u50a8\u5386\u53f2\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u5728\u6267\u884c\u6e05\u7406\u64cd\u4f5c\u524d\uff0c\u5907\u4efd\u91cd\u8981\u7684\u65e5\u5fd7\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p><p>\u6839\u636e\u670d\u52a1\u5668\u8d1f\u8f7d\u548c\u65e5\u5fd7\u6587\u4ef6\u5927\u5c0f\uff0c\u8c03\u6574cron\u4efb\u52a1\u7684\u6267\u884c\u9891\u7387\u3002<\/p>\n<\/li>\n<\/ul>\n<p><h>\u4e94\u3001\u95ee\u7b54\u73af\u8282<\/h4>\n<\/p>\n<p><b>\u95ee\uff1a\u4e3a\u4ec0\u4e48\u9700\u8981\u5b9a\u671f\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\uff1f<\/b><\/p>\n<p><p>\u7b54\uff1a\u5b9a\u671f\u6e05\u7406Nginx\u65e5\u5fd7\u6587\u4ef6\u53ef\u4ee5\u907f\u514d\u65e5\u5fd7\u6587\u4ef6\u5360\u7528\u8fc7\u591a\u78c1\u76d8\u7a7a\u95f4\uff0c\u63d0\u9ad8\u670d\u52a1\u5668\u6027\u80fd\uff0c\u540c\u65f6\u4fbf\u4e8e\u5206\u6790\u548c\u7ef4\u62a4\u3002<\/p>\n<\/p>\n<p><b>\u95ee\uff1a\u5982\u4f55\u68c0\u67e5Nginx\u65e5\u5fd7\u6587\u4ef6\u7684\u5927\u5c0f\uff1f<\/b><\/p>\n<p><p>\u7b54\uff1a\u53ef\u4ee5\u4f7f\u7528`du -sh \/path\/to\/logfile`\u547d\u4ee4\u68c0\u67e5Nginx\u65e5\u5fd7\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n<\/p>\n<p><b>\u95ee\uff1a\u5982\u4f55\u67e5\u770bNginx\u65e5\u5fd7\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/b><\/p>\n<p><p>\u7b54\uff1a\u53ef\u4ee5\u4f7f\u7528`cat \/path\/to\/logfile`\u6216`less \/path\/to\/logfile`\u547d\u4ee4\u67e5\u770bNginx\u65e5\u5fd7\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f15\u8a00 \u5728\u670d\u52a1\u5668\u6216VPS\u7ba1\u7406\u4e2d\uff0c\u65e5\u5fd7\u6587\u4ef6\u7684\u5b9a\u671f\u6e05\u7406\u662f\u4e00\u9879\u91cd\u8981\u7684\u7ef4\u62a4\u5de5\u4f5c\u3002Nginx\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684Web\u670d\u52a1\u5668\uff0c\u5176\u65e5\u5fd7\u6587\u4ef6\u4f1a\u968f\u7740\u65f6\u95f4\u79ef\u7d2f\u5927\u91cf\u6570\u636e\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u6e05\u7406\uff0c\u53ef\u80fd\u4f1a\u5360\u7528\u8fc7\u591a\u78c1\u76d8\u7a7a\u95f4\uff0c\u5f71\u54cd\u670d\u52a1\u5668\u6027\u80fd\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9a\u671f\u6e05\u7406Nginx\u65e5\u5fd7&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1385,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[163,99,8],"topic":[],"class_list":["post-1384","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-haiwai","tag-nginx","tag-99","tag-8"],"_links":{"self":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1384","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/comments?post=1384"}],"version-history":[{"count":1,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions"}],"predecessor-version":[{"id":1387,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/posts\/1384\/revisions\/1387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media\/1385"}],"wp:attachment":[{"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/media?parent=1384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/categories?post=1384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/tags?post=1384"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hgfwq.com\/blog\/wp-json\/wp\/v2\/topic?post=1384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}