接入层Nginx架构及模块介绍分享

发布时间:2020-08-08 08:51:40 作者:java06051515
来源:ITPUB博客 阅读:155
课程目标:

1)帮助大家对Nginx有一定的认识
2)熟悉Nginx有哪些应用场景
3)熟悉Nginx特点和架构模型以及相关流程
4)熟悉Nginx定制化开发的几种模块分类

课程大纲:
  1. Nginx简介及特点
  2. Nginx应用场景
  3. Nginx框架模型介绍
  4. Nginx内部流程介绍
  5. Nginx自定义模块开发介绍
  6. Nginx核心时间点模块介绍
  7. Nginx分流模块介绍
  8. Nginx动态upstream模块介绍
  9. Nginx query_upstrem模块介绍
  10. Nginx query_conf模块介绍
  11. Nginx 共享内存支持redis协议模块介绍
  12. Nginx 日志回放压测工具介绍

1. Nginx简介以及特点

Nginx简介:

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

Nginx社区分支:

Nginx源码结构:

Nginx特点:

2. Nginx应用场景

场景如下:

3. Nginx框架模型介绍

进程组件角色:

框架模型:
接入层Nginx架构及模块介绍分享

框架模型流程:
接入层Nginx架构及模块介绍分享

4. Nginx内部流程介绍

4.1 框架模型流程

接入层Nginx架构及模块介绍分享

接入层Nginx架构及模块介绍分享

4.2 master初始化流程

接入层Nginx架构及模块介绍分享

4.3 worker初始化

接入层Nginx架构及模块介绍分享

4.4 worker初始化流程

接入层Nginx架构及模块介绍分享

4.5 静态文件请求IO流程

接入层Nginx架构及模块介绍分享

4.6 http请求流程

接入层Nginx架构及模块介绍分享

4.7 http请求11个阶段

接入层Nginx架构及模块介绍分享

4.8 upstream模块

4.8.1 upstream框架流程

接入层Nginx架构及模块介绍分享

4.8.2 upstream内部流程

接入层Nginx架构及模块介绍分享

4.9 反向代理流程

接入层Nginx架构及模块介绍分享

5. Nginx定制化模块开发

5.1 Nginx的模块化设计特点

5.1 内部核心模块

接入层Nginx架构及模块介绍分享

接入层Nginx架构及模块介绍分享

5.2 handler模块

5.3 filter模块

5.4 upstream模块

load_balance:

5.3 ngx_lua模块

接入层Nginx架构及模块介绍分享

5.4 定制化开发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);
}

6. Nginx核心时间点模块介绍

解决接入层故障定位慢的问题,帮助OP快速判定问题根因,优先自证清白,提高接入层高效的生产力。
接入层Nginx架构及模块介绍分享

7. Nginx分流模块介绍

特点:
实现非常灵活的动态的修改策略从而进行切流量。
实现平滑无损的方式进行流量的切换。
通过秒级切换流量可以缩小影响范围,从而减少损失。
按照某一城市或者某个特征,秒级进行切换流量或者禁用流量。
容忍单机房级别容量故障,缩短了单机房故障的止损时间。
快速的将流量隔离或者流量抽样。
高效的灰度测试,提高生产力。
接入层Nginx架构及模块介绍分享

8. Nginx动态upstream模块介绍

让接入层可以适配动态调度的云环境,实现服务的平滑上下线、弹性扩/缩容。
从而提高接入层高效的生产力以及稳定性,保证业务流量的平滑无损。
接入层Nginx架构及模块介绍分享

9. Nginx query_upstream模块介绍

链路追踪,梳理接口到后端链路的情况。查询location接口对应upstream server信息。
接入层Nginx架构及模块介绍分享

10. Nginx query_conf模块介绍

获取nginx配置文件格式化为json格式信息。
接入层Nginx架构及模块介绍分享

11.Nginx 共享内存支持redis协议模块介绍

根据配置文件来动态的添加共享内存。
https://github.com/lidaohang/ngx_shm_dict

12. Nginx日志回放压测工具

方案说明
使用方式
作者:李航

现在注册滴滴云,有机会可得30元无门槛滴滴出行券

新购云服务1月5折 3月4.5折 6月低至4折

滴滴云使者招募,推荐最高返佣50%

推荐阅读:
  1. Nginx模块概述—干货分享!
  2. LNMP架构、Nginx介绍及MySQL、PHP安装教程

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx 介绍 分享

上一篇:JavaScript、Ajax、jQuery全部知识点,1分钟速懂!

下一篇:云盘工具CloudMounter for Mac

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》