├── .DS_Store ├── README.md ├── config └── ngx_http_hello_module.c /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/usamadar/ngx_hello_world/c35ed2106cf0e1cd6156dcb7221ae912160173cf/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is an improved version of the hello word Nginx module found at http://blog.zhuzhaoyuan.com/2009/08/creating-a-hello-world-nginx-module/ 2 | This module also accepts a value to the module directive and validates its value as well as prints it, instead of printing static "Hello World" 3 | 4 | The sample configuration can look like 5 | 6 | server { 7 | listen 8080; 8 | server_name localhost; 9 | 10 | location / { 11 | hello 'Hello World'; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /config: -------------------------------------------------------------------------------- 1 | ngx_addon_name=ngx_http_hello_module 2 | HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module" 3 | NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c" 4 | -------------------------------------------------------------------------------- /ngx_http_hello_module.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | static char *ngx_http_hello(ngx_conf_t *cf, void *post, void *data); 7 | 8 | static ngx_conf_post_handler_pt ngx_http_hello_p = ngx_http_hello; 9 | 10 | /* 11 | * The structure will holds the value of the 12 | * module directive hello 13 | */ 14 | typedef struct { 15 | ngx_str_t name; 16 | } ngx_http_hello_loc_conf_t; 17 | 18 | /* The function which initializes memory for the module configuration structure 19 | */ 20 | static void * 21 | ngx_http_hello_create_loc_conf(ngx_conf_t *cf) 22 | { 23 | ngx_http_hello_loc_conf_t *conf; 24 | 25 | conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t)); 26 | if (conf == NULL) { 27 | return NULL; 28 | } 29 | 30 | return conf; 31 | } 32 | 33 | /* 34 | * The command array or array, which holds one subarray for each module 35 | * directive along with a function which validates the value of the 36 | * directive and also initializes the main handler of this module 37 | */ 38 | static ngx_command_t ngx_http_hello_commands[] = { 39 | { ngx_string("hello"), 40 | NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, 41 | ngx_conf_set_str_slot, 42 | NGX_HTTP_LOC_CONF_OFFSET, 43 | offsetof(ngx_http_hello_loc_conf_t, name), 44 | &ngx_http_hello_p }, 45 | 46 | ngx_null_command 47 | }; 48 | 49 | 50 | static ngx_str_t hello_string; 51 | 52 | /* 53 | * The module context has hooks , here we have a hook for creating 54 | * location configuration 55 | */ 56 | static ngx_http_module_t ngx_http_hello_module_ctx = { 57 | NULL, /* preconfiguration */ 58 | NULL, /* postconfiguration */ 59 | 60 | NULL, /* create main configuration */ 61 | NULL, /* init main configuration */ 62 | 63 | NULL, /* create server configuration */ 64 | NULL, /* merge server configuration */ 65 | 66 | ngx_http_hello_create_loc_conf, /* create location configuration */ 67 | NULL /* merge location configuration */ 68 | }; 69 | 70 | 71 | /* 72 | * The module which binds the context and commands 73 | * 74 | */ 75 | ngx_module_t ngx_http_hello_module = { 76 | NGX_MODULE_V1, 77 | &ngx_http_hello_module_ctx, /* module context */ 78 | ngx_http_hello_commands, /* module directives */ 79 | NGX_HTTP_MODULE, /* module type */ 80 | NULL, /* init master */ 81 | NULL, /* init module */ 82 | NULL, /* init process */ 83 | NULL, /* init thread */ 84 | NULL, /* exit thread */ 85 | NULL, /* exit process */ 86 | NULL, /* exit master */ 87 | NGX_MODULE_V1_PADDING 88 | }; 89 | 90 | /* 91 | * Main handler function of the module. 92 | */ 93 | static ngx_int_t 94 | ngx_http_hello_handler(ngx_http_request_t *r) 95 | { 96 | ngx_int_t rc; 97 | ngx_buf_t *b; 98 | ngx_chain_t out; 99 | 100 | /* we response to 'GET' and 'HEAD' requests only */ 101 | if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { 102 | return NGX_HTTP_NOT_ALLOWED; 103 | } 104 | 105 | /* discard request body, since we don't need it here */ 106 | rc = ngx_http_discard_request_body(r); 107 | 108 | if (rc != NGX_OK) { 109 | return rc; 110 | } 111 | 112 | /* set the 'Content-type' header */ 113 | r->headers_out.content_type_len = sizeof("text/html") - 1; 114 | r->headers_out.content_type.len = sizeof("text/html") - 1; 115 | r->headers_out.content_type.data = (u_char *) "text/html"; 116 | 117 | /* send the header only, if the request type is http 'HEAD' */ 118 | if (r->method == NGX_HTTP_HEAD) { 119 | r->headers_out.status = NGX_HTTP_OK; 120 | r->headers_out.content_length_n = hello_string.len; 121 | 122 | return ngx_http_send_header(r); 123 | } 124 | 125 | /* allocate a buffer for your response body */ 126 | b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 127 | if (b == NULL) { 128 | return NGX_HTTP_INTERNAL_SERVER_ERROR; 129 | } 130 | 131 | /* attach this buffer to the buffer chain */ 132 | out.buf = b; 133 | out.next = NULL; 134 | 135 | /* adjust the pointers of the buffer */ 136 | b->pos = hello_string.data; 137 | b->last = hello_string.data + hello_string.len; 138 | b->memory = 1; /* this buffer is in memory */ 139 | b->last_buf = 1; /* this is the last buffer in the buffer chain */ 140 | 141 | /* set the status line */ 142 | r->headers_out.status = NGX_HTTP_OK; 143 | r->headers_out.content_length_n = hello_string.len; 144 | 145 | /* send the headers of your response */ 146 | rc = ngx_http_send_header(r); 147 | 148 | if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { 149 | return rc; 150 | } 151 | 152 | /* send the buffer chain of your response */ 153 | return ngx_http_output_filter(r, &out); 154 | } 155 | 156 | /* 157 | * Function for the directive hello , it validates its value 158 | * and copies it to a static variable to be printed later 159 | */ 160 | static char * 161 | ngx_http_hello(ngx_conf_t *cf, void *post, void *data) 162 | { 163 | ngx_http_core_loc_conf_t *clcf; 164 | 165 | clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); 166 | clcf->handler = ngx_http_hello_handler; 167 | 168 | ngx_str_t *name = data; // i.e., first field of ngx_http_hello_loc_conf_t 169 | 170 | if (ngx_strcmp(name->data, "") == 0) { 171 | return NGX_CONF_ERROR; 172 | } 173 | hello_string.data = name->data; 174 | hello_string.len = ngx_strlen(hello_string.data); 175 | 176 | return NGX_CONF_OK; 177 | } --------------------------------------------------------------------------------