Nginx源码分析流程¶
源码:
1#include <ngx_config.h>
2#include <ngx_core.h>
3#include <ngx_http.h>
4
5typedef struct {
6 ngx_str_t output_words;
7} ngx_http_hello_world_loc_conf_t;
8
9// To process HelloWorld command arguments
10static char* ngx_http_hello_world(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);
11
12// Allocate memory for HelloWorld command
13static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf);
14
15// Copy HelloWorld argument to another place
16static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
17
18// Structure for the HelloWorld command
19static ngx_command_t ngx_http_hello_world_commands[] = {
20 {
21 ngx_string("hello_world"), // The command name
22 NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
23 ngx_http_hello_world, // The command handler
24 NGX_HTTP_LOC_CONF_OFFSET,
25 offsetof(ngx_http_hello_world_loc_conf_t, output_words),
26 NULL
27 },
28 ngx_null_command
29};
30
31// Structure for the HelloWorld context
32static ngx_http_module_t ngx_http_hello_world_module_ctx = {
33 NULL,
34 NULL,
35 NULL,
36 NULL,
37 NULL,
38 NULL,
39 ngx_http_hello_world_create_loc_conf,
40 ngx_http_hello_world_merge_loc_conf
41};
42
43// Structure for the HelloWorld module, the most important thing
44ngx_module_t ngx_http_hello_world_module = {
45 NGX_MODULE_V1,
46 &ngx_http_hello_world_module_ctx,
47 ngx_http_hello_world_commands,
48 NGX_HTTP_MODULE,
49 NULL,
50 NULL,
51 NULL,
52 NULL,
53 NULL,
54 NULL,
55 NULL,
56 NGX_MODULE_V1_PADDING
57};
58
59static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t* r) {
60 ngx_int_t rc;
61 ngx_buf_t* b;
62 ngx_chain_t out[2];
63
64 ngx_http_hello_world_loc_conf_t* hlcf;
65 hlcf = ngx_http_get_module_loc_conf(r, ngx_http_hello_world_module);
66
67 r->headers_out.content_type.len = sizeof("text/plain") - 1;
68 r->headers_out.content_type.data = (u_char*)"text/plain";
69
70 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
71
72 out[0].buf = b;
73 out[0].next = &out[1];
74
75 b->pos = (u_char*)"hello_world, ";
76 b->last = b->pos + sizeof("hello_world, ") - 1;
77 b->memory = 1;
78
79 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
80
81 out[1].buf = b;
82 out[1].next = NULL;
83
84 b->pos = hlcf->output_words.data;
85 b->last = hlcf->output_words.data + (hlcf->output_words.len);
86 b->memory = 1;
87 b->last_buf = 1;
88
89 r->headers_out.status = NGX_HTTP_OK;
90 r->headers_out.content_length_n = hlcf->output_words.len + sizeof("hello_world, ") - 1;
91 rc = ngx_http_send_header(r);
92 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
93 return rc;
94 }
95
96 return ngx_http_output_filter(r, &out[0]);
97}
98
99static void* ngx_http_hello_world_create_loc_conf(ngx_conf_t* cf) {
100 ngx_http_hello_world_loc_conf_t* conf;
101
102 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_world_loc_conf_t));
103 if (conf == NULL) {
104 return NGX_CONF_ERROR;
105 }
106 conf->output_words.len = 0;
107 conf->output_words.data = NULL;
108
109 return conf;
110}
111
112static char* ngx_http_hello_world_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child) {
113 ngx_http_hello_world_loc_conf_t* prev = parent;
114 ngx_http_hello_world_loc_conf_t* conf = child;
115 ngx_conf_merge_str_value(conf->output_words, prev->output_words, "Nginx");
116 return NGX_CONF_OK;
117}
118
119static char* ngx_http_hello_world(ngx_conf_t* cf, ngx_command_t* cmd, void* conf) {
120 ngx_http_core_loc_conf_t* clcf;
121 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
122 clcf->handler = ngx_http_hello_world_handler;
123 ngx_conf_set_str_slot(cf, cmd, conf);
124 return NGX_CONF_OK;
125}
126
配置文件:
1ngx_addon_name=ngx_http_hello_world_module
2HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
3NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"