第一次接触nginx。一点一点来,持续学习。
一、安装与启动:
1、下载源代码(官方网站) 下载linux版本的源码。我下载的是nginx1.4.3版本。我的系统版本是ubuntu10.04。
2、放在某个位置,然后编译它。
- ./configure --prefix=/usr/local/nginx(指定安装部署后的根目录) --add-module=/home/my_nginx(新模块存放目录)
- make
- sudo make install
编译过程可能提示没有安装PCRE库,这时候需要提前装一下这个库。
3、检查是否安装成功,测试和启动nginx。
每次修改完配置文件,最好测试一下是否正确。
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
如果成功的话会提示成功信息。
启动:
/usr/local/nginx/sbin/nginx
二、配置文件:
在/usr/local/nginx/conf/nginx.conf
是nginx的配置文件。该文件以block的形式组织,并且有若干个层级,层级内部有自己的指令。最外的层级叫做main层级。worker_processes是属于一个main层级指令,它指定Nginx服务的Worker进程数量。在main层级下可以有event、http等层级,而http中又会有server的block,server的block中可以包含location的block。
三、Nginx原理简述
当Nginx接到一个HTTP请求时,它仅仅是通过查找配置文件将此次请求映射到一个location的block,而此location中所配置的各个指令则会启动不同的模块去完成工作,因此模块可以看做真正的工作者。通常一个location中的指令会涉及一个handler模块和多个filter模块。handler模块负责处理请求,完成响应内容的生成,而filter模块对响应内容进行处理。因此Nginx模块开发分为handler开发和filter开发。
四、Nginx自定义模块开发初探
1、为自定义模块撰写配置文件
按照规则填写,配置文件中最好不要乱加空格。
ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"
2、编写自定义模块
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
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_null_command
};
//模块上下文
static ngx_http_module_t ngx_http_mytest_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
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 type = ngx_string("text/plain");
ngx_str_t response = ngx_string("Hello, this is nginx world!");
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = response.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, response.len);
if(b == NULL)
return NGX_HTTP_INTERNAL_SERVER_ERROR;
ngx_memcpy(b->pos, response.data, response.len);
b->last = b->pos + response.len;
b->last_buf = 1;
ngx_chain_t out; //构造输出链表
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);
}
3、重新编译nginx的源代码
记得编译配置中加入附加模块位置
4、在nginx配置文件中加入自定义命令
在http block中的server block中加入一个新的location block。
location /mytest {
mytest;
}
5、检测配置文件是否正确,重启nginx
如果nginx重复启动,可以用下面的命令关闭nginx
killall -9 nginx
这样,新的模块就完成了!访问localhost/mytest就可以得到设计好的响应了!