阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx版本的“helloworld”

123次阅读
没有评论

共计 3109 个字符,预计需要花费 8 分钟才能阅读完成。

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);
}
static char *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”。

更多 Nginx 相关教程见以下内容

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2015-08/121857.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3109字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中