Heim  >  Artikel  >  Backend-Entwicklung  >  Nginx版本的“helloworld”

Nginx版本的“helloworld”

WBOY
WBOYOriginal
2016-08-10 08:48:381588Durchsuche

Nginx模块概述

Nginx的模块不能够像Apache那样动态添加,所有的模块都要预先编译进Nginx的二进制可执行文件中。
Nginx模块有三种角色:
(1)Handlers(处理模块)–用于处理HTTP请求并输出内容。
(2)Filters(过滤模块)–用于过滤Headler输出的内容。
(3)Load-balancers(负载均衡模块)–当有多台服务器供选择时,选择一台后端服务器并将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.c”,本例中为:ngx_http_hello_world_module.c)
vim /opt/nginx_hello_world/ngx_http_hello_world_module.c

<code><span>#include <ngx_config.h></ngx_config.h></span><span>#include<ngx_core.h></ngx_core.h></span><span>#include<ngx_http.h></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安装那一篇Nginx安装博客在这一步稍有不同
**./configure –prefix=/usr/local/nginx –add-module=/opt/nginx_hello_world
make&&make install**
(5)配置nginx.conf(/usr/local/nginx/conf/nginx.conf),在server部分增加以下内容:
**location = /hello{
hello_world;
}**
(6)启动Nginx,(Nginx的启动),用浏览器访问http://localhost/hello,就可以看到编写的Nginx Hello World 模块输出的文字“hello world”。

下篇写代码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Nginx版本的“helloworld”,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Laravel 5文档阅读摘要Nächster Artikel:最大子序列和算法分析