ngx_core_commands

本文详细解析了NGINX的配置文件结构及其核心配置项,通过具体示例阐述了如何设置NGINX的各项功能参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近一直在看NGINX,虽然还是不得要领,但是在宏观把握的前提下,慢慢的深入分析问题。一点一滴的积累,相信能达到融会贯通。罗马非一日炼成的。最近一直在看NGINX,虽然还是不得要领,但是在宏观把握的前提下,慢慢的深入分析问题。一点一滴的积累,相信能达到融会贯通。罗马非一日炼成的。

typedef struct ngx_file_s        ngx_file_t;
struct ngx_command_s {
    ngx_str_t             name;         //配置项名称


    //配置项类型(有几个参数或者可以在什么地方出现等)
    ngx_uint_t            type;         


    //出现了name中制定的配置项后,将会调用set方法处理配置项参数。
    //这个可以使用nginx预设的14个解析配置方法,也可以使用自定义的。
    char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);


    //在配置文件中的偏移量,它的取值范围是:
    /*
    NGX_HTTP_MAIN_CONF_OFFSET
    NGX_HTTP_SRV_CONF_OFFSET
    NGX_HTTP_LOC_CONF_OFFSET
    */
    //因为有可能模块同时会有main,srv,loc三种配置结构体,但是这个配置项解析完后要放在哪个结构体内呢?
    ngx_uint_t            conf;     


    //表示当前配置项在整个存储配置项的结构体中的偏移位置,
    //可以使用offsetof(test_stru, b)来获取    
    ngx_uint_t            offset;


    //命令处理完后的回调指针,对于set的14种预设的解析配置方法, 可能的结构有:
    /*
    ngx_conf_post_t
    ngx_conf_enum_t
    ngx_conf_bitmask_t
    null
    */
    void                 *post;
};



static ngx_command_t  ngx_core_commands[] = {


    { ngx_string("daemon"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      0,
      offsetof(ngx_core_conf_t, daemon),
      NULL },


    { ngx_string("master_process"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      0,
      offsetof(ngx_core_conf_t, master),
      NULL },


    { ngx_string("timer_resolution"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_msec_slot,
      0,
      offsetof(ngx_core_conf_t, timer_resolution),
      NULL },


    { ngx_string("pid"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, pid),
      NULL },


    { ngx_string("lock_file"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, lock_file),
      NULL },


    { ngx_string("worker_processes"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
      offsetof(ngx_core_conf_t, worker_processes),
      NULL },


    { ngx_string("debug_points"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_enum_slot,
      0,
      offsetof(ngx_core_conf_t, debug_points),
      &ngx_debug_points },


    { ngx_string("user"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE12,
      ngx_set_user,
      0,
      0,
      NULL },


    { ngx_string("worker_priority"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_set_priority,
      0,
      0,
      NULL },


    { ngx_string("worker_cpu_affinity"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_1MORE,
      ngx_set_cpu_affinity,
      0,
      0,
      NULL },


    { ngx_string("worker_rlimit_nofile"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
      offsetof(ngx_core_conf_t, rlimit_nofile),
      NULL },


    { ngx_string("worker_rlimit_core"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_off_slot,
      0,
      offsetof(ngx_core_conf_t, rlimit_core),
      NULL },


    { ngx_string("worker_rlimit_sigpending"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
      offsetof(ngx_core_conf_t, rlimit_sigpending),
      NULL },


    { ngx_string("working_directory"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      0,
      offsetof(ngx_core_conf_t, working_directory),
      NULL },


    { ngx_string("env"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_set_env,
      0,
      0,
      NULL },


#if (NGX_THREADS)
    { ngx_string("worker_threads"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_num_slot,
      0,
      offsetof(ngx_core_conf_t, worker_threads),
      NULL },


    { ngx_string("thread_stack_size"),
      NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_size_slot,
      0,
      offsetof(ngx_core_conf_t, thread_stack_size),
      NULL },


#endif


      ngx_null_command
};

最近一直在看NGINX,虽然还是不得要领,但是在宏观把握的前提下,慢慢的深入分析问题。一点一滴的积累,相信能达到融会贯通。罗马非一日炼成的。

name字段就是命令的名字,set就是对相应的命令的参数进行处理的回调函数。



#user  nobody;
worker_processes  1;


master_process  off;  
daemon  off;  


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




http {
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;
        }


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;


    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_timeout  5m;


    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}


会发现,命令的name是和配置文件中的命令是一一对应的。


### 如何为 Nginx 编写自定义模块 #### 1. 准备工作 为了编写 Nginx 的自定义模块,开发者需要熟悉 C 或者 C++ 编程语言以及 Nginx 的源码结构。Nginx 是用 C 语言编写的高性能 Web 服务器软件,因此其模块也主要基于 C 实现。 在开始之前,确保已经安装了 Nginx 源代码包,并具备基本的 Linux 开发环境[^1]。 --- #### 2. 创建自定义模块目录和文件 创建一个新的目录用于存放自定义模块的相关文件。例如: ```bash mkdir addition_module cd addition_module ``` 在这个目录中放置模块的核心逻辑 `.c` 文件,并创建一个 `config` 文件来描述该模块的信息。以下是两个必要的文件内容示例[^4]: - **`.c` 文件 (假设名为 `ngx_http_hello_module.c`)** ```c #include <nginx.h> #include <ngx_core.h> #include <ngx_http.h> static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r); static ngx_command_t ngx_http_hello_commands[] = { { ngx_string("hello_world"), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_conf_set_flag_slot, NGX_HTTP_LOC_CONF_OFFSET, offsetof(ngx_http_hello_loc_conf_t, enable), NULL }, ngx_null_command }; typedef struct { ngx_flag_t enable; } ngx_http_hello_loc_conf_t; static ngx_http_module_t ngx_http_hello_module_ctx = { NULL, /* preconfiguration */ NULL, /* postconfiguration */ NULL, /* create main configuration */ NULL, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ ngx_http_hello_create_loc_conf,/* create location configration */ ngx_http_hello_merge_loc_conf /* merge location configration */ }; ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_http_hello_module_ctx, /* module context */ ngx_http_hello_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 }; static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) { if (!(r->method & NGX_HTTP_GET)) { return NGX_HTTP_NOT_ALLOWED; } ngx_str_t types = ngx_string("text/plain"); r->headers_out.content_type = types; ngx_buf_t* b; ngx_chain_t out; b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); if (b == NULL) { return NGX_HTTP_INTERNAL_SERVER_ERROR; } b->pos = (u_char *) "Hello World"; b->last = (u_char *) "Hello World" + sizeof("Hello World") - 1; b->memory = 1; b->last_buf = 1; out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); } static void* ngx_http_hello_create_loc_conf(ngx_conf_t *cf) { ngx_http_hello_loc_conf_t *conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t)); conf->enable = NGX_CONF_UNSET; return conf; } static char* ngx_http_hello_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) { ngx_http_hello_loc_conf_t *prev = parent; ngx_http_hello_loc_conf_t *conf = child; ngx_conf_merge_value(conf->enable, prev->enable, 0); return NGX_CONF_OK; } ``` - **`config` 文件** ```bash ngx_addon_name=ngx_http_hello_module HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module" NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c" ``` 上述代码片段展示了如何通过处理请求并返回简单的字符串响应来构建一个基础的 HTTP 处理器模块。 --- #### 3. 修改 Nginx 构建脚本 为了让 Nginx 能够识别新添加的模块,在运行 configure 脚本时需指定路径参数。具体操作如下所示[^3]: ```bash ./configure --add-module=/path/to/addition_module make sudo make install ``` 此过程会将新的模块集成至现有的 Nginx 安装环境中,并更新二进制可执行程序以支持新增功能。 --- #### 4. 测试模块 完成以上步骤之后,可以通过编辑默认站点配置文件 `/etc/nginx/nginx.conf` 来启用刚加入的功能特性。例如设置某个 URI 下触发特定行为: ```nginx http { ... server { listen 80; server_name localhost; location /test { hello_world on; # 假设这是刚才实现的一个指令名称 } } } ``` 最后重启服务验证效果即可。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值