龙空技术网

Nginx基础入门005

ailx10 83

前言:

现在我们对“nginx解析中文文件名”大约比较着重,朋友们都想要了解一些“nginx解析中文文件名”的相关内容。那么小编同时在网络上收集了一些对于“nginx解析中文文件名””的相关内容,希望同学们能喜欢,姐妹们快快来了解一下吧!

总是有人要赢,为什么不能是你,同样都是人,为什么要比别人差。

上一篇:ailx10:Nginx基础入门004 ;下一篇:ailx10:Nginx基础入门006

ailx10

网络安全优秀回答者

网络安全硕士

如何把自己的HTTP模块编译进Nginx?

Nginx提供了一种简单的方式,将第三方模块编译到Nginx中:

首先把源代码文件全部放在一个目录下然后在该目录中编写一个config文件用于通知Nginx如何编译本模块再在configure脚本执行时加入参数--add-module=PATH(代码目录)执行正常编译安装流程

1 config文件怎么写?

可执行的shell脚本,需要定义3个变量:

ngx_addon_name:仅在configure执行时使用,一般设置为模块名称HTTP_MODULES:保存所有的HTTP模块名称,每个HTTP模块间加空格NGX_ADDON_SRCS:用于指定新增模块的源代码,多个代码间加空格

2 如何利用configure脚本将定制的模块加入到Nginx中?

auto/options脚本中,设置NGX_ADDONS变量

--add-module=*)                  NGX_ADDONS="$NGX_ADDONS $value" ;;

auto/modules脚本中,加入定制的第三方模块

if test -n "$NGX_ADDONS"; then    echo configuring additional modules    for ngx_addon_dir in $NGX_ADDONS    do        echo "adding module in $ngx_addon_dir"        if test -f $ngx_addon_dir/config; then            . $ngx_addon_dir/config            echo " + $ngx_addon_name was configured"        else            echo "$0: error: no $ngx_addon_dir/config was found"            exit 1        fi    donefi

之后定义ngx_module_t* ngx_modules[]数组,这个数组存储了Nginx中所有模块:

modules="$CORE_MODULES $EVENT_MODULES"if [ $HTTP = YES ]; then    modules="$modules $HTTP_MODULES \             $HTTP_FILTER_MODULES \             $HTTP_HEADERS_FILTER_MODULE \             $HTTP_AUX_FILTER_MODULES \             $HTTP_COPY_FILTER_MODULE \             $HTTP_RANGE_BODY_FILTER_MODULE \             $HTTP_NOT_MODIFIED_FILTER_MODULE"    NGX_ADDON_DEPS="$NGX_ADDON_DEPS \$(HTTP_DEPS)"fi

$HTTP_MODULES模块已经在config文件中重定义了。

3 如何直接修改Makefile文件?

最好不要用这种方式!

在执行完configure后,对生成的objs/ngx_modules.c和objs/Makefile文件直接修改:

第一步:修改objs/ngx_modules.c

extern ngx_module_t ngx_http_ailx10_module;ngx_module_t* ngx_modules[] = {  ...  &ngx_http_ailx10_module,  ...  NULL;};

第二步:修改objs/Makefile

# 编译objs/addon/httpmodule/ngx_http_ailx10_module.o: $(ADDON_DEPS) \  ../sample/httpmodule//ngx_http_ailx10_module.c  $(CC) -c $(CFLAGS) $(ALL_INCS) \    -o objs/addon/httpmodule/ngx_http_ailx10_module.o \    ../sample/httpmodule//ngx_http_ailx10_module.c#链接objs/nginx: objs/src/core/nginx.o \  ...  objs/addon/httpmodule/ngx_http_ailx10_module.o \  objs/ngx_modules.o  $(LINK) -o objs/nginx \    objs/src/core/nginx.o \    ...    objs/addon/httpmodule/ngx_http_ailx10_module.o \    objs/ngx_modules.o \    -lpthread -lcrypt -lpcre -lcrypto -lz

HTTP模块的数据结构!

定义一个自己的HTTP模块:

ngx_module_t ngx_http_ailx10_module;

ngx_module_t 是一个Nginx模块的数据结构

typedef struct ngx_module_s ngx_module_t;struct ngx_module_s {  #define NGX_MODULE_V1 0,0,0,0,0,0,1  ngx_uint_t ctx_index;// ctx_index表示当前模块在这类模块中的序号  ngx_uint_t index;// index表示当前模块在ngx_modules数组中的序号,也就是在所有模块中的序号  /* spare系列的保留变量,暂未使用 */  ngx_uint_t spare0;  ngx_uint_t spare1;  ngx_uint_t spare2;  ngx_uint_t spare3;  ngx_unit_t version;//模块的版本  void *ctx;// ctx用于指向一类模块的上下文结构体  // 在HTTP模块中,ctx需要指向ngx_http_module_t结构体  // 在event模块中,指向ngx_event_module_t结构体  ngx_command_t *commands;//commands将处理nginx.conf中的配置项  ngx_uint_t type;// type字段决定了该模块的模块类型  ngx_int_t (*init_master)(ngx_log_t *log);//实际上没用  /*init_module回调方法在初始化所有模块时被调用。在master/worker模式下,这个阶段将在启动worker子进程前完成*/  ngx_int_t (*init_module)(ngx_cyle_t *cycle);/* init_process回调方法在正常服务前被调用。在master/worker模式下,多个worker子进程已经产生,在每个worker进程    的初始化过程会调用所有模块的init_process函数*/  ngx_int_t (*init_process)(ngx_cycle_t *cycle);  ngx_int_t (*init_thread)(ngx_cycle_t *cycle); //实际上没用  void (*exit_thread)(ngx_cycle_t *cycle);//实际上没用  void (*exit_process)(ngx_cycle_t *cycle);  void (*exit_master)(ngx_cycle_t *cycle); /*以下8个spare_hook变量也是保留字段,目前没有使用 */  uintptr_t             spare_hook0;  uintptr_t             spare_hook1;  uintptr_t             spare_hook2;  uintptr_t             spare_hook3;  uintptr_t             spare_hook4;  uintptr_t             spare_hook5;  uintptr_t             spare_hook6;  uintptr_t             spare_hook7;}

自定义HTTP模块时,务必把type字段设为NGX_HTTP_MODULE

init_master、 init_module、 init_process、 init_thread 这4个回调函数与Nginx框架有关,与HTTP框架无关。即使没有http{}配置项,这些回调函数依然会执行,通常开发HTTP模块时,都把它们设置为NULL。

定义HTTP模块时,最重要的是设置ctxcommands这2个成员。对于HTTP模块来说,ngx_module_t中的ctx指针必须指向ngx_http_module_t接口。

下面是重头戏:ngx_http_module_t接口:

HTTP框架在读取和重载配置文件的8阶段:

typedef struct {  ngx_int_t   (*preconfiguration)(ngx_conf_t *cf); //解析配置文件前调用  ngx_int_t   (*postconfiguration)(ngx_conf_t *cf); //完成配置文件的解析后调用  void       *(*create_main_conf)(ngx_conf_t *cf);  char       *(*init_main_conf)(ngx_conf_t *cf, void *conf);//常用于初始化main级别配置项  void       *(*create_srv_conf)(ngx_conf_t *cf);  char       *(*merge_srv_conf)(ngx_conf_t *cf, void *prev, void *conf);  void       *(*create_loc_conf)(ngx_conf_t *cf);  char       *(*merge_loc_conf)(ngx_conf_t *cf, void *prev, void *conf);} ngx_http_module_t;

HTTP框架调用这些回调的实际顺序可能是:

1.create_main_conf2.create_srv_conf3.create_loc_conf4.preconfiguration5.init_main_conf6.merge_srv_conf7.merge_loc_conf8.postconfiguration

再来看看这个commands指针,指向一个以ngx_null_command结尾的数组,数组的每个元素都是ngx_command_t类型。Nginx在解析配置文件中的一个配置项的时候,首先会遍历所有模块,对于每一个模块来说,就是通过遍历commands数组进行的,当检查到ngx_null_command的时候,就会停止用当前模块解析当前的配置项,每个ngx_command_t 结构体定义了自己感兴趣的一个配置项:

#define ngx_null_command  { ngx_null_string, 0, NULL, 0, 0, NULL }typedef struct ngx_command_s ngx_command_t;struct ngx_command_s {  ngx_str_t             name;//配置项名称,如"gzip"  ngx_uint_t            type; //配置项可以出现的位置,取值可能为NGX_HTTP_LOC_CONF  | NGX_HTTP_SRV_CONFI  char               *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); //出现了name中指定的配置项后,将会调用set方法处理配置项的参数  ngx_uint_t            conf;//crate分配内存的时候的偏移量 NGX_HTTP_LOC_CONF_OFFSET NGX_HTTP_SRV_CONF_OFFSET  ngx_uint_t            offset;//通常用于使用预设的解析方法解析配置项  void                 *post;//配置项读取后的处理方法,必须是ngx_conf_post_t结构的指针};

标签: #nginx解析中文文件名