运维工具工具使用转载部分

当初我要是这么学习Nginx就好了!(多图详解)

2020-03-29  本文已影响0人  码农小光

以下文章来源于51CTO技术栈 ,作者李航

本文主要帮助大家熟悉 Nginx 有哪些应用场景、Nginx 特点和架构模型以及相关流程、Nginx 定制化开发的几种模块分类。读完本文你将对 Nginx 有一定的认识。

本文将围绕如下几个部分进行讲解:

Nginx 简介以及特点

Nginx (engine x) 是一个高性能的 Web 服务器和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器:

Nginx 社区分支:

Nginx 源码结构(代码量大约 11 万行 C 代码):

Nginx 特点如下:

Nginx 应用场景

Nginx 的应用场景如下:

Nginx 框架模型介绍

进程组件角色:

Nginx 的框架模型如下图:

image

框架模型流程如下图:

image

Nginx 内部流程介绍

Nginx 框架模型流程如下图:

image image

Master 初始化流程,如下图:

image

Worker 初始化:

image

Worker 初始化流程图如下:

image

静态文件请求 IO 流程如下图:

image

HTTP 请求流程如下图:

image

HTTP 请求 11 个阶段,如下图所示:

image

upstream模块:

upstream 框架流程,如下图:

image

upstream 内部流程,如下图:

image

反向代理流程,如下图:

image

Nginx 定制化模块开发

Nginx 的模块化设计特点如下:

内部核心模块:

image image

Handler 模块:接受来自客户端的请求并构建响应头和响应体。

image

Filter 模块:过滤(filter)模块是过滤响应头和内容的模块,可以对回复的头和内容进行处理。它的处理时间在获取回复内容之后,向用户发送响应之前。

image

Upstream 模块:使 Nginx 跨越单机的限制,完成网络数据的接收、处理和转发,纯异步的访问后端服务。

image

Load_Balance:负载均衡模块,实现特定的算法,在众多的后端服务器中,选择一个服务器出来作为某个请求的转发服务器。

image

ngx_lua 模块:

image

定制化开发 Demo

Handler 模块:

#配置文件:
server {
    ...    
    location test {
        test_counter on;
    }
}
#config
ngx_addon_name=ngx_http_test_module
HTTP_MODULES="$HTTP_MODULES ngx_http_test_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_test_module.c"
#ngx_http_test_module.c
static ngx_int_t
ngx_http_test_handler(ngx_http_request_t *r)
{
    ngx_int_t                               rc;
    ngx_buf_t                               *b;
    ngx_chain_t                             out;
    ngx_http_test_conf_t                    *lrcf;
    ngx_str_t                               ngx_test_string = ngx_string("hello test");

    lrcf = ngx_http_get_module_loc_conf(r, ngx_http_test_module);
    if ( lrcf->test_counter == 0 ) {
        return NGX_DECLINED;
    }

    /* we response to 'GET' and 'HEAD' requests only */
    if ( !(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD)) ) {
            return NGX_HTTP_NOT_ALLOWED;
    }

    /* discard request body, since we don't need it here */
    rc = ngx_http_discard_request_body(r);

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

    /* set the 'Content-type' header */
    /*
     *r->headers_out.content_type.len = sizeof("text/html") - 1;
     *r->headers_out.content_type.data = (u_char *)"text/html";
    */
    ngx_str_set(&r->headers_out.content_type, "text/html");

    /* send the header only, if the request type is http 'HEAD' */
    if ( r->method == NGX_HTTP_HEAD ) {
        r->headers_out.status = NGX_HTTP_OK;
        r->headers_out.content_length_n = ngx_test_string.len;

        return ngx_http_send_header(r);
    }

    /* set the status line */
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n =  ngx_test_string.len;

    /* send the headers of your response */
    rc = ngx_http_send_header(r);
    if ( rc == NGX_ERROR || rc > NGX_OK || r->header_only ) {
        return rc;
    }

    /* allocate a buffer for your response body */
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if ( b == NULL ) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    /* attach this buffer to the buffer chain */
    out.buf = b;
    out.next = NULL;

    /* adjust the pointers of the buffer */
    b->pos = ngx_test_string.data;
    b->last = ngx_test_string.data + ngx_test_string.len;
    b->memory = 1;    /* this buffer is in memory */
    b->last_buf = 1;  /* this is the last buffer in the buffer chain */

    /* send the buffer chain of your response */
    return ngx_http_output_filter(r, &out);
}

Nginx 核心时间点模块介绍

解决接入层故障定位慢的问题,帮助 OP 快速判定问题根因,优先自证清白,提高接入层高效的生产力。

image

Nginx 分流模块介绍

Nginx 分流模块特点如下:

image

Nginx 动态 upstream 模块介绍

让接入层可以适配动态调度的云环境,实现服务的平滑上下线、弹性扩/缩容。

从而提高接入层高效的生产力以及稳定性,保证业务流量的平滑无损。

image

Nginx query_upstream 模块介绍

链路追踪,梳理接口到后端链路的情况。查询 location 接口对应 upstream server 信息。 image

Nginx query_conf 模块介绍

获取 Nginx 配置文件格式化为 json 格式信息: image

Nginx 共享内存支持 Redis 协议模块介绍

根据配置文件来动态的添加共享内存:

https://github.com/lidaohang/ngx_shm_dict 

ngx_shm_dict:共享内存核心模块(红黑树,队列)ngx_shm_dict_manager:添加定时器事件,定时的清除共享内存中过期的 Key,添加读事件,支持 Redis 协议,通过 redis-cli get,set,del,ttlngx_shm_dict_view:共享内存查看

image

Nginx 日志回放压测工具

解析日志进行回放压测,模拟后端服务器慢等各种异常情况 :

https://github.com/lidaohang/playback-testing

方案说明:

使用方式:

server/backserver/main.go
bin/wrk -c30 -t1 -s conf/nginx_log.lua http://localhost:8095

作者:李航

简介:多年的底层开发经验,在高性能 Nginx 开发和分布式缓存 Redis Cluster 有着丰富的经验,目前从事分布式存储工作。先后在 58 同城、汽车之家、优酷土豆集团工作。目前供职于滴滴基础平台-技术专家岗位,主要负责分布式 Ceph 系统。个人主要关注的技术领域:高性能 Nginx 开发、分布式缓存、分布式存储。

编辑:陶家龙

上一篇下一篇

猜你喜欢

热点阅读