您的位置:首页 > 运维架构 > Nginx

nginx学习笔记二(nginx的配置)

2016-05-03 16:04 609 查看
在阅读《深入理解nginx》的时候,对于nginx的配置一直有些困惑。参阅了官方给的nginx users guide之后,将我对nginx配置的理解记录如下:

一..配置文件是用来干啥的

我们知道Nginx是由多个模块所构成的,在一个正常运行的Nginx系统中,会存在着http 模块,事件模块等工作模块。每个模块都发挥着自己的作用。而配置文件nginx.conf就是用来配置这些模块的工作参数,比如http块可以配置http模块的工作方式,events块可以配置events模块的工作参数。

二..http模块的配置说明

静态web服务器的主要功能由ngx_http_core_module模块(也是一个http模块)实现,所以主要是要在配置文件中编写http模块。

一个http配置块中比较重要的字块有server,location。

一个server对应一个虚拟主机,它只处理与之相对应的主机域名请求。location会根据用户请求中的/uri表达式来寻找匹配块,然后依据匹配块中的配置来处理该用户请求。

假设配置如下:

server {

server_name: localhost
location / {
root /data/www;
}

location /images/ {
root /data;
}
}


那么,对于http://localhost/images/example.png这样的请求,会匹配到localhost这样的server_name。之后又会匹配到/images/这个uri,因为location中定义了root /data,这里的root指的应该是资源在主机中的位置。所以,nginx server会取得主机中的/data/images/examples.png,然后发送给client。

三.带参数的http配置项使用

配置项是用来配置http模块的工作参数。在上一节的例子中我们看到在location块中添加了mytest配置项。这个配置项是不带参数的。但是有时候配置项是需要带参数的,我们需要做一些额外的工作,以使得在解析函数中可以获取到相应配置项的参数。下面主要讲述如何获取得到相应配置项的参数:

1.定义用于保存配置参数的结构体

最常见的场景是在location块中定义多个带参数的配置项,如下:

location /test {
test_flag on;
test_str apple;
mytest;
}


这里的test_flag带有的是bool型的参数,test_str带有的是str类型的参数。所以对于本Location配置块,我们需要自己设计一个结构体来存储感兴趣的配置项参数:

typedef struct{
ngx_str_t my_str;
ngx_flag_t my_flag;
}ngx_http_mytest_conf_t;


2.实现ngx_http_module_t结构体中的回调函数方法

create_main_conf, create_srv_conf, create_loc_conf 这三个方法负责把我们分配的用于保存配置项的结构体传递给HTTP框架。我们得按需实现这三个回调函数,来为我们设计的参数结构体分配空间和进行初始化等等。如下:

static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf){
ngx_http_mytest_conf_t *mycf;
mycf = (ngx_http_mytest_conf_t *) ngx_pcalloc(cf->pool,sizeof(ngx_http_mytest_conf_t);
if(mycf==NULL) return NULL;

mycf->my_flag = NGX_CONF_UNSET;
mycf->my_str.len=0;
mycf->my_str.data=NULL;
}


3.设定配置项的解析方式
这个就和上一节介绍的没有什么区别了。对于每一个配置项,都需要实现ngx_command_s来设定该配置项的解析形式。当然,nginx自身提供了14种默认的解析函数,利用这些函数可以降低开发的工作量。

//处理配置项

static ngx_command_t ngx_http_mytest_commands[] = {

{

ngx_string("mytest"),

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,

ngx_http_mytest,

NGX_HTTP_LOC_CONF_OFFSET,

0,

NULL

},
{

ngx_string("test_flag"),

NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,

ngx_conf_set_flag_slot,

NGX_HTTP_LOC_CONF_OFFSET,

offsetof(ngx_http_mytest_conf_t, my_flag),

NULL

},
{

ngx_string("test_str"),

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,

ngx_conf_set_str_slot,

NGX_HTTP_LOC_CONF_OFFSET,

offsetof(ngx_http_mytest_conf_t,my_str),

NULL

},

ngx_null_command

};


最后整个模块定义源码:
#include <ngx_config.h>

#include <ngx_core.h>

#include <ngx_http.h>

//data structure for storing config parameters
typedef struct{ ngx_str_t my_str; ngx_flag_t my_flag; }ngx_http_mytest_conf_t;
static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf);

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);

static char *

ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

//处理配置项

static ngx_command_t ngx_http_mytest_commands[] = {

{

ngx_string("mytest"),

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,

ngx_http_mytest,

NGX_HTTP_LOC_CONF_OFFSET,

0,

NULL

},
{

ngx_string("test_flag"),

NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,

ngx_conf_set_flag_slot,

NGX_HTTP_LOC_CONF_OFFSET,

offsetof(ngx_http_mytest_conf_t, my_flag),

NULL

},
{

ngx_string("test_str"),

NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,

ngx_conf_set_str_slot,

NGX_HTTP_LOC_CONF_OFFSET,

offsetof(ngx_http_mytest_conf_t,my_str),

NULL

},

ngx_null_command

};

//模块上下文

static ngx_http_module_t ngx_http_mytest_module_ctx = {

NULL,

NULL,

NULL,

NULL,

NULL,

NULL,

ngx_http_mytest_create_loc_conf,

NULL

};

//新模块定义

ngx_module_t ngx_http_mytest_module = {

NGX_MODULE_V1,

&ngx_http_mytest_module_ctx,

ngx_http_mytest_commands,

NGX_HTTP_MODULE,

NULL,

NULL,

NULL,

NULL,

NULL,

NULL,

NULL,

NGX_MODULE_V1_PADDING

};

//配置项对应的回调函数

static char *

ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)

{

ngx_http_core_loc_conf_t *clcf;

clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

clcf->handler = ngx_http_mytest_handler;

return NGX_CONF_OK;

}

//实际完成处理的回调函数

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)

{

if (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) {

return NGX_HTTP_NOT_ALLOWED;

}

ngx_int_t rc = ngx_http_discard_request_body(r);

if (rc != NGX_OK) {

return rc;

}
ngx_str_t str_format = ngx_string("test_str=%V,test_flag=%i");

ngx_http_mytest_conf_t * mycf;//存储配置项参数的结构体
mycf = ngx_http_get_module_loc_conf(r,ngx_http_mytest_module);
ngx_str_t my_str = mycf-> my_str;
ngx_flag_t my_flag = mycf->my_flag;
int data_len = str_format.len + my_str.len+1;

ngx_str_t type = ngx_string("text/plain");

ngx_str_t response = ngx_string("Hello World");

r->headers_out.status = NGX_HTTP_OK;

r->headers_out.content_length_n = data_len;

r->headers_out.content_type = type;

rc = ngx_http_send_header(r);

if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {

return rc;

}

ngx_buf_t *b;

b = ngx_create_temp_buf(r->pool, data_len);

if (b == NULL) {

return NGX_HTTP_INTERNAL_SERVER_ERROR;

}

// ngx_memcpy(b->pos, response.data, response.len);
ngx_snprintf(b->pos,data_len,(char *)str_format.data,&test_str,test_flag);

b->last = b->pos + data_len;

b->last_buf = 1;

ngx_chain_t out;

out.buf = b;

out.next = NULL;

return ngx_http_output_filter(r, &out);

}

static void* ngx_http_mytest_create_loc_conf(ngx_conf_t *cf){ ngx_http_mytest_conf_t *mycf; mycf = (ngx_http_mytest_conf_t *) ngx_pcalloc(cf->pool,sizeof(ngx_http_mytest_conf_t); if(mycf==NULL) return NULL; mycf->my_flag = NGX_CONF_UNSET; mycf->my_str.len=0; mycf->my_str.data=NULL; }


测试结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: