问题:nginx如何添加一个新的功能模块?
mkdir ngx_http_ndg_hello_module
touch config
touch ngx_http_ndg_hello_module.c
config
ngx_addon_name=ngx_http_ndg_hello_module
ngx_module_type=HTTP
ngx_module_name=ngx_http_ndg_hello_module
ngx_module_srcs="$ngx_addon_dir/ngx_http_ndg_hello_module.c"
. auto/module
ngx_http_ndg_hello_module.c
#include<ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
/* ngx_str_t output_words;*/
ngx_flag_t enable;
} ngx_http_ndg_hello_loc_conf_t;
static ngx_int_t ngx_http_ndg_hello_handler(ngx_http_request_t *r);
static ngx_int_t ngx_http_ndg_hello_init(ngx_conf_t *cf);
static void *ngx_http_ndg_hello_create_loc_conf(ngx_conf_t *cf);
/**
* This module provided directive: hello world.
*
*/
static ngx_command_t ngx_http_ndg_hello_commands[] = {
{ngx_string("ndg_hello"), /* directive 指令的名字*/
NGX_HTTP_LOC_CONF | NGX_CONF_FLAG, /* location context and takes no arguments*/
ngx_conf_set_flag_slot, /* configuration setup function */
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_ndg_hello_loc_conf_t, enable),
NULL},
ngx_null_command /*command termination */
};
/* The module context. */
static ngx_http_module_t ngx_http_ndg_hello_module_ctx = {
NULL, /* preconfiguration */
ngx_http_ndg_hello_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_ndg_hello_create_loc_conf, /* create location configuration */
NULL, /* merge location configuration */
};
/* Module definition. */
ngx_module_t ngx_http_ndg_hello_module = {
NGX_MODULE_V1,
&ngx_http_ndg_hello_module_ctx, /* module context */
ngx_http_ndg_hello_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 void * ngx_http_ndg_hello_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_ndg_hello_loc_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_ndg_hello_loc_conf_t));
if (conf == NULL)
{
return NULL;
}
conf->enable = NGX_CONF_UNSET;
return conf;
}
static ngx_int_t ngx_http_ndg_hello_handler(ngx_http_request_t *r)
{
ngx_http_ndg_hello_loc_conf_t *lcf;
lcf = ngx_http_get_module_loc_conf(r, ngx_http_ndg_hello_module);
if (lcf->enable)
{
printf("hello world \n");
ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"hello ansi c");
}else
{
printf("hello disabled \n");
}
return NGX_DECLINED;
}
static ngx_int_t ngx_http_ndg_hello_init(ngx_conf_t* cf) {
ngx_http_handler_pt *h;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers);
*h = ngx_http_ndg_hello_handler;
return NGX_OK;
}
编译安装nginx 1.19.0
./auto/configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/path/nginx-release-1.19.0/ngx_http_ndg_hello_module
make
make install
vim /usr/local/etc/nginx/nginx.conf
#add location
location /hello{
ndg_hello on;
}
brew services restart nginx
curl -v '127.0.0.1:8080'
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Sat, 11 Jul 2020 08:04:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 14 Jun 2020 18:54:05 GMT
Connection: keep-alive
ETag: "5ee6724d-264"
Accept-Ranges: bytes
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
➜ nginx-release-1.19.0 curl -i "http://127.0.0.1:8080/hello"
HTTP/1.1 301 Moved Permanently
Server: nginx/1.19.0
Date: Sat, 11 Jul 2020 08:05:11 GMT
Content-Type: text/html
Content-Length: 169
Location: http://127.0.0.1:8080/hello/
Connection: keep-alive
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.19.0</center>
</body>
</html>
#查看error.log
2020/07/11 16:05:11 [error] 56352#0: *18 hello ansi c, client: 127.0.0.1, server: localhost, request: "GET /hello HTTP/1.1", host: "127.0.0.1:8080"
模块开发过程
![](https://img.haomeiwen.com/i1546510/1f6c3d173328a780.png)
模块开发过程
参考
- Nginx 模块开发准备
- Nginx完全开发指南:使用C、C++、JavaScript和Lua
网友评论