{"id":3718,"date":"2019-01-11T16:31:43","date_gmt":"2019-01-11T08:31:43","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3718"},"modified":"2019-01-11T16:36:31","modified_gmt":"2019-01-11T08:36:31","slug":"%e8%af%a6%e8%a7%a3-nginx%e6%97%a5%e5%bf%97%e5%88%86%e5%89%b2%e5%92%8c-nginx-%e6%97%a5%e5%bf%97%e5%ae%89%e5%85%a8%e5%88%86%e5%89%b2","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3718","title":{"rendered":"\u8be6\u89e3 nginx\u65e5\u5fd7\u5206\u5272\u548c nginx \u65e5\u5fd7\u5b89\u5168\u5206\u5272"},"content":{"rendered":"<p>\n\tNginx \u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u5b83\u7684\u65e5\u5fd7\u4e0d\u4f1a\u81ea\u52a8\u5206\u5272\u3002\u6240\u6709\u7684\u65e5\u5fd7\u90fd\u4f1a\u7d2f\u79ef\u8d77\u6765\uff0c\u5bfc\u81f4\u65e5\u5fd7\u6587\u4ef6\u975e\u5e38\u7684\u5927\uff0c\u4e0d\u662f\u786c\u76d8\u7a7a\u95f4\u5360\u6ee1\u5c31\u662f\u6587\u4ef6\u592a\u5927\u65e0\u6cd5\u6253\u5f00\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5bf9 Nginx \u7684\u65e5\u5fd7\u8fdb\u884c\u5207\u5272\uff0c\u5206\u5272\u6210\u6bcf\u5929\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u5e76\u4e14\u8981\u8fdb\u884c\u5b89\u5168\u7684\u65e5\u5fd7\u5206\u5272\uff0c\u4e0d\u80fd\u5f71\u54cd\u5230 Nginx \u7684\u6b63\u5e38\u8fd0\u884c\u3002\n<\/p>\n<p>\n\tnginx \u7684\u65e5\u5fd7\u5206\u5272\u53ef\u4ee5\u5206\u4e3a\u624b\u52a8\u5206\u5272\u548c\u81ea\u52a8\u5206\u5272\u3002\u6240\u8c13\u624b\u52a8\u5206\u5272\u5c31\u662f\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\uff0c\u81ea\u5df1\u6267\u884c\u547d\u4ee4\u53bb\u5907\u4efd\uff0c\u5206\u5272\u65e5\u5fd7\uff0c\u8fd9\u79cd\u7528\u6cd5\u901a\u5e38\u7528\u4e8e\u7a81\u53d1\u60c5\u51b5\u3002\u81ea\u52a8\u5206\u5272 Nginx \u65e5\u5fd7\uff0c\u5c31\u662f\u501f\u52a9 crontab \u5b9a\u65f6\u8ba4\u4e3a\uff0c\u901a\u8fc7\u81ea\u52a8\u6267\u884c shell \u811a\u672c\u53bb\u5206\u5272 Nginx \u65e5\u5fd7\u3002\n<\/p>\n<p>\n\t\u9996\u5148\uff0c\u6211\u4eec\u901a\u8fc7 mv \u547d\u4ee4\uff0c\u628a\u65e5\u5fd7\u6587\u4ef6\u79fb\u52a8\u5230\u5176\u4ed6\u4f4d\u7f6e\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nmv access.log access_bak_20190111.log\r\nmv error.log error_bak_20190111.log<\/pre>\n<p>\n\t\u5907\u4efd\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 nginx -s reopen \u547d\u4ee4\u91cd\u65b0\u6253\u5f00\u65e5\u5fd7\u6587\u4ef6\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nnginx -s reopen<\/pre>\n<p>\n\t\u6216\u8005\u6267\u884c kill -USR1 $(cat \/usr\/local\/openresty\/nginx\/logs\/nginx.pid) \u547d\u4ee4\u4e5f\u53ef\u4ee5\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nkill -USR1 nginx_pid # \u5f53\u524d nginx \u7684 pid\uff0c\u4e3b\u8fdb\u7a0b\u53f7<\/pre>\n<p>\n\t\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5728\u6ca1\u6709\u6267\u884c nginx -s reopen \u6216 kill -USR1 `cat ${pid_path}` \u4e4b\u524d\uff0c\u5373\u4fbf\u5df2\u7ecf\u5bf9\u6587\u4ef6\u6267\u884c\u4e86 mv \u547d\u4ee4\u4e5f\u53ea\u662f\u6539\u53d8\u4e86\u6587\u4ef6\u7684\u540d\u79f0\uff0cnginx \u8fd8\u662f\u4f1a\u5411\u65b0\u547d\u540d\u7684\u6587\u4ef6 access_bak_20190111.log \u4e2d\u7167\u5e38\u5199\u5165\u65e5\u5fd7\u6570\u636e\u3002\u539f\u56e0\u5728\u4e8e linux \u7cfb\u7edf\u4e2d\uff0c\u5185\u6838\u662f\u6839\u636e\u6587\u4ef6\u63cf\u8ff0\u7b26\u6765\u627e\u6587\u4ef6\u7684\u3002\n<\/p>\n<p>\n\tUSR1 \u662f\u81ea\u5b9a\u4e49\u4fe1\u53f7\uff0c\u4e5f\u5c31\u662f\u8fdb\u7a0b\u7f16\u5199\u8005\u81ea\u5df1\u786e\u5b9a\u6536\u5230\u8fd9\u4e2a\u4fe1\u53f7\u8be5\u5e72\u4ec0\u4e48\u3002\u800c\u5728 nginx \u4e2d\u5b83\u81ea\u5df1\u7f16\u5199\u4e86\u4ee3\u7801\u5f53\u63a5\u5230 USR1 \u4fe1\u53f7\u7684\u65f6\u5019\u8ba9 nginx \u91cd\u65b0\u6253\u5f00\u65e5\u5fd7\u6587\u4ef6\uff08\u91cd\u65b0\u6253\u5f00\u7684\u65e5\u5fd7\u5c31\u662f\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u7684\u4f4d\u7f6e\u548c\u540d\u79f0\uff09\u3002\n<\/p>\n<p>\n\t\u4f46\u5728\u6bcf\u6b21\u90fd\u8fd9\u6837\u64cd\u4f5c\u5c31\u663e\u5f97\u592a\u9ebb\u70e6\u4e86\uff0c\u80fd\u4e0d\u80fd\u6bcf\u5929\u751f\u6210\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\u5462\uff1f\n<\/p>\n<p>\n\t\u5f53\u7136\u53ef\u4ee5\uff0c\u6211\u4eec\u501f\u52a9 shell \u811a\u672c\u6765\u5b9e\u73b0\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\n#!\/bin\/bash\r\n#\u5206\u5272 nginx \u65e5\u5fd7\u4ee5\u9632\u6b62\u5355\u4e2a\u65e5\u5fd7\u6587\u4ef6\u5360\u7528\u592a\u591a\u78c1\u76d8\u7a7a\u95f4\r\nLOGS_PATH=\/usr\/local\/nginx\/logs\r\nYESTERDAY=$(date -d &quot;yesterday&quot; +%Y-%m-%d)\r\nmv ${LOGS_PATH}\/access.log ${LOGS_PATH}\/access_${YESTERDAY}.log\r\nmv ${LOGS_PATH}\/error.log ${LOGS_PATH}\/error_${YESTERDAY}.log\r\n## \u5411 Nginx \u4e3b\u8fdb\u7a0b\u53d1\u9001 USR1 \u4fe1\u53f7\u3002USR1 \u4fe1\u53f7\u662f\u91cd\u65b0\u6253\u5f00\u65e5\u5fd7\u6587\u4ef6\r\nkill -USR1 $(cat \/usr\/local\/nginx\/logs\/nginx.pid)<\/pre>\n<p>\n\t\u7136\u540e\u5c06\u4e0a\u9762\u8fd9\u4e2a xttblog_nginx_log.sh \u811a\u672c\u52a0\u5165\u5230 crontab \u4e2d\u3002\n<\/p>\n<pre class=\"brush:bash;\">\r\nvi \/etc\/crontab<\/pre>\n<p>\n\t\u7136\u540e\u8f93\u5165\u4e0b\u9762\u7684\u5185\u5bb9\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n0 0 * * * root \/usr\/local\/nginx\/logs\/xttblog_nginx_log.sh<\/pre>\n<p>\n\t\u7136\u540e\u4fdd\u5b58\uff0c\u8fd9\u6837\u6bcf\u5929\u5c31\u4f1a\u6709\u4e00\u4e2a root \u8eab\u4efd\u53bb\u6267\u884c\u811a\u672c \/usr\/local\/nginx\/logs\/xttblog_nginx_log.sh\uff0c\u4ece\u800c\u5b9e\u73b0\u5b9a\u65f6\u81ea\u52a8\u5206\u5272 Nginx \u65e5\u5fd7\uff08\u5305\u62ec\u8bbf\u95ee\u65e5\u5fd7\u548c\u9519\u8bef\u65e5\u5fd7\uff09\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=3718\">\u8be6\u89e3 nginx\u65e5\u5fd7\u5206\u5272\u548c nginx \u65e5\u5fd7\u5b89\u5168\u5206\u5272<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Nginx \u6709\u4e00\u4e2a\u95ee\u9898\u5c31\u662f\u5b83\u7684\u65e5\u5fd7\u4e0d\u4f1a\u81ea\u52a8\u5206\u5272\u3002\u6240\u6709\u7684\u65e5\u5fd7\u90fd\u4f1a\u7d2f\u79ef\u8d77\u6765\uff0c\u5bfc\u81f4\u65e5\u5fd7\u6587\u4ef6\u975e\u5e38\u7684\u5927\uff0c\u4e0d\u662f\u786c\u76d8\u7a7a\u95f4\u5360\u6ee1 [&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":[240,1152,313,1154,239],"class_list":["post-3718","post","type-post","status-publish","format-standard","hentry","category-yyzt","tag-log4jtomcat","tag-nginx","tag-nginx-tfs","tag-tomcat"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3718","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=3718"}],"version-history":[{"count":2,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3718\/revisions"}],"predecessor-version":[{"id":3720,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3718\/revisions\/3720"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}