美文网首页
Nginx配置中的指令是如何被执行的

Nginx配置中的指令是如何被执行的

作者: ligang1109 | 来源:发表于2018-04-07 22:01 被阅读25次

之前遇到过一个nginx配置方面的问题,这里拿出来和大家分享下:

我们在定义一些指令后,会发现这样一个问题,有些指令会被执行,但是有些不会,比如下面这个配置:

location / {
    rewrite a b;
    rewrite b c;
    fastcgi_pass http://127.0.0.1:9000;
    content_by_lua_file test.lua;
}

执行表现如下:

  1. 两个rewrite指令均会执行
  2. fastcgi_passcontent_by_lua_file只能有一个执行

同一http处理阶段的指令为什么会有这两种不同的执行表现呢?下面说下我的理解:

nginx在启动后,会有个配置的解析过程,在这个过程中,会执行指令模块中定义的一些方法。

我们先看下http_rewrite模块的代码:

static ngx_int_tngx_http_rewrite_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_SERVER_REWRITE_PHASE].handlers);
    if (h == NULL) {
        return NGX_ERROR;   
    }            

    *h = ngx_http_rewrite_handler;

    return NGX_OK;
}

这段代码会将ngx_http_rewrite_handler方法添加到nginx在server_rewrite_phase中的handlers中。

这样在http处理的rewrite阶段,如果该模块指令的处理方法在handlers列表中被添加过,那么这些指令的处理方法都可以得到执行。

下面再来看下fastcgi模块的相关代码:

{ ngx_string("fastcgi_pass"),
    NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
    ngx_http_fastcgi_pass,
    NGX_HTTP_LOC_CONF_OFFSET,
    0,
    NULL },

static char *ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_fastcgi_loc_conf_t *flcf = conf;
    ngx_url_t                   u;
    ngx_str_t                  *value, *url;
    ngx_uint_t                  n;
    ngx_http_core_loc_conf_t   *clcf;
    ngx_http_script_compile_t   sc;

    if (flcf->upstream.upstream || flcf->fastcgi_lengths) {
        return "is duplicate";   
    }

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    clcf->handler = ngx_http_fastcgi_handler;

    ......
}

这段代码说明:当解析配置遇到fastcgi_pass指令后,会执行ngx_http_fastcgi_pass方法。

在这个方法中处理请求时,如果进入该指令所在的location,则将content阶段的处理方法设置为ngx_http_fastcgi_handler

最后再来看下nginx-lua模块的相关代码:

{ ngx_string("content_by_lua_file"),
    NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
    ngx_http_lua_content_by_lua,
    NGX_HTTP_LOC_CONF_OFFSET,
    0,
    (void *) ngx_http_lua_content_handler_file },

char *ngx_http_lua_content_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
......

    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    if (clcf == NULL) {
        return NGX_CONF_ERROR;
    }

    clcf->handler = ngx_http_lua_content_handler;

......
}

可以看到,和fastcgi_pass指令有着同样的处理过程。

通过上面的代码,我们可以得出如下结论:

  1. nginx指令的执行表现(是否会被执行),这个是在nginx解析配置时决定的。
  2. 指令对应的执行方法有两种注册方式:注册到处理阶段的执行列表中或是直接指定为该阶段的唯一处理方法。
  3. 使用上面第二种方式注册处理方法后,注册到该阶段执行列表中的方法就都不会得到执行。

以上是我在学习nginx中的一些个人理解,希望能帮助大家更好的使用nginx。个人见识有限,如果不当,还望指正,谢谢!

相关文章

  • Nginx配置中的指令是如何被执行的

    之前遇到过一个nginx配置方面的问题,这里拿出来和大家分享下: 我们在定义一些指令后,会发现这样一个问题,有些指...

  • 如何在Alpine容器中使用nginx

    安装nginx ,输入指令 apk add nginx 配置用户,通过apk直接安装的nginx配置中默认用户是n...

  • nginx简介和使用2

    nginx由多个经配置文件配置中的指令定义的模块组成,。 指令分为:简单指令和块指令。 简单的指令: 指令名、空格...

  • Nginx+Laravel URL优化

    Nginx 在Nginx中,使用如下站点配置指令就可以支持URL美化: ``` location / { try_...

  • linux运维之Nginx(二)

    Nginx(二) 关于配置指令: Main配置段常见的配置指令: 分类: 正常运行必备的配置 优化性能相关的配...

  • Nginx详细配置

    Nginx详细配置 默认的config: nginx文件结构 1、全局块:配置影响nginx全局的指令。一般有运行...

  • Nginx常用配置参数详解

    在Nginx配置文件中,每一条指令配置都必须以分号结束,请不要忘记。 用户组配置 用于配置运行Nginx服务器用户...

  • Nginx常用指令配置

    Nginx常用指令配置 先cd进入nginx目录下 启动 start nginx 停止 nginx -s stop...

  • Nginx负载均衡配置介绍

    目录 Nginx概述 Nginx调度算法 Nginx指令使用 Nginx负载均衡配置参数 1. Nginx概述 N...

  • Nginx: 基本配置篇

    nginx.conf 配置结构 1、main全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户...

网友评论

      本文标题:Nginx配置中的指令是如何被执行的

      本文链接:https://www.haomeiwen.com/subject/jkjuhftx.html