问一个很小白的问题

来源:2-12 HTTP请求

zkang

2018-12-06

老师,请问一下如何将自己写的C文件编译进nginx呢?
图片描述
图片描述

我在src源码的外层目录新建了一个unit_test/src文件夹,然后自己写的单元测试代码放在这个里面,我想把这几个文件编译进nginx,请问该怎么做,我在configure参数里面添加了–add-module=./unit_test/src,然后再文件夹下新建了config文件,把几个源文件都添加进去了,但是貌似生成的makefile有问题.请问老师这个改怎么处理

图片描述

写回答

1回答

Jeson

2018-12-06

你好,你编译的时候,报出是什么错误?如果是自己学习入门的话,建议你参考如下这个例子:

(一)模块源码

准备工作:新建的模块名为ngx_http_mytest_module,于是在/home/zxin/nginx下新建个目录ngx_http_mytest_module。该目录下新建两个文件config,ngx_http_mytest_module.c。要将编写的模块加进nginx里,需要和nginx源码一起编译,因此,config就是告诉nginx如何将我们的模块编译进nginx。


ngx_http_mytest_module.c:
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_config.h>
 
static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
 
/* 配置指令 */
static ngx_command_t ngx_http_mytest_commands[] = {
        {
             ngx_string("mytest"),
             NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
             ngx_http_mytest,
             NGX_HTTP_LOC_CONF_OFFSET,
             0,
             NULL },
 
        ngx_null_command
};
 
/* 模块上下文 */
static ngx_http_module_t ngx_http_mytest_module_ctx = {
        NULL,
        NULL,
 
        NULL,
        NULL,
 
        NULL,
        NULL,
 
        NULL,
        NULL
};
 
/* 模块定义 */
ngx_module_t ngx_http_mytest_module = {
        NGX_MODULE_V1,
        &ngx_http_mytest_module_ctx,
        ngx_http_mytest_commands,
        NGX_HTTP_MODULE,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NULL,
        NGX_MODULE_V1_PADDING
};
 
/* 挂载handler函数 */
static char *ngx_http_mytest(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_mytest_handler;
 
        return NGX_CONF_OK;
}
 
/* handler函数 */
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
        //必须是GET或HEAD方法
        if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD)))
            return NGX_HTTP_NOT_ALLOWED;
 
        //丢弃请求包体
        ngx_int_t rc = ngx_http_discard_request_body(r);
        if (rc != NGX_OK) {
            return rc;
        }
 
        ngx_str_t type = ngx_string("text/plain");//响应的Content-Type
        ngx_str_t response = ngx_string("Hello World !");//响应的包体
 
        r->headers_out.status = NGX_HTTP_OK;//响应的状态码
        r->headers_out.content_length_n = response.len;//包体的长度
        r->headers_out.content_type = type;//响应的Content-Type
 
        rc = ngx_http_send_header(r);//发送响应头部
        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
            return rc;
        }
 
        ngx_buf_t *b;//构造ngx_buf_t结构,准备发送包体
        b = ngx_create_temp_buf(r->pool, response.len);
        if (b == NULL) {
            return NGX_HTTP_INTERNAL_SERVER_ERROR;
        }
 
        ngx_memcpy(b->pos, response.data, response.len);//将"Hello World !"拷贝到ngx_buf_t中
        b->last = b->pos + response.len;//设置last指针
        b->last_buf = 1;//声明是最后一块缓冲区
 
        ngx_chain_t out;//构造发送的ngx_chain_t结构
        out.buf = b;
        out.next = NULL;
 
        return ngx_http_output_filter(r, &out);//发送包体
}



config:

ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"


将模块编译进nginx内:


1.先进入目录:/usr/local/src/nginx-1.9.15


2.添加模块:./configure --add-module=/home/zxin/nginx/ngx_http_mytest_module

3.make

4.make install


然后修改nginx.conf文件,在server{...}配置块内加入

location /test {

                mytest;

        }

重启nginx:

/usr/local/nginx/sbin/nginx -s reload


接着便可访问:http://localhost/test



(二)实验结果

浏览器访问:

//img.mukewang.com/szimg/5c0912cf0001575808460292.jpg


0
0

Nginx入门到实践-Nginx中间件应用+搭建Webserver架构

中间件、负载均衡、应用层安全防护、动静分离、Nginx+LUA开发等

4183 学习 · 908 问题

查看课程