美文网首页
nginx-2 添加新的功能模块

nginx-2 添加新的功能模块

作者: 王国的荣耀 | 来源:发表于2020-07-11 16:10 被阅读0次

问题: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"

模块开发过程

模块开发过程

参考

  1. Nginx 模块开发准备
  2. Nginx完全开发指南:使用C、C++、JavaScript和Lua

相关文章

  • nginx-2 添加新的功能模块

    问题:nginx如何添加一个新的功能模块? config ngx_http_ndg_hello_module.c ...

  • WEBPACK4+BABEL7+REACT 组件打包

    概述 由于最近项目需求,需要在原有架构上添加新功能模块。新功能模块基于react开发。大部分react单页应用使用...

  • iOS 从0到1搭建可用App框架

    搭建app框架需要:创建xcode工程,添加常用第三方框架,添加常用工具类,修改into.plist,添加功能模块...

  • javaScript进阶面向对象ES6-2020-01-06

    p14tab栏添加功能模块上 图片.png图片.png添加功能实现:图片.png图片.png图片.pngAdjac...

  • 11.9

    今天一天都在做往LED灯模块里添加语音功能模块,上午的时候已经简单的添加完成,可以实现LED灯开关的播报,下午的时...

  • Nginx-2 编译安装

    关闭防火墙关闭selinux 1、安装编译环境 yum -y install gcc gcc-c++ 2、安装pc...

  • 工作总结

    这一周主要教师端:作文库判断realname和photo为空的情况,添加默认链接,梳理裤兜书院UI界面和功能模块。...

  • android热修复平台架构方案

    热修复基于阿里的andfix实现,在原更新平台上添加新功能模块来实现。 1、系统结构 (1)整体结构 保持不变 (...

  • IOS学习笔记之类别与扩展

    1. 类别 (1)为已有的类添加新的方法 (2)可以在类别中添加新属性,但是不能为类别添加新的实例变量。 (3)缺...

  • 添加新商品

    建议使用浏览器:chrome浏览器 登录管理后台,选择商品管理菜单,点击添加商品。商品管理菜单 输入商品基本信息,...

网友评论

      本文标题:nginx-2 添加新的功能模块

      本文链接:https://www.haomeiwen.com/subject/rqzmcktx.html