Nginx版本的“helloworld”
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Nginx版本的“helloworld”,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2921字,纯文字阅读大概需要5分钟。
内容图文
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
然后输入以下内容保存并退出:
ngx_sddon_name=nginx_http_hello_world_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS$ngx_addon_dir/ngx_http_hello_world_module.c"
CORE_LIBS="$CORE_LIBS -lpcre"
(3)创建Nginx的模块c程序文件(格式为“ngx_http_模块名称_module.c”,本例中为:ngx_http_hello_world_module.c)
vim /opt/nginx_hello_world/ngx_http_hello_world_module.c
#include <ngx_config.h>
#include<ngx_core.h>
#include<ngx_http.h>
static
char *ngx_http_hello_world(ngx_conf_t *cf,ngx_command_t *cmd,void *conf);
static ngx_command_t ngx_http_hello_world_commands[]={
{
ngx_string("hello_world"),
NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
ngx_http_hello_world,
0,
0,
NULL
},
ngx_null_command
};
static u_char ngx_hello_world[]="hello world";
static ngx_http_module_t ngx_http_hello_world_module_ctx ={
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
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,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r)
{
ngx_buf_t *b;
ngx_chain_t out;
r->headers_out.content_type.len = sizeof("text/plain") - 1;
r->headers_out.content_type.data = (u_char *)"text/plain" ;
b= ngx_pcalloc(r->pool,sizeof(ngx_buf_t));
out.buf =b;
out.next =NULL;
b->pos=ngx_hello_world;
b->last =ngx_hello_world +sizeof(ngx_hello_world);
b->memory =1;
b->last_buf =1;
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n =sizeof(ngx_hello_world);
ngx_http_send_header(r);
return ngx_http_output_filter(r,&out);
}
staticchar *ngx_http_hello_world(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_hello_world_handler;
return NGX_CONF_OK;
}
(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”。
下篇写代码分析
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/zhangli401x/article/details/47336551
内容总结
以上是互联网集市为您收集整理的Nginx版本的“helloworld”全部内容,希望文章能够帮你解决Nginx版本的“helloworld”所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。