{"id":18036,"date":"2024-11-18T09:18:22","date_gmt":"2024-11-18T01:18:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/18036\/"},"modified":"2024-11-18T09:18:22","modified_gmt":"2024-11-18T01:18:22","slug":"nginx%e6%80%8e%e4%b9%88%e9%85%8d%e7%bd%ae%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/18036\/","title":{"rendered":"nginx\u600e\u4e48\u914d\u7f6e\u65e5\u5fd7"},"content":{"rendered":"<blockquote><p>\n  \u5728 nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7\uff0c\u81f3\u5173\u91cd\u8981\u7684\u662f\u4e3a\u4e86\u8ddf\u8e2a\u670d\u52a1\u5668\u6027\u80fd\u3001\u8bc6\u522b\u95ee\u9898\u548c\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a\u5728 nginx.conf \u4e2d\uff0c\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u8def\u5f84\u3002\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\uff0c\u4f8b\u5982\u5e38\u7528\u7684 main \u683c\u5f0f\u3002\u6307\u5b9a\u8bbf\u95ee\u548c\u9519\u8bef\u65e5\u5fd7\u7684\u8def\u5f84\u3002\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u5982 info \u4ee5\u8bb0\u5f55\u6240\u6709\u6d88\u606f\u3002\u91cd\u65b0\u542f\u52a8 nginx \u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002\u9a8c\u8bc1 access.log \u548c error.log \u6587\u4ef6\u4e2d\u662f\u5426\u6709\u65e5\u5fd7\u5185\u5bb9\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/27\/2024052719422416789.jpg\" class=\"aligncenter\" title=\"nginx\u600e\u4e48\u914d\u7f6e\u65e5\u5fd7\u63d2\u56fe\" alt=\"nginx\u600e\u4e48\u914d\u7f6e\u65e5\u5fd7\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7<\/strong><\/p>\n<p>\u5728 Nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u8ddf\u8e2a\u670d\u52a1\u5668\u6027\u80fd\u3001\u8bc6\u522b\u95ee\u9898\u5e76\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u672c\u6587\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u4e3a Nginx \u914d\u7f6e\u65e5\u5fd7\u7684\u6b65\u9aa4\u3002<\/p>\n<p><strong>1. \u914d\u7f6e Nginx.conf<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u5728 Nginx \u914d\u7f6e\u6587\u4ef6 \/etc\/\/nginx.conf \u6216 \/usr\/local\/etc\/nginx\/nginx.conf \u4e2d\u627e\u5230\u4ee5\u4e0b\u90e8\u5206\uff1a<\/p>\n<pre>http {\n    # \u5176\u4ed6\u914d\u7f6e...\n\n    # \u65e5\u5fd7\u914d\u7f6e\u90e8\u5206\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\n    # \u8bbe\u7f6e\u65e5\u5fd7\u8def\u5f84\n    access_log  \/path\/to\/access.log  main;\n    error_log  \/path\/to\/error.log  info;\n\n    # \u5176\u4ed6\u914d\u7f6e...\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>2. \u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f<\/strong><\/p>\n<p>\u5728 log_format \u6307\u4ee4\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6307\u5b9a\u65e5\u5fd7\u6761\u76ee\u7684\u683c\u5f0f\u3002main \u683c\u5f0f\u662f\u4e00\u4e2a\u5e38\u7528\u7684\u683c\u5f0f\uff0c\u5176\u5305\u542b\u4ee5\u4e0b\u5b57\u6bb5\uff1a<\/p>\n<ul>\n<li>$remote_addr &#8211; \u5ba2\u6237\u7aef IP \u5730\u5740<\/li>\n<li>$remote_user &#8211; \u5ba2\u6237\u7aef\u7528\u6237\u6807\u8bc6\uff08\u5982\u679c\u901a\u8fc7\u8eab\u4efd\u9a8c\u8bc1\uff09<\/li>\n<li>$time_local &#8211; \u672c\u5730\u65f6\u95f4<\/li>\n<li>$request &#8211; \u8bf7\u6c42\u884c<\/li>\n<li>$status &#8211; HTTP \u72b6\u6001\u7801<\/li>\n<li>$body_bytes_sent &#8211; \u53d1\u9001\u7684\u54cd\u5e94\u4f53\u5927\u5c0f<\/li>\n<li>$http_referer &#8211; \u5f15\u7528\u6765\u6e90 URL<\/li>\n<li>$http_user_agent &#8211; \u5ba2\u6237\u7aef\u7528\u6237\u4ee3\u7406<\/li>\n<li>$http_x_forwarded_for &#8211; \u5982\u679c\u4f7f\u7528\u4ee3\u7406\uff0c\u5219\u4e3a\u5ba2\u6237\u7aef IP \u5730\u5740<\/li>\n<\/ul>\n<p><strong>3. \u8bbe\u7f6e\u65e5\u5fd7\u8def\u5f84<\/strong><\/p>\n<p>\u4f7f\u7528 _log \u548c error_log \u6307\u4ee4\u6307\u5b9a\u8bbf\u95ee\u548c\u9519\u8bef\u65e5\u5fd7\u7684\u8def\u5f84\u3002<\/p>\n<ul>\n<li>access_log\uff1a\u8bbf\u95ee\u65e5\u5fd7\u8bb0\u5f55\u6240\u6709\u5ba2\u6237\u7aef\u8bf7\u6c42\u3002<\/li>\n<li>error_log\uff1a\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55 Nginx \u9047\u5230\u7684\u9519\u8bef\u548c\u8b66\u544a\u3002<\/li>\n<\/ul>\n<p><strong>4. \u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b<\/strong><\/p>\n<p>info \u7ea7\u522b\u8bb0\u5f55\u6240\u6709\u6d88\u606f\uff0c\u5305\u62ec\u9519\u8bef\u3001\u8b66\u544a\u548c\u4fe1\u606f\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574\u6b64\u7ea7\u522b\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>debug\uff1a\u8bb0\u5f55\u8c03\u8bd5\u4fe1\u606f\uff08\u6700\u8be6\u7ec6\u7684\u7ea7\u522b\uff09<\/li>\n<li>notice\uff1a\u8bb0\u5f55\u91cd\u8981\u4fe1\u606f<\/li>\n<li>error\uff1a\u4ec5\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f<\/li>\n<\/ul>\n<p><strong>5. \u91cd\u65b0\u542f\u52a8 Nginx<\/strong><\/p>\n<p>\u5b8c\u6210\u914d\u7f6e\u540e\uff0c\u91cd\u65b0\u542f\u52a8 Nginx \u4ee5\u4f7f\u66f4\u6539\u751f\u6548\uff1a<\/p>\n<ul>\n<li> <strong>Systemd<\/strong>\uff1asystemctl restart nginx<\/li>\n<li> <strong>Upstart<\/strong>\uff1aservice nginx restart<\/li>\n<\/ul>\n<p><strong>6. \u9a8c\u8bc1\u65e5\u5fd7\u6587\u4ef6<\/strong><\/p>\n<p>\u91cd\u65b0\u542f\u52a8 Nginx \u540e\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u5728\u6307\u5b9a\u7684\u8def\u5f84\u4e2d\u770b\u5230 access.log \u548c error.log \u6587\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7f16\u8f91\u5668\u6216\u65e5\u5fd7\u5206\u6790\u5de5\u5177\u67e5\u770b\u65e5\u5fd7\u5185\u5bb9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u914d\u7f6e\u65e5\u5fd7\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7\uff0c\u81f3\u5173\u91cd\u8981\u7684\u662f\u4e3a\u4e86\u8ddf\u8e2a\u670d\u52a1\u5668\u6027\u80fd\u3001\u8bc6\u522b\u95ee\u9898\u548c\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u4ee5\u4e0b\u662f\u6b65\u9aa4\uff1a\u5728 nginx.conf \u4e2d\uff0c\u914d\u7f6e\u65e5\u5fd7\u683c\u5f0f\u548c\u8def\u5f84\u3002\u8bbe\u7f6e\u65e5\u5fd7\u683c\u5f0f\uff0c\u4f8b\u5982\u5e38\u7528\u7684 main \u683c\u5f0f\u3002\u6307\u5b9a\u8bbf\u95ee\u548c\u9519\u8bef\u65e5\u5fd7\u7684\u8def\u5f84\u3002\u8bbe\u7f6e\u65e5\u5fd7\u7ea7\u522b\uff0c\u5982 info \u4ee5\u8bb0\u5f55\u6240\u6709\u6d88\u606f\u3002\u91cd\u65b0\u542f\u52a8 nginx \u4ee5\u4f7f\u66f4\u6539\u751f\u6548\u3002\u9a8c\u8bc1 access.log \u548c error.log \u6587\u4ef6\u4e2d\u662f\u5426\u6709\u65e5\u5fd7\u5185\u5bb9\u3002 \u5982\u4f55\u5728 Nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7 \u5728 Nginx \u4e2d\u914d\u7f6e\u65e5\u5fd7\u81f3\u5173\u91cd\u8981\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u60a8\u8ddf\u8e2a\u670d\u52a1\u5668\u6027\u80fd\u3001\u8bc6\u522b\u95ee\u9898\u5e76\u8fdb\u884c\u6545\u969c\u6392\u9664\u3002\u672c\u6587\u5c06\u5f15\u5bfc\u60a8\u5b8c\u6210\u4e3a Nginx \u914d\u7f6e\u65e5\u5fd7\u7684\u6b65\u9aa4\u3002 1. \u914d\u7f6e Nginx.conf \u9996\u5148\uff0c\u5728 Nginx \u914d\u7f6e\u6587\u4ef6 \/etc\/\/nginx.conf \u6216 \/usr\/local\/etc\/nginx\/nginx.conf \u4e2d\u627e\u5230\u4ee5\u4e0b\u90e8\u5206\uff1a http { # \u5176\u4ed6\u914d\u7f6e&#8230; # \u65e5\u5fd7\u914d\u7f6e\u90e8\u5206 log_format main &#8216;$remote_addr &#8211; $remote_user [$time_local] &#8220;$request&#8221; &#8216; &#8216;$status $body_bytes_sent &#8220;$http_referer&#8221; &#8216; &#8216;&#8221;$http_user_agent&#8221; &#8220;$http_x_forwarded_for&#8221;&#8216;; # \u8bbe\u7f6e\u65e5\u5fd7\u8def\u5f84 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-18036","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=18036"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/18036\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=18036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=18036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=18036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}