{"id":51055,"date":"2024-10-08T15:44:19","date_gmt":"2024-10-08T07:44:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51055\/"},"modified":"2024-10-08T15:44:19","modified_gmt":"2024-10-08T07:44:19","slug":"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-handler-%e6%a8%a1%e5%a1%8a%e7%9a%84%e6%8e%9b%e8%bc%89","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51055\/","title":{"rendered":"nginx \u6559\u7a0b\uff1aNginx handler \u6a21\u584a\u7684\u639b\u8f09"},"content":{"rendered":"<h1 id=\"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-handler-%e6%a8%a1%e5%a1%8a%e7%9a%84%e6%8e%9b%e8%bc%89-tErZYtxINa\">Nginx \u6559\u7a0b\uff1aNginx handler \u6a21\u584a\u7684\u639b\u8f09<\/h1>\n<p>Nginx \u662f\u4e00\u6b3e\u9ad8\u6548\u80fd\u7684\u7db2\u9801\u4f3a\u670d\u5668\uff0c\u5ee3\u6cdb\u61c9\u7528\u65bc\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u3001\u53cd\u5411\u4ee3\u7406\u3001\u8ca0\u8f09\u5747\u8861\u7b49\u591a\u7a2e\u5834\u666f\u3002\u5176\u6a21\u584a\u5316\u7684\u8a2d\u8a08\u4f7f\u5f97\u7528\u6236\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9748\u6d3b\u914d\u7f6e\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e Nginx \u7684 handler \u6a21\u584a\u7684\u639b\u8f09\uff0c\u5e6b\u52a9\u7528\u6236\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528 Nginx\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-nginx-handler-%e6%a8%a1%e5%a1%8a%ef%bc%9f-tErZYtxINa\">\u4ec0\u9ebc\u662f Nginx handler \u6a21\u584a\uff1f<\/h2>\n<p>Nginx handler \u6a21\u584a\u662f\u7528\u4f86\u8655\u7406\u8acb\u6c42\u7684\u6838\u5fc3\u7d44\u4ef6\u3002\u5b83\u8ca0\u8cac\u6839\u64da\u8acb\u6c42\u7684 URI \u548c\u5176\u4ed6\u53c3\u6578\uff0c\u9078\u64c7\u5408\u9069\u7684\u8655\u7406\u65b9\u5f0f\u3002\u9019\u4e9b\u8655\u7406\u65b9\u5f0f\u53ef\u4ee5\u662f\u975c\u614b\u6587\u4ef6\u7684\u63d0\u4f9b\u3001\u52d5\u614b\u8acb\u6c42\u7684\u8f49\u767c\uff0c\u6216\u662f\u5176\u4ed6\u81ea\u5b9a\u7fa9\u7684\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<h2 id=\"handler-%e6%a8%a1%e5%a1%8a%e7%9a%84%e9%a1%9e%e5%9e%8b-tErZYtxINa\">Handler \u6a21\u584a\u7684\u985e\u578b<\/h2>\n<p>Nginx \u63d0\u4f9b\u4e86\u591a\u7a2e handler \u6a21\u584a\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5e38\u898b\u7684\u985e\u578b\uff1a<\/p>\n<ul>\n<li><strong>static handler<\/strong>\uff1a\u7528\u65bc\u63d0\u4f9b\u975c\u614b\u6587\u4ef6\uff0c\u5982 HTML\u3001CSS\u3001JavaScript \u7b49\u3002<\/li>\n<li><strong>proxy handler<\/strong>\uff1a\u7528\u65bc\u5c07\u8acb\u6c42\u8f49\u767c\u5230\u5176\u4ed6\u4f3a\u670d\u5668\uff0c\u5e38\u7528\u65bc\u53cd\u5411\u4ee3\u7406\u3002<\/li>\n<li><strong>fastcgi handler<\/strong>\uff1a\u7528\u65bc\u8655\u7406 PHP \u7b49\u52d5\u614b\u8acb\u6c42\uff0c\u901a\u904e FastCGI \u5354\u8b70\u8207\u5f8c\u7aef\u61c9\u7528\u4f3a\u670d\u5668\u901a\u4fe1\u3002<\/li>\n<li><strong>uwsgi handler<\/strong>\uff1a\u5c08\u9580\u7528\u65bc\u8655\u7406 Python \u61c9\u7528\u7684\u8acb\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e6%8e%9b%e8%bc%89-nginx-handler-%e6%a8%a1%e5%a1%8a-tErZYtxINa\">\u5982\u4f55\u639b\u8f09 Nginx handler \u6a21\u584a<\/h2>\n<p>\u639b\u8f09 handler \u6a21\u584a\u7684\u904e\u7a0b\u4e3b\u8981\u6d89\u53ca\u5230 Nginx \u914d\u7f6e\u6587\u4ef6\u7684\u7de8\u8f2f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u57fa\u672c\u7684\u6b65\u9a5f\u548c\u793a\u4f8b\uff1a<\/p>\n<h3 id=\"1-%e7%b7%a8%e8%bc%af-nginx-%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6-tErZYtxINa\">1. \u7de8\u8f2f Nginx \u914d\u7f6e\u6587\u4ef6<\/h3>\n<p>Nginx \u7684\u4e3b\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/etc\/nginx\/nginx.conf\u3002\u7528\u6236\u53ef\u4ee5\u4f7f\u7528\u6587\u672c\u7de8\u8f2f\u5668\uff08\u5982 vim \u6216 nano\uff09\u6253\u958b\u8a72\u6587\u4ef6\u9032\u884c\u7de8\u8f2f\u3002<\/p>\n<pre><code>sudo nano \/etc\/nginx\/nginx.conf<\/code><\/pre>\n<h3 id=\"2-%e5%ae%9a%e7%be%a9-server-%e5%8d%80%e5%a1%8a-tErZYtxINa\">2. \u5b9a\u7fa9 server \u5340\u584a<\/h3>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b server \u5340\u584a\uff0c\u9019\u662f Nginx \u8655\u7406\u8acb\u6c42\u7684\u57fa\u672c\u55ae\u4f4d\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 server \u5340\u584a\u793a\u4f8b\uff1a<\/p>\n<pre><code>server {\n    listen 80;\n    server_name example.com;\n\n    location \/ {\n        root \/var\/www\/html;\n        index index.html index.htm;\n    }\n}<\/code><\/pre>\n<h3 id=\"3-%e6%8e%9b%e8%bc%89-handler-%e6%a8%a1%e5%a1%8a-tErZYtxINa\">3. \u639b\u8f09 handler \u6a21\u584a<\/h3>\n<p>\u5728 location \u5340\u584a\u4e2d\uff0c\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u639b\u8f09\u4e0d\u540c\u7684 handler \u6a21\u584a\u3002\u4f8b\u5982\uff0c\u82e5\u8981\u4f7f\u7528 FastCGI \u8655\u7406 PHP \u8acb\u6c42\uff0c\u53ef\u4ee5\u9019\u6a23\u914d\u7f6e\uff1a<\/p>\n<pre><code>location ~ .php$ {\n    include fastcgi_params;\n    fastcgi_pass 127.0.0.1:9000;\n    fastcgi_index index.php;\n    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n}<\/code><\/pre>\n<h3 id=\"4-%e6%b8%ac%e8%a9%a6%e9%85%8d%e7%bd%ae%e4%b8%a6%e9%87%8d%e5%95%9f-nginx-tErZYtxINa\">4. \u6e2c\u8a66\u914d\u7f6e\u4e26\u91cd\u555f Nginx<\/h3>\n<p>\u5b8c\u6210\u914d\u7f6e\u5f8c\uff0c\u5efa\u8b70\u5148\u6e2c\u8a66\u914d\u7f6e\u6587\u4ef6\u7684\u6b63\u78ba\u6027\uff1a<\/p>\n<pre><code>sudo nginx -t<\/code><\/pre>\n<p>\u5982\u679c\u6e2c\u8a66\u901a\u904e\uff0c\u5247\u53ef\u4ee5\u91cd\u555f Nginx \u4f7f\u914d\u7f6e\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx<\/code><\/pre>\n<h2 id=\"%e5%b8%b8%e8%a6%8b%e5%95%8f%e9%a1%8c-tErZYtxINa\">\u5e38\u898b\u554f\u984c<\/h2>\n<h3 id=\"1-%e5%a6%82%e4%bd%95%e6%9f%a5%e7%9c%8b-nginx-%e7%9a%84%e9%8c%af%e8%aa%a4%e6%97%a5%e8%aa%8c%ef%bc%9f-tErZYtxINa\">1. \u5982\u4f55\u67e5\u770b Nginx \u7684\u932f\u8aa4\u65e5\u8a8c\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e\u67e5\u770b Nginx \u7684\u932f\u8aa4\u65e5\u8a8c\u4f86\u6392\u67e5\u554f\u984c\uff0c\u65e5\u8a8c\u6587\u4ef6\u901a\u5e38\u4f4d\u65bc \/var\/log\/nginx\/error.log\u3002<\/p>\n<h3 id=\"2-%e5%a6%82%e4%bd%95%e5%84%aa%e5%8c%96-nginx-%e7%9a%84%e6%80%a7%e8%83%bd%ef%bc%9f-tErZYtxINa\">2. \u5982\u4f55\u512a\u5316 Nginx \u7684\u6027\u80fd\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e\u8abf\u6574 worker_processes\u3001worker_connections \u7b49\u53c3\u6578\u4f86\u512a\u5316 Nginx \u7684\u6027\u80fd\uff0c\u5177\u9ad4\u914d\u7f6e\u9700\u6839\u64da\u4f3a\u670d\u5668\u7684\u786c\u9ad4\u8cc7\u6e90\u548c\u61c9\u7528\u9700\u6c42\u9032\u884c\u8abf\u6574\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-tErZYtxINa\">\u7e3d\u7d50<\/h2>\n<p>Nginx \u7684 handler \u6a21\u584a\u70ba\u7528\u6236\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u8acb\u6c42\u8655\u7406\u80fd\u529b\uff0c\u901a\u904e\u5408\u7406\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u6709\u6548\u63d0\u5347\u7db2\u7ad9\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u7121\u8ad6\u662f\u975c\u614b\u5167\u5bb9\u7684\u63d0\u4f9b\u9084\u662f\u52d5\u614b\u8acb\u6c42\u7684\u8655\u7406\uff0cNginx \u90fd\u80fd\u5920\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u82e5\u60a8\u9700\u8981\u9032\u4e00\u6b65\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u4f86\u90e8\u7f72 Nginx\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u5728 Nginx \u4e2d\u639b\u8f09 handler \u6a21\u584a\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\u8207\u9748\u6d3b\u6027\uff0c\u638c\u63e1\u914d\u7f6e\u6280\u5de7\u8207\u6700\u4f73\u5be6\u8e10\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-51055","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51055","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=51055"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51055\/revisions"}],"predecessor-version":[{"id":51056,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51055\/revisions\/51056"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}