Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1340179
  • 博文数量: 166
  • 博客积分: 46
  • 博客等级: 民兵
  • 技术积分: 4061
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-11 13:45
个人简介

现任职北京某互联网公司运维经理,高级架构师,涉足互联网运维行业已经超过10年。曾服务于京东商城,互动百科等互联网公司,早期运维界新星。 长期专研,C语言开发,操作系统内核,大型互联网架构。http://www.bdkyr.com

文章分类

分类: 架构设计与优化

2013-12-17 11:54:10

      博文题目之所以用改造,而没有用模块开发之类等。只是个人觉得本篇博文并未上升到真正的开发高度,而是在看懂前人代码的基础上,增加代码或者修改代码。使其更加完善,跟业务结合的更完美,效率更高。小小的改造放置于此,贵在一个思路,详情如下:
     

1、硬件环境:

   联想R430X 2U服务器

 

2、软件环境

   centos6.2 2.6.32-220.el6.x86_64

   nginx-1.0.10.tar.gz

3、所用配置文件关键部分

daemon off;  #注意关闭daemon模式
server


  {

    listen       8100;

    server_name  192.168.15.127;

    index index.html index.htm;

    root  /data0/htdocs/www;

    location /ok{

    load_status on;

    }

  }

4nginx模块目录

   cd /home/xuekun/nginx

   mkdir ngx_module_load

5、编辑nginx模块的编译相关文件(config)

 

[root@centos6 nginx]# cat ngx_module_load/config

ngx_addon_name=ngx_module_load

HTTP_MODULES="$HTTP_MODULES ngx_module_load"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_module_load.c"

CORE_LIBS="$CORE_LIBS "

6、模块源码文件

#include

#include

#include

#include

 

static char *ngx_http_set_status(ngx_conf_t *cf, ngx_command_t *cmd,

                void *conf);

 

static ngx_int_t ngx_load_average(ngx_int_t aload[], ngx_int_t nelem);

static ngx_command_t  ngx_load_commands[] = {

 

        { ngx_string("load_status"),

                NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,

                ngx_http_set_status,

                0,

                0,

                NULL },

 

        ngx_null_command

};

static ngx_http_module_t  ngx_load_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_module_load = {

        NGX_MODULE_V1,

        &ngx_load_module_ctx,      /* module context */

        ngx_load_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_int_t ngx_load_average(ngx_int_t aload[], ngx_int_t nelem)

{

        ngx_int_t i;

        double      loadavg[3];

        getloadavg(loadavg, nelem);

        for (i = 0; i < nelem; i ++) {

                aload[i] = loadavg[i] * 1000;

        }

        return NGX_OK;

}

static ngx_int_t ngx_load_handler(ngx_http_request_t *r)

{

        size_t             size;

        ngx_int_t          rc;

        ngx_buf_t         *b;

        ngx_chain_t        out;

        /*load value*/

        ngx_int_t          load;

 

        if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {

                return NGX_HTTP_NOT_ALLOWED;

        }

 

        rc = ngx_http_discard_request_body(r);

 

        if (rc != NGX_OK) {

                return rc;

        }

 

        ngx_str_set(&r->headers_out.content_type, "text/plain");

    /*get loadavg*/

        ngx_load_average(&load, 1);

 

        if (r->method == NGX_HTTP_HEAD) {

                r->headers_out.status = NGX_HTTP_OK;

 

                rc = ngx_http_send_header(r);

 

                if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {

                        return rc;

                }

        }

        size = sizeof("Current loadavg:  \n") + NGX_ATOMIC_T_LEN;

 

        b = ngx_create_temp_buf(r->pool, size);

        if (b == NULL) {

                return NGX_HTTP_INTERNAL_SERVER_ERROR;

        }

 

        out.buf = b;

        out.next = NULL;

 

        b->last = ngx_sprintf(b->last, "Current loadavg:%1.3f\n", load * 1.0 / 1000);

 

        r->headers_out.status = NGX_HTTP_OK;

        r->headers_out.content_length_n = b->last - b->pos;

 

        b->last_buf = 1;

 

        rc = ngx_http_send_header(r);

 

        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {

                return rc;

        }

 

        return ngx_http_output_filter(r, &out);

 

}

 

static char *ngx_http_set_status(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_load_handler;

 

        return NGX_CONF_OK;

}

 

7configuremake

   cd nginx-1.0.10

   ./configure --add-module=/home/xuekun/ngx_module_load/
8、运行nginx
./objs/nginx -c /home/xuekun/cstudy/nginx/nginx-1.0.10/conf/nginx.conf
9、通过浏览器查看效果
 

阅读(5037) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

fayewangfans2013-12-22 23:50:18

写的不错,看过一个显示helloworld的测试模块 。
前段时间在看nginx构架设计,还有实现代码,最近开始学一下nginx模块开发,蛮有意思的