>  기사  >  백엔드 개발  >  "helloworld"의 Nginx 버전

"helloworld"의 Nginx 버전

WBOY
WBOY원래의
2016-08-10 08:48:381588검색

Nginx 모듈 개요

Nginx 모듈은 Apache처럼 동적으로 추가할 수 없습니다. 모든 모듈은 Nginx 바이너리 실행 파일로 사전 컴파일되어야 합니다.
Nginx 모듈에는 세 가지 역할이 있습니다:
(1) 핸들러(처리 모듈) - HTTP 요청을 처리하고 콘텐츠를 출력하는 데 사용됩니다.
(2) 필터(필터 모듈) - 헤더가 출력하는 콘텐츠를 필터링하는 데 사용됩니다.
(3) 로드 밸런서(로드 밸런싱 모듈) – 선택할 서버가 여러 개인 경우 백엔드 서버를 선택하고 해당 서버로 HTTP 요청을 전달합니다.

Hello world 모듈 작성 및 설치

(1) 다음 명령을 실행하여 이 디렉터리에 Nginx 모듈을 작성합니다.
mkdir -p /opt/nginx_hello_world
cd / opt /nginx_hello_world
(2) nginx 모듈에 필요한 구성 파일(config라는 이름) 만들기 시작
vim /opt/nginx_hello_world
그런 다음 다음 내용을 입력하여 저장하고 종료합니다.

<code>ngx_sdd
HTTP_MODULES=<span>"<span>$HTTP_MODULES</span> ngx_http_hello_world_module"</span>
NGX_ADD>"<span>$NGX_ADDON_SRCS</span><span>$ngx_addon_dir</span>/ngx_http_hello_world_module.c"
CORE_LIBS=<span>"<span>$CORE_LIBS</span> -lpcre"</span></code>

(3) Nginx 모듈 c 프로그램 파일을 생성합니다(형식은 "ngx_http_module name_module.c", 이 경우: ngx_http_hello_world_module.c)
vim /opt/nginx_hello_world/ngx_http_hello_world_module.c

<code><span>#include <ngx_config.h></span><span>#include<ngx_core.h></span><span>#include<ngx_http.h></span><span>static</span><span>char</span> *ngx_http_hello_world(ngx_conf_t *cf,ngx_command_t *cmd,<span>void</span> *conf);

<span>static</span> ngx_command_t ngx_http_hello_world_commands[]={
{
ngx_string(<span>"hello_world"</span>),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hello_world,
<span>0</span>,
<span>0</span>,
<span>NULL</span>
},
ngx_null_command
};

<span>static</span> u_char ngx_hello_world[]=<span>"hello world"</span>;
<span>static</span> ngx_http_module_t ngx_http_hello_world_module_ctx ={
<span>NULL</span>,
<span>NULL</span>,

<span>NULL</span>,
<span>NULL</span>,

<span>NULL</span>,
<span>NULL</span>,

<span>NULL</span>,
<span>NULL</span>
};
ngx_module_t ngx_http_hello_world_module ={
NGX_MODULE_V1,
&ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
<span>NULL</span>,
<span>NULL</span>,
<span>NULL</span>,
<span>NULL</span>,
<span>NULL</span>,
<span>NULL</span>,
<span>NULL</span>,
NGX_MODULE_V1_PADDING
};

<span>static</span> ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;

r->headers_out<span>.content_type</span><span>.len</span> = <span>sizeof</span>(<span>"text/plain"</span>) - <span>1</span>;
r->headers_out<span>.content_type</span><span>.data</span> = (u_char *)<span>"text/plain"</span> ;

b= ngx_pcalloc(r->pool,<span>sizeof</span>(ngx_buf_t));

out<span>.buf</span> =b;
out<span>.next</span> =<span>NULL</span>;

b->pos=ngx_hello_world;
b->last =ngx_hello_world +<span>sizeof</span>(ngx_hello_world);
b->memory =<span>1</span>;
b->last_buf =<span>1</span>;

r->headers_out<span>.status</span> = NGX_HTTP_OK;
r->headers_out<span>.content_length_n</span> =<span>sizeof</span>(ngx_hello_world);
ngx_http_send_header(r);

<span>return</span> ngx_http_output_filter(r,&out);
}
<span>static</span><span>char</span> *ngx_http_hello_world(ngx_conf_t *cf,ngx_command_t *cmd, <span>void</span> *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_hello_world_handler;
<span>return</span> NGX_CONF_OK;

}</code>

(4) 이 단계에서는 Nginx 설치 블로그가 약간 다릅니다
**./configure –prefix=/usr/local/nginx –add-module=/opt/nginx_hello_world
make&&make install**
(5) nginx.conf(/usr/local/nginx/conf/nginx.conf)를 구성하고 서버 섹션에 다음 내용을 추가합니다.
**위치 = /안녕하세요{
안녕하세요_세계
}**
(6) Nginx를 시작하고(Nginx 시작) 브라우저를 사용하여 http://localhost/hello에 액세스하면 작성된 Nginx Hello World 모듈에서 출력되는 "hello world" 텍스트를 볼 수 있습니다.

다음 글에 코드 분석 작성

저작권 안내: 이 글은 블로거의 원본 글이므로 블로거의 허락 없이 복제할 수 없습니다.

위 내용은 Nginx 버전의 "helloworld"를 다양한 측면에서 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.