{"id":51051,"date":"2024-10-08T15:42:17","date_gmt":"2024-10-08T07:42:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/51051\/"},"modified":"2024-10-08T15:42:18","modified_gmt":"2024-10-08T07:42:18","slug":"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-%e7%a4%ba%e4%be%8b%ef%bc%9ahello-handler-%e6%a8%a1%e5%a1%8a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/51051\/","title":{"rendered":"nginx \u6559\u7a0b\uff1aNginx \u793a\u4f8b\uff1ahello handler \u6a21\u584a"},"content":{"rendered":"<h1 id=\"nginx-%e6%95%99%e7%a8%8b%ef%bc%9anginx-%e7%a4%ba%e4%be%8b%ef%bc%9ahello-handler-%e6%a8%a1%e5%a1%8a-iZIFLEcFDC\">Nginx \u6559\u7a0b\uff1aNginx \u793a\u4f8b\uff1ahello handler \u6a21\u584a<\/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\u672c\u6587\u5c07\u4ecb\u7d39 Nginx \u7684\u4e00\u500b\u7c21\u55ae\u793a\u4f8b\uff1ahello handler \u6a21\u584a\uff0c\u5e6b\u52a9\u8b80\u8005\u7406\u89e3\u5982\u4f55\u4f7f\u7528 Nginx \u4f86\u8655\u7406 HTTP \u8acb\u6c42\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-hello-handler-%e6%a8%a1%e5%a1%8a%ef%bc%9f-iZIFLEcFDC\">\u4ec0\u9ebc\u662f hello handler \u6a21\u584a\uff1f<\/h2>\n<p>hello handler \u6a21\u584a\u662f\u4e00\u500b\u7c21\u55ae\u7684 Nginx \u6a21\u584a\uff0c\u4e3b\u8981\u7528\u65bc\u56de\u61c9 HTTP \u8acb\u6c42\u4e26\u8fd4\u56de\u4e00\u6bb5\u7c21\u55ae\u7684\u6587\u672c\u3002\u9019\u500b\u6a21\u584a\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5e6b\u52a9\u958b\u767c\u8005\u7406\u89e3 Nginx \u7684\u8acb\u6c42\u8655\u7406\u6d41\u7a0b\uff0c\u4e26\u5c55\u793a\u5982\u4f55\u81ea\u5b9a\u7fa9 Nginx \u7684\u884c\u70ba\u3002<\/p>\n<h2 id=\"%e5%ae%89%e8%a3%9d-nginx-iZIFLEcFDC\">\u5b89\u88dd Nginx<\/h2>\n<p>\u5728\u4f7f\u7528 hello handler \u6a21\u584a\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b89\u88dd Nginx\u3002\u4ee5\u4e0b\u662f\u57fa\u65bc Ubuntu \u7cfb\u7d71\u7684\u5b89\u88dd\u6b65\u9a5f\uff1a<\/p>\n<pre><code>sudo apt update\nsudo apt install nginx\n<\/code><\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u555f\u52d5 Nginx\uff1a<\/p>\n<pre><code>sudo systemctl start nginx\n<\/code><\/pre>\n<h2 id=\"%e7%b7%a8%e5%af%ab-hello-handler-%e6%a8%a1%e5%a1%8a-iZIFLEcFDC\">\u7de8\u5beb hello handler \u6a21\u584a<\/h2>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u7de8\u5beb\u4e00\u500b\u7c21\u55ae\u7684 hello handler \u6a21\u584a\u3002\u9996\u5148\uff0c\u5275\u5efa\u4e00\u500b\u65b0\u7684 C \u6a94\u6848\uff0c\u4f8b\u5982 <code>ngx_http_hello_module.c<\/code>\uff0c\u4e26\u5728\u5176\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u78bc\uff1a<\/p>\n<pre><code>#include &lt;ngx_config.h&gt;\n#include &lt;ngx_core.h&gt;\n#include &lt;ngx_http.h&gt;\n\nstatic ngx_int_t ngx_http_hello_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    if (b == NULL) {\n        return NGX_HTTP_INTERNAL_SERVER_ERROR;\n    }\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;request_bufs = b;\n    return NGX_HTTP_OK;\n}\n\nstatic ngx_http_module_t ngx_http_hello_module_ctx = {\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL\n};\n\nstatic ngx_command_t ngx_http_hello_commands[] = {\n    { ngx_string(\"hello\"),\n      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,\n      ngx_http_hello_handler,\n      0,\n      0,\n      NULL },\n    ngx_null_command\n};\n\nstatic ngx_module_t ngx_http_hello_module = {\n    NGX_MODULE_V1,\n    &amp;ngx_http_hello_module_ctx,\n    ngx_http_hello_commands,\n    NGX_HTTP_MODULE,\n    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,\n    NGX_MODULE_V1_PADDING\n};\n\nngx_int_t ngx_http_hello_init(ngx_conf_t *cf) {\n    return NGX_OK;\n}\nNGX_MODULE_V1(ngx_http_hello_module, \"ngx_http_hello_module\", \"1.0\", NGX_MODULE_V1_PADDING);\n<\/code><\/pre>\n<h2 id=\"%e7%b7%a8%e8%ad%af%e5%92%8c%e5%ae%89%e8%a3%9d%e6%a8%a1%e5%a1%8a-iZIFLEcFDC\">\u7de8\u8b6f\u548c\u5b89\u88dd\u6a21\u584a<\/h2>\n<p>\u7de8\u5beb\u5b8c hello handler \u6a21\u584a\u5f8c\uff0c\u63a5\u4e0b\u4f86\u9700\u8981\u7de8\u8b6f\u548c\u5b89\u88dd\u5b83\u3002\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86 Nginx \u7684\u958b\u767c\u5305\uff0c\u7136\u5f8c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u7de8\u8b6f\u6a21\u584a\uff1a<\/p>\n<pre><code>cd \/path\/to\/nginx\/source\n.\/configure --add-module=\/path\/to\/ngx_http_hello_module\nmake\nsudo make install\n<\/code><\/pre>\n<h2 id=\"%e9%85%8d%e7%bd%ae-nginx-%e4%bd%bf%e7%94%a8-hello-handler-%e6%a8%a1%e5%a1%8a-iZIFLEcFDC\">\u914d\u7f6e Nginx \u4f7f\u7528 hello handler \u6a21\u584a<\/h2>\n<p>\u7de8\u8b6f\u5b8c\u6210\u5f8c\uff0c\u9700\u8981\u5728 Nginx \u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5c0d hello handler \u6a21\u584a\u7684\u8abf\u7528\u3002\u6253\u958b Nginx \u7684\u914d\u7f6e\u6587\u4ef6\uff08\u901a\u5e38\u4f4d\u65bc <code>\/etc\/nginx\/nginx.conf<\/code>\uff09\uff0c\u4e26\u5728 server \u5340\u584a\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5167\u5bb9\uff1a<\/p>\n<pre><code>location \/hello {\n    hello;\n}\n<\/code><\/pre>\n<p>\u4fdd\u5b58\u914d\u7f6e\u6587\u4ef6\u5f8c\uff0c\u91cd\u65b0\u52a0\u8f09 Nginx \u914d\u7f6e\uff1a<\/p>\n<pre><code>sudo systemctl reload nginx\n<\/code><\/pre>\n<h2 id=\"%e6%b8%ac%e8%a9%a6-hello-handler-%e6%a8%a1%e5%a1%8a-iZIFLEcFDC\">\u6e2c\u8a66 hello handler \u6a21\u584a<\/h2>\n<p>\u73fe\u5728\u53ef\u4ee5\u901a\u904e\u700f\u89bd\u5668\u6216\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5982 curl\uff09\u4f86\u6e2c\u8a66 hello handler \u6a21\u584a\u3002\u6253\u958b\u700f\u89bd\u5668\u4e26\u8a2a\u554f <code>http:\/\/your_server_ip\/hello<\/code>\uff0c\u61c9\u8a72\u6703\u770b\u5230 &#8220;Hello, Nginx!&#8221; \u7684\u97ff\u61c9\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-iZIFLEcFDC\">\u7e3d\u7d50<\/h2>\n<p>\u672c\u6587\u4ecb\u7d39\u4e86\u5982\u4f55\u4f7f\u7528 Nginx \u7684 hello handler \u6a21\u584a\u4f86\u8655\u7406 HTTP \u8acb\u6c42\u3002\u901a\u904e\u9019\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u8b80\u8005\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3 Nginx \u7684\u8acb\u6c42\u8655\u7406\u6d41\u7a0b\u53ca\u5176\u64f4\u5c55\u6027\u3002\u5982\u679c\u60a8\u5c0d\u65bc Nginx \u7684\u4f7f\u7528\u6709\u8208\u8da3\uff0c\u4e26\u5e0c\u671b\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u4ee5\u7372\u53d6\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 Nginx \u6559\u7a0b\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 hello handler \u6a21\u584a\u5275\u5efa\u7c21\u55ae\u793a\u4f8b\uff0c\u5feb\u901f\u638c\u63e1 Nginx \u7684\u57fa\u672c\u529f\u80fd\u8207\u914d\u7f6e\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-51051","post","type-post","status-publish","format-standard","hentry","category-setup-tutorials"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51051","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=51051"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51051\/revisions"}],"predecessor-version":[{"id":51052,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/51051\/revisions\/51052"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=51051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=51051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=51051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}