Nginx interpretation built-in non-default module ngx_http_stub_status_module

Source: Internet
Author: User

1 Background

Http://nginx.org/en/docs/http/ngx_http_stub_status_module.html

The Ngx_http_stub_status_module is an nginx built-in HTTP module that provides status information for Nginx. This module is not compiled by default, so it is necessary to specify the module to be loaded when compiling Nginx:

--with-http_stub_status_module

Why do you take it as an example? Because it is also a short enough module, is a typical handler module. Then we will explain the module process, are:

    1. A Brief introduction
    2. Examples of Use
    3. Instruction Introduction
    4. SOURCE Analysis
2 Simple Example
location /nginx_status {  stub_status on;  access_log   off;
access_log /usr/local/nginx/logs/status.log;    #日志 allow SOME.IP.ADD.RESS; deny all;}

We assume that you are experimenting on this machine and that you are opening port 80, then enter it in the browser:

http://localhost/nginx_status

You will see a message like this:

Active connections: 291server accepts handled requests  16630948 16630948 31070465Reading: 6 Writing: 179 Waiting: 106

Its meaning is easy to understand:

    • First line
      • Current number of active connections: 291
    • Second line
      • Number of connections accepted by the server: 16630948 (accepted connection #)
      • Number of connections that the server has processed: 16630948 (handled connection #)
      • Requests processed by the server: 31070465 (can be calculated, average 1.8 requests per connection) (Handled connection #)
    • Third line
      • The number of Reading–nginx read Requests is 6;
      • The number of times the Writting–nginx reads the request body, processes the request and sends a response to the client is 179;
      • waiting– the number of long connections currently active: 106.

Nginx official explanation is as follows:

    • active connections–number of all open connections
    • server accepts handled requests–nginx accepted 16630948 connections, handled 16630948 connections (no one is closed just it was accepted), and handles 31070465 requests (1.8 requests per connection)
    • reading–nginx Reads request header
    • writing–nginx reads request body, processes request, or writes response to a client
    • waiting–keep-alive connections, actually it is active-(reading + writing)
3 directives

The only instruction in this module is:

stub_status
    • Grammar:stub_status on
    • Scope: Location
    • Function: Statistics The location of the information.
4 Source Analysis

Look at the full code first:

/* Copyright (c) Igor Sysoev * Copyright (c) Nginx, Inc. */#include <ngx_config.h> #include <ngx_core.h> #incl                                 Ude <ngx_http.h>static Char *ngx_http_set_status (ngx_conf_t *cf, ngx_command_t *cmd, void *conf), static ngx_command_t ngx_http_status_commands[] = {{ngx_string ("Stub_status"), Ngx_http_srv_conf|n Gx_http_loc_conf| Ngx_conf_flag, ngx_http_set_status, 0, 0, NULL}, ngx_null_command};static ngx_http_module_t ngx                                  _http_stub_status_module_ctx = {NULL,/* preconfiguration */NULL, /* postconfiguration */NULL,/* Create main configuration */NUL L,/* init main configuration */NULL,/* Create Serv                          ER configuration */null,/* Merge server configuration */null,        /* Create location Configuration */NULL/* Merge location configuration */}; ngx_module_t Ngx_http_stub_status_module = {NGX_MODULE_V1, &AMP;NGX_HTTP_STUB_STATUS_MODULE_CTX,/* Module CO ntext */ngx_http_status_commands,/* Module directives */Ngx_http_module,/* mo  Dule type */NULL,/* INIT master */NULL,/* init module */NULL,/* INIT process */NULL,/* init Thread */NULL,/* EXIT thread */NULL,/* Exit PR Ocess */NULL,/* Exit Master */ngx_module_v1_padding};static ngx_int_t ngx_http_s    Tatus_handler (ngx_http_request_t *r) {size_t size;    ngx_int_t RC;    ngx_buf_t *b;   Ngx_chain_t out; Ngx_atomic_int_t ap, HN, AC, RQ, RD, WR;    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");        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 ("Active connections: \ n") + Ngx_atomic_t_len + sizeof ("server accepts handled requests\ N ")-1 + 6 + 3 * ngx_atomic_t_len + sizeof (" Reading:Writing:Waiting: \ n ") + 3 * 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;    AP = *ngx_stat_accepted;    HN = *ngx_stat_handled; Ac = *ngx_stat_active;    RQ = *ngx_stat_requests;    rd = *ngx_stat_reading;    WR = *ngx_stat_writing;    B->last = ngx_sprintf (b->last, "Active connections:%ua \ n", AC); B->last = Ngx_cpymem (b->last, "server accepts handled requests\n", sizeof ("server accepts ha    ndled requests\n ")-1);    B->last = ngx_sprintf (B->last, "%ua%ua%ua \ n", AP, HN, RQ); B->last = ngx_sprintf (B->last, "Reading:%ua Writing:%ua waiting:%ua \ n", RD, WR, AC-(R    D + WR));    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_http_status_handler; return NGX_CONF_OK;}

Pretty short, huh? The key is that Nginx provides a good way to extend the module, so you can write less code (NDK can let you write less, this is something).

4.1 Module Definition Ngx_http_stub_status_module
ngx_module_t  ngx_http_stub_status_module = {    NGX_MODULE_V1,    &ngx_http_stub_status_module_ctx,      /* module context */    ngx_http_status_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};

There is no essential difference from the Ngx_http_hello_world_module described earlier.

4.2 Command Set definition Ngx_http_status_commands
static ngx_command_t  ngx_http_status_commands[] = {    { ngx_string("stub_status"),      NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,      ngx_http_set_status,      0,      0,      NULL },      ngx_null_command};

command set definition as above, get the following information:

    • Name:stub_status
    • Type:server conf, location conf, Conf flag, one of the last relatively unfamiliar, similar values are:
      • #define NGX_CONF_ARGS_NUMBER 0x000000ff
      • #define NGX_CONF_BLOCK 0x00000100
      • #define NGX_CONF_FLAG 0x00000200
      • #define NGX_CONF_ANY 0x00000400
      • #define NGX_CONF_1MORE 0x00000800
      • #define NGX_CONF_2MORE 0x00001000
      • #define NGX_CONF_MULTI 0x00002000
    • Set:ngx_http_set_status

Here are some explanations of types:

4.2.1 Ngx_conf_xxx

The following macro definitions are from Ngx_conf_file.h:

#define NGX_CONF_NOARGS 0x00000001//command does not accept parameter # define NGX_CONF_TAKE1 0x00000002//Command carries 1 parameters # define Ngx_conf_take 2 0x00000004//Command carries 2 parameters # define NGX_CONF_TAKE3 0x00000008//Command carries 3 parameters # define NGX_CONF_TAKE4 0x00000010/ /Command carry 4 parameters # define NGX_CONF_TAKE5 0x00000020//Command carry 5 Parameters # define NGX_CONF_TAKE6 0x00000040//Command carry 6 parameters # define NG X_conf_take7 0x00000080//Command carries 7 parameter # define NGX_CONF_TAKE12 (ngx_conf_take1| NGX_CONF_TAKE2)//command carries 1 or 2 parameter # define NGX_CONF_TAKE13 (ngx_conf_take1| NGX_CONF_TAKE3)//command carries 1 or 3 parameter # define NGX_CONF_TAKE23 (ngx_conf_take2| NGX_CONF_TAKE3)//command carries 2 or 3 parameter # define NGX_CONF_TAKE123 (ngx_conf_take1| ngx_conf_take2| NGX_CONF_TAKE3)//command carries 1, 2 or 3 parameter # define NGX_CONF_TAKE1234 (ngx_conf_take1| ngx_conf_take2| ngx_conf_take3| NGX_CONF_TAKE4)//Command carry 1, 2, 3 or 4 parameter # define Ngx_conf_args_number 0x000000ff//Command # define Ngx_conf_block 0x00000100/ /block field, followed by {...}, such as server {...} #define NGX_CONF_FLAG 0x00000200//Command accepts "on|off "parameter # define Ngx_conf_any 0x00000400#define ngx_conf_1more 0x00000800//Command carry at least 1 parameters # define Ngx_conf_2more 0x00001000//Command carries at least 2 parameters # define NGX_CONF_MULTI 0x00002000//Command carry multiple parameters
4.3 Context definition Ngx_http_stub_status_module_ctx
static ngx_http_module_t  ngx_http_stub_status_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 */};

This is all NULL, simple enough, speechless??

4.4 Command Set function Ngx_http_set_status
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_http_status_handler;    return NGX_CONF_OK;}

In contrast to the Ngx_http_hello_world_module:

static char* ngx_http_hello_world(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_hello_world_handler;    ngx_conf_set_str_slot(cf, cmd, conf);    return NGX_CONF_OK;}

The only difference is that ngx_http_hello_world_module a more ngx_conf_set_str_slot. This is left to do a question, the following will be introduced, for the time being irrelevant to the key topic.

4.5 Command handling function Ngx_http_status_handler
static ngx_int_t ngx_http_status_handler(ngx_http_request_t *r){    size_t             size;    ngx_int_t          rc;    ngx_buf_t         *b;    ngx_chain_t        out;    ngx_atomic_int_t   ap, hn, ac, rq, rd, wr;

This module requires the accepted request class to be, and other types of requests to be rejected.

    if (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD) {        return NGX_HTTP_NOT_ALLOWED;    }

Discard the request body, because this module is not used.

    rc = ngx_http_discard_request_body(r);    if (rc != NGX_OK) {        return rc;    }

If the request is of the head type, the Content_Type, status field of the response header is set directly, and the response header is sent.

    ngx_str_set(&r->headers_out.content_type, "text/plain");    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;        }    }

Create a buffer and write to the buffer what we see above in the browser.

    size = sizeof ("Active connections: \ n") + Ngx_atomic_t_len + sizeof ("server accepts handled requests\n")-    1 + 6 + 3 * ngx_atomic_t_len + sizeof ("Reading:Writing:Waiting: \ n") + 3 * 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;    AP = *ngx_stat_accepted;    HN = *ngx_stat_handled;    AC = *ngx_stat_active;    RQ = *ngx_stat_requests;    rd = *ngx_stat_reading;    WR = *ngx_stat_writing;    Encapsulated sprintf b->last = ngx_sprintf (b->last, "Active connections:%ua \ n", AC); Encapsulates the memcpy b->last = Ngx_cpymem (b->last, "server accepts handled requests\n", sizeof ("    Server accepts handled requests\n ")-1);    B->last = ngx_sprintf (B->last, "%ua%ua%ua \ n", AP, HN, RQ); B->last = ngx_sprintf (B->last, "Reading:%ua Writing:%ua waiting:%ua \ n", RD, WR, ac-(rd + WR)); 

The buffer is finished. Then set the status of the response header, Content_length_n (remember?) The B->last-b->pos is just the second area of the buffer, which is the part of the data that has been written. )

    r->headers_out.status = NGX_HTTP_OK;    r->headers_out.content_length_n = b->last - b->pos;    b->last_buf = 1;

Sends a response header.

    rc = ngx_http_send_header(r);    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {        return rc;    }

Filter

    return ngx_http_output_filter(r, &out);}
5 Reference
    1. Http://wiki.nginx.org/HttpStubStatusModule
    2. http://blog.csdn.net/lengzijian/article/details/7356064
    3. Http://www.codinglabs.org/html/intro-of-nginx-module-development.html

Nginx interpretation built-in non-default module ngx_http_stub_status_module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.