{"id":51189,"date":"2024-10-08T16:43:17","date_gmt":"2024-10-08T08:43:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51189\/"},"modified":"2024-10-08T16:43:17","modified_gmt":"2024-10-08T08:43:17","slug":"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-handler-%e7%9a%84%e7%b7%a8%e5%af%ab%e6%ad%a5%e9%a9%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51189\/","title":{"rendered":"nginx \u6559\u7a0b\uff1aNginx handler \u7684\u7de8\u5beb\u6b65\u9a5f"},"content":{"rendered":"<h1 id=\"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-handler-%e7%9a%84%e7%b7%a8%e5%af%ab%e6%ad%a5%e9%a9%9f-MGDRdXWLkg\">Nginx \u6559\u7a0b\uff1aNginx handler \u7684\u7de8\u5beb\u6b65\u9a5f<\/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\u5834\u666f\u3002\u5176\u6a21\u7d44\u5316\u7684\u67b6\u69cb\u4f7f\u5f97\u7528\u6236\u80fd\u5920\u6839\u64da\u9700\u6c42\u64f4\u5c55\u529f\u80fd\uff0c\u5176\u4e2d handler \u7684\u7de8\u5beb\u662f Nginx \u64f4\u5c55\u7684\u91cd\u8981\u90e8\u5206\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u7de8\u5beb Nginx handler\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u5229\u7528\u9019\u4e00\u5f37\u5927\u7684\u5de5\u5177\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-nginx-handler%ef%bc%9f-MGDRdXWLkg\">\u4ec0\u9ebc\u662f Nginx Handler\uff1f<\/h2>\n<p>Nginx handler \u662f\u4e00\u7a2e\u7528\u65bc\u8655\u7406\u8acb\u6c42\u7684\u51fd\u6578\uff0c\u901a\u5e38\u7528\u65bc\u81ea\u5b9a\u7fa9\u8acb\u6c42\u7684\u8655\u7406\u908f\u8f2f\u3002\u901a\u904e\u7de8\u5beb handler\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u64f4\u5c55 Nginx \u7684\u529f\u80fd\uff0c\u5be6\u73fe\u7279\u5b9a\u7684\u696d\u52d9\u9700\u6c42\u3002\u4f8b\u5982\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u8acb\u6c42\u7684 URL \u6216\u8005\u8acb\u6c42\u7684\u5167\u5bb9\u4f86\u6c7a\u5b9a\u5982\u4f55\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e7%b7%a8%e5%af%ab-nginx-handler-%e7%9a%84%e6%ad%a5%e9%a9%9f-MGDRdXWLkg\">\u7de8\u5beb Nginx Handler \u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e7%92%b0%e5%a2%83%e6%ba%96%e5%82%99-MGDRdXWLkg\">1. \u74b0\u5883\u6e96\u5099<\/h3>\n<p>\u5728\u958b\u59cb\u7de8\u5beb handler \u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u6e96\u5099\u597d\u958b\u767c\u74b0\u5883\u3002\u78ba\u4fdd\u5df2\u7d93\u5b89\u88dd\u4e86 Nginx \u548c\u76f8\u95dc\u7684\u958b\u767c\u5de5\u5177\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u5b89\u88dd Nginx\uff1a<\/p>\n<pre><code>sudo apt-get update\nsudo apt-get install nginx\n<\/code><\/pre>\n<h3 id=\"2-%e5%89%b5%e5%bb%ba%e6%a8%a1%e7%b5%84-MGDRdXWLkg\">2. \u5275\u5efa\u6a21\u7d44<\/h3>\n<p>\u8981\u7de8\u5beb handler\uff0c\u9996\u5148\u9700\u8981\u5275\u5efa\u4e00\u500b Nginx \u6a21\u7d44\u3002\u53ef\u4ee5\u5728 Nginx \u7684\u6e90\u78bc\u76ee\u9304\u4e0b\u5275\u5efa\u4e00\u500b\u65b0\u7684\u6a21\u7d44\u6587\u4ef6\u593e\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code>cd \/path\/to\/nginx\/source\nmkdir my_module\ncd my_module\n<\/code><\/pre>\n<h3 id=\"3-%e7%b7%a8%e5%af%ab-handler-%e5%87%bd%e6%95%b8-MGDRdXWLkg\">3. \u7de8\u5beb handler \u51fd\u6578<\/h3>\n<p>\u5728\u6a21\u7d44\u6587\u4ef6\u593e\u4e2d\uff0c\u5275\u5efa\u4e00\u500b C \u8a9e\u8a00\u6587\u4ef6\uff0c\u4f8b\u5982 <code>my_handler.c<\/code>\uff0c\u4e26\u7de8\u5beb handler \u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684 handler \u7bc4\u4f8b\uff1a<\/p>\n<pre><code>#include \n#include \n#include \n\nstatic ngx_int_t my_handler(ngx_http_request_t *r) {\n    ngx_str_t response = ngx_string(\"Hello, Nginx!\");\n    r-&gt;headers_out.status = NGX_HTTP_OK;\n    r-&gt;headers_out.content_length_n = response.len;\n    ngx_http_send_header(r);\n    ngx_buf_t *b = ngx_create_temp_buf(r-&gt;pool, response.len);\n    ngx_memcpy(b-&gt;pos, response.data, response.len);\n    b-&gt;last = b-&gt;pos + response.len;\n    b-&gt;last_buf = 1;\n    r-&gt;upstream-&gt;output_filter(r, b);\n    return NGX_DONE;\n}\n<\/code><\/pre>\n<h3 id=\"4-%e8%a8%bb%e5%86%8a-handler-MGDRdXWLkg\">4. \u8a3b\u518a handler<\/h3>\n<p>\u5728\u6a21\u7d44\u7684 <code>ngx_http_my_module.c<\/code> \u6587\u4ef6\u4e2d\uff0c\u8a3b\u518a\u525b\u624d\u7de8\u5beb\u7684 handler \u51fd\u6578\u3002\u4ee5\u4e0b\u662f\u8a3b\u518a\u7684\u7bc4\u4f8b\uff1a<\/p>\n<pre><code>static ngx_http_module_t my_module_ctx = {\n    NULL, \/* preconfiguration *\/\n    NULL, \/* postconfiguration *\/\n\n    NULL, \/* create main configuration *\/\n    NULL, \/* init main configuration *\/\n\n    NULL, \/* create server configuration *\/\n    NULL, \/* merge server configuration *\/\n\n    NULL, \/* create location configuration *\/\n    NULL  \/* merge location configuration *\/\n};\n\nstatic ngx_command_t my_commands[] = {\n    { ngx_string(\"my_directive\"),\n      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,\n      my_handler,\n      NGX_HTTP_LOC_CONF_OFFSET,\n      0,\n      NULL },\n    ngx_null_command\n};\n\nstatic ngx_module_t my_module = {\n    NGX_MODULE_V1,\n    &amp;my_module_ctx, \/* module context *\/\n    my_commands, \/* module directives *\/\n    NGX_HTTP_MODULE, \/* module type *\/\n    NULL, \/* init master *\/\n    NULL, \/* init module *\/\n    NULL, \/* init process *\/\n    NULL, \/* init thread *\/\n    NULL, \/* exit thread *\/\n    NULL, \/* exit process *\/\n    NULL, \/* exit master *\/\n    NGX_MODULE_V1_PADDING\n};\n\nngx_module_t *ngx_http_my_module = &amp;my_module;\n<\/code><\/pre>\n<h3 id=\"5-%e7%b7%a8%e8%ad%af%e5%92%8c%e5%ae%89%e8%a3%9d%e6%a8%a1%e7%b5%84-MGDRdXWLkg\">5. \u7de8\u8b6f\u548c\u5b89\u88dd\u6a21\u7d44<\/h3>\n<p>\u5b8c\u6210 handler \u7684\u7de8\u5beb\u5f8c\uff0c\u9700\u8981\u7de8\u8b6f\u548c\u5b89\u88dd\u6a21\u7d44\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u4f86\u7de8\u8b6f Nginx\uff1a<\/p>\n<pre><code>cd \/path\/to\/nginx\/source\n.\/configure --add-module=.\/my_module\nmake\nsudo make install\n<\/code><\/pre>\n<h3 id=\"6-%e9%85%8d%e7%bd%ae-nginx-MGDRdXWLkg\">6. \u914d\u7f6e Nginx<\/h3>\n<p>\u7de8\u8b6f\u5b8c\u6210\u5f8c\uff0c\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5c0d\u61c9\u7684\u914d\u7f6e\u3002\u53ef\u4ee5\u5728 <code>nginx.conf<\/code> \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>location \/my {\n    my_directive;\n}\n<\/code><\/pre>\n<h3 id=\"7-%e9%87%8d%e5%95%9f-nginx-MGDRdXWLkg\">7. \u91cd\u555f Nginx<\/h3>\n<p>\u6700\u5f8c\uff0c\u91cd\u555f Nginx \u4ee5\u4f7f\u914d\u7f6e\u751f\u6548\uff1a<\/p>\n<pre><code>sudo systemctl restart nginx\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-MGDRdXWLkg\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u6210\u529f\u7de8\u5beb\u4e00\u500b\u7c21\u55ae\u7684 Nginx handler\uff0c\u4e26\u5c07\u5176\u96c6\u6210\u5230 Nginx \u4e2d\u3002\u9019\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u64f4\u5c55\u80fd\u529b\uff0c\u80fd\u5920\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9032\u884c\u5b9a\u5236\u5316\u958b\u767c\u3002\u82e5\u60a8\u9700\u8981\u66f4\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7528\u6236\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u7de8\u5beb Nginx handler \u7684\u8a73\u7d30\u6b65\u9a5f\uff0c\u638c\u63e1 Nginx \u7684\u9ad8\u6548\u80fd\u914d\u7f6e\u8207\u64f4\u5c55\u6280\u5de7\uff0c\u63d0\u5347\u7db2\u7ad9\u6027\u80fd\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-51189","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51189","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=51189"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51189\/revisions"}],"predecessor-version":[{"id":51190,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51189\/revisions\/51190"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}