Nginx 모듈 개발
4271 단어 nginx
/*
* 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
간단! Certbot을 사용하여 웹 사이트를 SSL(HTTPS)화하는 방법초보자가 인프라 주위를 정돈하는 것은 매우 어렵습니다. 이번은 사이트를 간단하게 SSL화(HTTP에서 HTTPS통신)로 변경하는 방법을 소개합니다! 이번에는 소프트웨어 시스템 Nginx CentOS7 의 환경에서 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.