Nginx 모듈 개발

4271 단어 nginx
C 언어 로 ngxhttp_hello_module. c 파일
 
/*
 * ngx_http_hello_module.c
 *
 *  Created on: Apr 25, 2015
 *      Author: lizhenbin
 */
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>

//  nginx      ,  hello,      
static char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);

//   hello       
static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r);

//     ngx_command_t  ,      hello    
//     ngx_http_hello     
static ngx_command_t ngx_http_hello_commands[] = {
		{
				ngx_string("hello"),
				NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
				ngx_http_hello,
				NGX_HTTP_LOC_CONF_OFFSET,
				0,
				NULL
		},
		ngx_null_command
};

//     HTTP      ,          
static ngx_http_module_t ngx_http_hello_module_ctx = {
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL
};

//   http hello  
ngx_module_t ngx_http_hello_module = {
		NGX_MODULE_V1,
		&ngx_http_hello_module_ctx,
		ngx_http_hello_commands,
		NGX_HTTP_MODULE,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL,
		NGX_MODULE_V1_PADDING
};

static char *ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
	ngx_http_core_loc_conf_t *clcf;

	//    hello         
	clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

	// HTTP   NGX_HTTP_CONTENT_PHASE   ,    hell             
	//    ngx_http_hello_handler  
	clcf->handler = ngx_http_hello_handler;

	return NGX_CONF_OK;
};

static ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r) {

	//    GET  HEAD  ,    504
	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 world, this is my first http module.");
	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_length_n = response.len;
	r->headers_out.content_type = type;

	//   HTTP  
	rc = ngx_http_send_header(r);
	//       ,  
	if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
		return rc;
	}

	//   ngx_buf_t   ,      
	ngx_buf_t *b;
	b = ngx_create_temp_buf(r->pool, response.len);
	if (b == NULL) {
		// 500  
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	//       ngx_buf_t 
	ngx_memcpy(b->pos, response.data, response.len);

	//   last  
	b->last = b->pos + response.len;
	//           
	b->last_buf = 1;

	//       ngx_chain_t   
	ngx_chain_t out;
	out.buf = b;
	out.next = NULL;

	return ngx_http_output_filter(r, &out);
};




그리고 config 파일 을 만 듭 니 다. 파일 은 위 와 같은 디 렉 터 리 아래 에 있 습 니 다.
 
 
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"

마지막 으로, linux 에서 루트 로 전환 하 는 세 개의 명령 을 실행 하고, 세 개의 명령 을 실행 합 니 다
./configure --prefix=/usr/nginx/ --add-module=/home/lizhenbin/workspace/nginx-1.7.12/src/module
make
make install

  
마지막 으로 / usr / nginx / conf 다음 위치 설정
/usr/nginx/conf

vim nginx.conf

location /hello {
              hello;
         }

/usr/nginx/sbin
./nginx

 
Liux 의 iptables 를 끄 고 nginx 는 기본적으로 80 포트 를 사용 합 니 다.
마지막 으로 입력 하면 IP 주소 로 접근 하면 됩 니 다.
http://127.0.0.1/hello

좋은 웹페이지 즐겨찾기