nginx除去配置解析和初始化,就是根據請求根據狀態機一步步執行流程,那麼本節主要講解首先是整個nginx對於一個客戶端請求大致請求流程,其次是根據這個流程來寫自己的handler模塊。
nginx接受到一個client請求調用流程:
1 ngx_event_accept主要是accept連接生成並初始化connection
2 ngx_http_init_connection 函數主要設置rev和wev事件回調函數
3 ngx_http_init_request 初始化request
4 ngx_http_process_request_line 處理請求line
5 ngx_http_process_request_headers處理請求header
6 ngx_http_process_request處理請求
7 ngx_http_handler
8 ngx_http_core_run_phases
函數詳細:
void
ngx_http_core_run_phases(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_http_phase_handler_t *ph;
ngx_http_core_main_conf_t *cmcf;
cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
ph = cmcf->phase_engine.handlers;
while (ph[r->phase_handler].checker) {
rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
if (rc == NGX_OK) {
return;
}
}
}
然後遍歷整個請求狀態機的11個階段
序號 |
階段名稱 |
階段描述 |
0 |
NGX_HTTP_POST_READ_PHASE |
請求頭讀取完成後的階段 |
1 |
NGX_HTTP_SERVER_REWRITE_PHASE |
Server內請求地址重寫階段 |
2 |
NGX_HTTP_FIND_CONFIG_PHASE |
配置查找階段 |
3 |
NGX_HTTP_REWRITE_PHASE |
Location內請求地址重寫階段 |
4 |
NGX_HTTP_POST_REWRITE_PHASE |
請求地址重寫完成之後的階段 |
5 |
NGX_HTTP_PREACCESS_PHASE |
訪問權限檢查準備階段 |
6 |
NGX_HTTP_ACCESS_PHASE |
訪問權限檢查階段 |
7 |
NGX_HTTP_POST_ACCESS_PHASE |
訪問權限檢查完後之後的階段 |
8 |
NGX_HTTP_TRY_FILES_PHASE |
配置項try_files處理階段 |
9 |
NGX_HTTP_CONTENT_PHASE |
內容產生階段 |
10 |
NGX_HTTP_LOG_PHASE |
日誌模塊處理階段 |
一般我們寫handler模塊都是在NGX_HTTP_CONTENT_PHASE階段,也就是說我們直接把函數掛在在NGX_HTTP_CONTENT_PHASE就ok
我們自己寫一個nginx handler模塊
config文件
//config 文件
ngx_addon_name=ngx_http_read_module
HTTP_MODULES="$HTTP_MODULES ngx_http_read_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_read_module.c"
/*
* =====================================================================================
*
* Filename: ngx_http_read_module.c
*
* Description: 這個一個讀取一個leek文件返回給用戶
*
* Version: 1.0
* Created: 2013年10月10日 11時06分53秒
* Revision: none
* Compiler: gcc
*
* Author: [email protected]
* Organization:
*
* =====================================================================================
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#define TEST_STRING "This is a static module test string"
typedef struct
{
ngx_str_t name;
}ngx_http_test_loc_t;
ngx_str_t g_buf;
static void* ngx_http_leek_create_loc_conf(ngx_conf_t* cf);
static char* ngx_http_leek_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child);
static ngx_int_t ngx_http_leekdump_handler(ngx_http_request_t *r);
/*
* 這個函數調用創建一個ngx_http_test_loc_t結構,進行初始化成員,然後把這個結構掛在到http->ctx->loc_conf所對應的索引下面
*/
static void* ngx_http_leek_create_loc_conf(ngx_conf_t* cf) {
ngx_http_test_loc_t* conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_test_loc_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->name.len = 0;
conf->name.data = NULL;
return conf;
}
/* 當讀取配置文件並且解析到leek命令時候就會調用這個函數 */
char* setvalue(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_test_loc_t* temp;
struct stat buf;
ngx_conf_set_str_slot(cf, cmd, conf);
temp = (ngx_http_test_loc_t*)conf;
if (access((char*)temp->name.data, F_OK) == -1)
{
return NGX_CONF_ERROR;
}
if (stat((char*)temp->name.data, &buf))
{
return NGX_CONF_ERROR;
}
g_buf.data = ngx_alloc(buf.st_size +1, cf->log);
g_buf.data[buf.st_size] = 0;
g_buf.len = buf.st_size+1;
int fd = open((char*)temp->name.data, O_RDONLY);
if (fd == -1)
{
return NGX_CONF_ERROR;
}
read(fd, g_buf.data, buf.st_size);
return NGX_CONF_OK;
}
/*
* ngx_string(leek) => 定義一個leek命令
* NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1 => 命令只能存在loc作用域中並且帶一個參數(NGX_CONF_TAKE1)
* 回調函數爲setvalue
* 保存在LOC數組中
* 設置name在結構體ngx_http_test_loc_t偏移
*
*/
static ngx_command_t ngx_http_read_commands [] = {
{
ngx_string("leek"),
NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
setvalue,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_test_loc_t, name),
NULL
},
ngx_null_command
};
/*
* 掛在http回調函到階段NGX_HTTP_CONTENT_PHASE
*/
static ngx_int_t ngx_http_read_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_CONTENT_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_leekdump_handler;
return NGX_OK;
}
ngx_http_module_t ngx_http_read_module_ctx = {
NULL, /* preconfiguration */
ngx_http_read_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_leek_create_loc_conf, /* create location configuration */
ngx_http_leek_merge_loc_conf /* merge location configuration */
};
/*
* 模塊定義
*/
ngx_module_t ngx_http_read_module =
{
NGX_MODULE_V1,
&ngx_http_read_module_ctx, /* module context */
ngx_http_read_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
};
/*
* 合併對多個loc進行合併,用戶對用戶沒有設置的loc使用parent loc值
*/
static char* ngx_http_leek_merge_loc_conf(ngx_conf_t* cf, void* parent, void* child) {
ngx_http_test_loc_t* prev = parent;
ngx_http_test_loc_t* conf = child;
ngx_conf_merge_str_value(conf->name, prev->name, "Nginx");
return NGX_CONF_OK;
}
/*
* 回調函數,當用戶訪問leek頁面的時候,就會把命令leek 設置的參數值文件,發給客戶端
* 關注兩個函數
* 1 ngx_http_send_header
* 2 ngx_http_output_filter
*/
static ngx_int_t ngx_http_leekdump_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t* b;
ngx_chain_t out[3];
if (ngx_rstrncasecmp((u_char *)"/leek", r->uri.data, 5) != 0) {
return NGX_DECLINED;
}
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char*)"text/html";
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
out[0].buf = b;
out[0].next = NULL;
b->pos = (u_char*)g_buf.data;
b->last = b->pos + g_buf.len;
b->memory = 1;
b->last_buf = 1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = g_buf.len;
rc = ngx_http_send_header(r);
if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
return rc;
}
return ngx_http_output_filter(r, &out[0]);
}
(END)
nginx.conf配置文件如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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;
}
location /leek {
leek "/root/hunter/nginx/ngx_gdb/conf/mime.types.default";
}
#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;
}
}
}
此時content handler主要功能是當用戶訪問/"leek"頁面的時候,把文件(
/root/hunter/nginx/ngx_gdb/conf/mime.types.default
)返回給用戶瀏覽器客戶端