14
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Organization

はじめてのnginxモジュール「ngx_http_hello_world」を作ってみた

目標

Hello world ! を表示する。他は何もしない。

やったこと

  • まずはconfigを用意する
config
ngx_addon_name=ngx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
  • 実装ファイルを用意する
ngx_http_hello_world_module.c
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

static ngx_command_t ngx_http_hello_world_module_commands[] = {
    {
         ngx_string("ngx_hello_world"),
         NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
         ngx_http_hello_world,
         0,
         0,
         NULL,
    },

    ngx_null_command,
};

static ngx_http_module_t  ngx_http_hello_world_module_ctx = {
    NULL,                          /* preconfiguration */
    NULL,                          /* postconfiguration */

    NULL,                          /* create main configuration */
    NULL,                          /* init main configuration */

    NULL,                          /* create server configuration */
    NULL,                          /* merge server configuration */

    NULL,                          /* create location configuration */
    NULL,                          /* merge location configuration */
};


ngx_module_t  ngx_http_hello_world_module = {
    NGX_MODULE_V1,
    &ngx_http_hello_world_module_ctx, /* module context */
    ngx_http_hello_world_module_commands,   /* module directives */
    NGX_HTTP_MODULE,               /* module type */
    NULL,                          /* init master */
    NULL,                          /* init module */
    NULL,                          /* init process */
    NULL,                          /* init thread */
    NULL,                          /* exit thread */
    NULL,                          /* exit process */
    NULL,                          /* exit master */
    NGX_MODULE_V1_PADDING
};

static ngx_str_t  text_plain_type = ngx_string("text/plain");
static u_char hello_world[] =  "Hello world !";

static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
    ngx_http_complex_value_t  cv;

    if (! (r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_HTTP_NOT_ALLOWED;
    }

    ngx_memzero(&cv, sizeof(ngx_http_complex_value_t));

    cv.value.len = sizeof(hello_world) - 1;
    cv.value.data = hello_world;

    return ngx_http_send_response(r, NGX_HTTP_OK, &text_plain_type, &cv);
}


static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_core_loc_conf_t  *clcf;

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    clcf->handler = ngx_http_hello_world_handler;

    return NGX_CONF_OK;
}

ビルドしてみる

./configure --add-module=<THIS MODULE DIRECTORY>
make
make install

設定する

conf/nginx.confに以下の行を追加します。

        location = /hello-world {
            ngx_hello_world;
        }

動かしてみる

./sbin/nginx -t
./sbin/nginx
wget http://localhost:10080/hello-world

できましたー!

参考になったもの

Register as a new user and use Qiita more conveniently

  1. You can follow users and tags
  2. you can stock useful information
  3. You can make editorial suggestions for articles
What you can do with signing up
14
Help us understand the problem. What are the problem?