阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx之Keepalive请求长连接复用率统计

188次阅读
没有评论

共计 6472 个字符,预计需要花费 17 分钟才能阅读完成。

该模块的实现原理比较简单:在 NGX_LOG_PHASE 阶段加入一个 handler,根据请求是否 upstream,再结合 upstream->peer.cached 参数即可判断该请求是否复用长连接。

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include <ngx_files.h>

#define KEEPALIVE_LOG_DEBUG
#define KEEPALIVE_LOG_DEBUG_LEVEL NGX_LOG_NOTICE
#define KEEPALIVE_LOG_MAX_LOG_LEN 1024

typedef struct {
 ngx_log_t *default_log;
}ngx_http_keepalive_log_main_conf_t;

typedef struct {
 ngx_flag_t on;
 ngx_log_t *log;
} ngx_http_keepalive_log_loc_conf_t;

static void* ngx_http_keepalive_log_create_main_conf(ngx_conf_t *cf);
static void* ngx_http_keepalive_log_create_loc_conf(ngx_conf_t *cf);
static char* ngx_http_keepalive_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_keepalive_log_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_keepalive_log_init_process(ngx_cycle_t *cycle);
static ngx_int_t ngx_http_keepalive_log_handler(ngx_http_request_t *r);
static void ngx_http_keepalive_log_get_req_info(ngx_http_request_t *r, ngx_str_t *serv, ngx_str_t *hostname, ngx_str_t *ip);
static ngx_log_t *ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name);
static char* ngx_http_keepalive_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

static ngx_http_module_t ngx_http_keepalive_log_module_ctx = {
    NULL,
    ngx_http_keepalive_log_init,    /* postconfiguration */
   
 ngx_http_keepalive_log_create_main_conf,  /* create main configuration */
    NULL,

    NULL,
    NULL,

    ngx_http_keepalive_log_create_loc_conf,
    ngx_http_keepalive_log_merge_loc_conf   
};

static ngx_command_t ngx_http_keepalive_log_commands[] = {
 {
  ngx_string(“keepalive_log”),
  NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LIF_CONF | NGX_CONF_TAKE1,
  ngx_http_keepalive_log_set_log,
  NGX_HTTP_LOC_CONF_OFFSET,
  0,
  NULL
 },
 {
  ngx_string(“keepalive_log_enable”),
  NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
  ngx_conf_set_flag_slot,
  NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_keepalive_log_loc_conf_t, on),
  NULL
 },
 ngx_null_command
};

ngx_module_t ngx_http_keepalive_log_module = {
 NGX_MODULE_V1,
 &ngx_http_keepalive_log_module_ctx,
 ngx_http_keepalive_log_commands,
 NGX_HTTP_MODULE,
 NULL,              /* init master */
 NULL,              /* init module */
 ngx_http_keepalive_log_init_process,      /* init process */
 NULL,              /* init thread */
 NULL,              /* exit thread */
 NULL,              /* exit process */
 NULL,              /* exit master */
 NGX_MODULE_V1_PADDING
};

static ngx_int_t
ngx_http_keepalive_log_init_process(ngx_cycle_t *cycle)
{
 ngx_http_keepalive_log_main_conf_t *kmcf = ngx_http_cycle_get_module_main_conf(cycle, ngx_http_keepalive_log_module);
 kmcf->default_log = &cycle->new_log;

 return NGX_OK;
}

static void*
ngx_http_keepalive_log_create_main_conf(ngx_conf_t *cf)
{
 ngx_http_keepalive_log_main_conf_t *kmcf;
 kmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_keepalive_log_main_conf_t));
 if(!kmcf) {
  return NGX_CONF_ERROR;
 }
 kmcf->default_log = NULL;

 return kmcf;
}

static void*
ngx_http_keepalive_log_create_loc_conf(ngx_conf_t *cf)
{
 ngx_http_keepalive_log_loc_conf_t *klcf;

 klcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_keepalive_log_loc_conf_t));
 if(klcf == NULL) {
  return NULL;
 }

 klcf->on = NGX_CONF_UNSET;
 klcf->log = NGX_CONF_UNSET_PTR;
   
    return klcf;
}

static char*
ngx_http_keepalive_log_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
 ngx_http_keepalive_log_loc_conf_t *prev = parent;
 ngx_http_keepalive_log_loc_conf_t *conf = child;

 ngx_conf_merge_value(conf->on, prev->on, 0);
 ngx_conf_merge_ptr_value(conf->log, prev->log, NULL);

 return NGX_CONF_OK;
}

static ngx_int_t
ngx_http_keepalive_log_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_LOG_PHASE].handlers);
 if (h == NULL) {
  return NGX_ERROR;
 }
 *h = ngx_http_keepalive_log_handler;

    return NGX_OK;
}

static ngx_int_t
ngx_http_keepalive_log_handler(ngx_http_request_t *r)
{
 ngx_fd_t fd;
 u_char *p;
 u_char msg[KEEPALIVE_LOG_MAX_LOG_LEN];
 ngx_http_keepalive_log_main_conf_t *kmcf;

 
 ngx_http_keepalive_log_loc_conf_t *klcf = ngx_http_get_module_loc_conf(r, ngx_http_keepalive_log_module);
 if(!klcf->on || !klcf->log) {
  return NGX_OK;
 }
 fd= klcf->log->file->fd;

 if (r == r->main) {
  u_char portstr[] = “65535”;   
  ngx_str_t serv;
  ngx_str_t hostname;
  ngx_str_t ip;
 
  serv.data = portstr;
  ngx_http_keepalive_log_get_req_info(r, &serv, &hostname, &ip);
 
#ifdef KEEPALIVE_LOG_DEBUG
  kmcf = ngx_http_get_module_main_conf(r, ngx_http_keepalive_log_module);
  ngx_log_error(KEEPALIVE_LOG_DEBUG_LEVEL, kmcf->default_log, 0,
   “%V %V %V”, &ip, &serv, &hostname);
#endif
 
  if (!r->upstream) {
   return NGX_OK;
  }

  ngx_uint_t cached = r->upstream->peer.cached ? 1 : 0;
  p = ngx_snprintf(msg, KEEPALIVE_LOG_MAX_LOG_LEN, “%V %V %V %ui\n”, ip, serv, hostname, cached);
  (void) ngx_write_fd(fd, msg, p – msg);
 }

    return NGX_OK;
}

static void
ngx_http_keepalive_log_get_req_info(ngx_http_request_t *r, ngx_str_t *serv, ngx_str_t *hostname, ngx_str_t *ip)
{
 ngx_http_core_srv_conf_t *cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

 if(cscf->server_name.len > 0) {
  *serv = cscf->server_name;
 } else {
  ngx_uint_t port;

  port = ntohs(((struct sockaddr_in*)(r->connection->local_sockaddr))->sin_port);
  if(port > 0 && port < 65536) {
   // 用端口来代表 server
   serv->len = ngx_sprintf(serv->data, “%ui”, port) – serv->data;
  } else {
   ngx_str_set(serv, “-“);
  }
 }

 if(r->headers_in.host) {
  *hostname = r->headers_in.host->value;
 } else {
  ngx_str_set(hostname, “-“);
 }

 *ip = r->connection->addr_text;
}

static ngx_log_t
*ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name)
{
 ngx_log_t *log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
 if(log == NULL) {
  return NULL;
 }

 log->file = ngx_conf_open_file(cycle, name);
 if(log->file == NULL) {
  return NULL;
 }

 return log;
}
 
static char*
ngx_http_keepalive_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
 ngx_http_keepalive_log_loc_conf_t *klcf = conf;
 ngx_str_t *value = NULL;

 if(klcf->log != NGX_CONF_UNSET_PTR) {
  return “is duplicated”;
 }

 value = cf->args->elts;
 klcf->log = ngx_log_create(cf->cycle, &value[1]);
 if(klcf->log == NULL) {
  return NGX_CONF_ERROR;
 }

 return NGX_CONF_OK;
}

Linux 下 HAProxy+Keepalived 双机高可用方案  http://www.linuxidc.com/Linux/2016-02/128566.htm

Haproxy+Keepalived 搭建 Weblogic 高可用负载均衡集群 http://www.linuxidc.com/Linux/2013-09/89732.htm

Keepalived+HAProxy 配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm

CentOS 6.3 下 Haproxy+Keepalived+Apache 配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm

Haproxy + KeepAlived 实现 WEB 群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm

Haproxy+Keepalived 构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.htm

配置文件 keepalived.conf 详解  http://www.linuxidc.com/Linux/2016-08/134792.htm

《Keepalived 权威指南》下载见 http://www.linuxidc.com/Linux/2012-05/60951.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-11/137487.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计6472字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中