Nginx 설치 설정 과 사용자 정의 모듈 Nginx 첫 번 째 편 추가

4655 단어 nginxlinux
nginx 를 처음 접 하 다.조금씩 와 서 계속 공부 하 세 요.
1. 설치 및 시작:
1. 소스 코드 (공식 사이트) 를 다운로드 하여 Liux 버 전의 소스 코드 를 다운로드 합 니 다.내 가 다운로드 한 것 은 nginx 1.4.3 버 전이 다.제 시스템 버 전 은 ubuntu 10.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

4. 567913. 성공 하면 성공 정 보 를 알려 줍 니 다.
시작:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

2. 프로필:
/ usr / local / nginx / conf / nginx. conf
nginx 프로필 입 니 다.이 파일 은 block 형식 으로 구성 되 고 몇 개의 등급 이 있 으 며 등급 내부 에 자신의 명령 이 있 습 니 다.가장 바깥 의 등급 을 main 등급 이 라 고 한다.worker_processes 는 main 계층 명령 에 속 합 니 다. Nginx 서비스의 Worker 프로 세 스 수 를 지정 합 니 다.main 등급 아래 에 이벤트, http 등 등급 이 있 을 수 있 고 http 에는 server 의 block 이 있 으 며 server 의 block 에는 location 의 block 이 포함 되 어 있 습 니 다.
일반적으로 server 의 block 은 host 를 표시 하고 그 중의 한 location 은 하나의 경로 맵 규칙 을 대표 합 니 다. 이 두 block 은 HTTP 설정 의 핵심 이 라 고 할 수 있 습 니 다.
3. Nginx 원리 약술
Nginx 가 HTTP 요청 을 받 았 을 때 설정 파일 을 찾 아서 이번 요청 을 location 의 block 에 비 추 는 것 일 뿐 이 며, 이 location 에 설 치 된 각 명령 은 서로 다른 모듈 을 시작 하여 작업 을 수행 하기 때문에 모듈 은 실제 작업 자로 볼 수 있 습 니 다.보통 하나의 location 의 명령 은 handler 모듈 과 여러 개의 filter 모듈 과 관련된다.handler 모듈 은 요청 을 처리 하고 응답 내용 의 생 성 을 완성 하 며 filter 모듈 은 응답 내용 을 처리 합 니 다.따라서 Nginx 모듈 개발 은 handler 개발 과 filter 개발 로 나 뉜 다.
4. Nginx 사용자 정의 모듈 개발 초기 분석
1. 사용자 정의 모듈 에 프로필 작성
규칙 에 따라 작성 하고 프로필 에 빈 칸 을 함부로 넣 지 않 는 것 이 좋 습 니 다.
/usr/local/nginx/sbin/nginx

2. 사용자 정의 모듈 작성
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"

3. nginx 의 소스 코드 를 다시 컴 파일 합 니 다.
컴 파일 설정 에 추가 모듈 위 치 를 추가 하 는 것 을 기억 하 십시오.
4. nginx 프로필 에 사용자 정의 명령 추가
http block 의 server block 에 새로운 location block 을 추가 합 니 다.
#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);
}

5. 설정 파일 이 올 바른 지 확인 하고 nginx 를 다시 시작 합 니 다.
nginx 가 반복 적 으로 시작 하면 아래 명령 으로 nginx 를 닫 을 수 있 습 니 다.
location /mytest {
    mytest;
}

이렇게 해서 새로운 모듈 이 완성 되 었 습 니 다!localhost / my test 를 방문 하면 디자인 된 응답 을 받 을 수 있 습 니 다!

좋은 웹페이지 즐겨찾기