参考的一篇博客《为NGINX编译第三方动态模块》
源码的地址:https://github.com/perusio/nginx-hello-world-module/tree/master。
源码被我放到系统中/root/nginx-hello-world-module
目录下,ls /root/nginx-hello-world-module
可以看一下源码里边的内容。
/root/nginx-hello-world-module/ngx_http_hello_world_module.c
里边的内容如下:
/**
* @file ngx_http_hello_world_module.c
* @author António P. P. Almeida <appa@perusio.net>
* @date Wed Aug 17 12:06:52 2011
*
* @brief A hello world module for Nginx.
*
* @section LICENSE
*
* Copyright (C) 2011 by Dominic Fallows, António P. P. Almeida <appa@perusio.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define HELLO_WORLD "hello world\r\n"
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r);
/**
* This module provided directive: hello world.
*
*/
static ngx_command_t ngx_http_hello_world_commands[] = {
{ ngx_string("hello_world"), /* directive */
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, /* location context and takes
no arguments*/
ngx_http_hello_world, /* configuration setup function */
0, /* No offset. Only one context is supported. */
0, /* No offset when storing the module configuration on struct. */
NULL},
ngx_null_command /* command termination */
};
/* The hello world string. */
static u_char ngx_hello_world[] = HELLO_WORLD;
/* The module context. */
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 */
};
/* Module definition. */
ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx, /* module context */
ngx_http_hello_world_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
};
/**
* Content handler.
*
* @param r
* Pointer to the request structure. See http_request.h.
* @return
* The status of the response generation.
*/
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
/* Set the Content-Type header. */
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *) "text/plain";
/* Allocate a new buffer for sending out the reply. */
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
/* Insertion in the buffer chain. */
out.buf = b;
out.next = NULL; /* just one buffer */
b->pos = ngx_hello_world; /* first position in memory of the data */
b->last = ngx_hello_world + sizeof(ngx_hello_world) - 1; /* last position in memory of the data */
b->memory = 1; /* content is in read-only memory */
b->last_buf = 1; /* there will be no more buffers in the request */
/* Sending the headers for the reply. */
r->headers_out.status = NGX_HTTP_OK; /* 200 status code */
/* Get the content length of the body. */
r->headers_out.content_length_n = sizeof(ngx_hello_world) - 1;
ngx_http_send_header(r); /* Send the headers */
/* Send the body, and return the status code of the output filter chain. */
return ngx_http_output_filter(r, &out);
} /* ngx_http_hello_world_handler */
/**
* Configuration setup function that installs the content handler.
*
* @param cf
* Module configuration structure pointer.
* @param cmd
* Module directives structure pointer.
* @param conf
* Module configuration structure pointer.
* @return string
* Status of the configuration setup.
*/
static char *ngx_http_hello_world(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */
/* Install the hello world handler. */
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
clcf->handler = ngx_http_hello_world_handler;
return NGX_CONF_OK;
} /* ngx_http_hello_world */
/root/nginx-hello-world-module/config
里边的内容如下:
ngx_addon_name=ngx_http_hello_world_module
if test -n "$ngx_module_link"; then
ngx_module_type=HTTP
ngx_module_name=ngx_http_hello_world_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_hello_world_module.c"
. auto/module
else
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
fi
我的nginx
源码所在目录是/root/nginx-1.14.0
。
sudo /nginx/sbin/nginx -V
看一下configure arguments:
后边的字符串,我这里是--prefix=/nginx --add-module=/root/njs-0.7.0/nginx
还需要添加到到./configure
后边。
注意下边命令需要到nginx
源码中configure
所在目录执行。
sudo ./configure --prefix=/nginx --add-module=/root/njs-0.7.0/nginx --add-dynamic-module=/root/nginx-hello-world-module
,可以看到--prefix=/nginx --add-module=/root/njs-0.7.0/nginx
是上边使用sudo /nginx/sbin/nginx -V
看到的configure arguments
,而--add-dynamic-module=/root/nginx-hello-world-module
是后边填上的,/root/nginx-hello-world-module
就是源码所在位置。
完成之后如下图:
sudo make modules
只编译模块相关代码。
完成之后如下:
最后产生的.so
文件在源代码目录中的objs
目录里边。
在nginx
的nginx.conf
(我的是/nginx/conf/nginx.conf
)填上一些内容,整体内容如下:
worker_processes 1;
load_module /root/nginx-1.14.0/objs/ngx_http_hello_world_module.so;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
hello_world;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
/nginx/sbin/nginx -t -c /nginx/conf/nginx.conf
查看一下/nginx/conf/nginx.conf
的拼写是否正确。
我这里显示如下:
nginx: the configuration file /nginx/conf/nginx.conf syntax is ok
nginx: configuration file /nginx/conf/nginx.conf test is successful
只要显示有syntax is ok
和test is successful
这样的字样,说明拼写正确。
sudo /nginx/sbin/nginx -c /nginx/conf/nginx.conf
使用/nginx/conf/nginx.conf
启动nginx
。
curl http://localhost:80
返回字符串hello world
就是成功了。