17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-02-16

目標

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

できましたー!

参考になったもの

17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?