nginx,hello World!
向nginx中添加第一个最简单的hello world模块
一、编写ngx_http_mytest_module模块
1. ngx_http_mytest_module.c
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> //mytest module define static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r)
{
//不是GET或者HEAD方法,返回405
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 World!");
//设置http状态码
r->headers_out.status = NGX_HTTP_OK;
//设置响应Content-Type
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 = ; //设置响应内容到chain中
ngx_chain_t out;
out.buf = b;
out.next = NULL; return ngx_http_output_filter(r, &out);
} //set handler
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;
} //conf structure
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,
,
NULL
},
ngx_null_command
}; //module ctx
static ngx_http_module_t ngx_http_mytest_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL,NULL, NULL}; //mytest module
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
};
ngx_http_mytest_module.c
2.config
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"
config
二、安装nginx
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx-mytest-module/
三、修改配置文件
location /test {
mytest;
}
四、启动nginx,并通过浏览器访问

五、查看日志文件

倒数第二行对/test的响应。
六、对过程一些问题的总结
1. 执行configure后,objs文件夹下ngx_modules.c文件中只有3部分:
1)extern模块

2)ngx_modules数组


3)ngx_module_names数组

2. nginx自定义模块需要添加的头文件

3. 如果主机不能访问虚拟机web服务器,可能是需要关闭防火墙
iptables -F
iptables -P INPUT ACCEPT
nginx,hello World!的更多相关文章
- 看完这篇还不了解 Nginx,那我就哭了!
作者:蔷薇Nina www.cnblogs.com/wcwnina/p/8728391.html 想必大家一定听说过 Nginx,若没听说过它,那么一定听过它的"同行"Apache ...
- 就是要让你搞懂Nginx,这篇就够了!
开源Linux 长按二维码加关注~ 作者:渐暖° 出处:blog.csdn.net/yujing1314/article/details/107000737 来源:公众号51CTO技术栈 Nginx ...
- 【转】linux 编译安装nginx,配置自启动脚本
linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...
- Startssl 现在就启用 HTTPS,免费的!
为什么要使用HTTPS 主要是为了安全,虽然没有100%的安全,但是我们可以尽量提高安全级别,目前大型网站都已经使用HTTPS了 注册StartSSL 注册页面 选择国家 和 输入 邮箱 他们会通过 ...
- 话说Centos下nginx,php,mysql以及phpmyadmin的配置
大话centos下部署phalcon框架 Centos还是ubuntu? 当我沿用这个标题的时候,心里在想"我能说我之前用的windows吗?",windows下xampp,wam ...
- CentOS源码安装搭建LNMP全过程(包括nginx,mysql,php,svn)【转】
转自:http://blog.csdn.net/yanzi1225627/article/details/49123659 服务器环境为:CentOS6.5 64位 目标:搭建LNMP(Linux + ...
- nginx,wsgi,flask之间的关系
之前看写flask 应用的一些疑问,百度上的答案解释的不错,这里记着以后可以看看Web 服务器层对于传统的客户端 - 服务器架构,客户端向服务器发送请求,服务器接收请求,处理请求,最后给客户端返回请求 ...
- PHP学习笔记--1,不总结,不掌握,不明白!
不总结,不掌握,不明白! 前言: 学php一开始就是语法,变量,数组,函数,OOP(面向对象[封装,继承,多态,抽象]) 这些都是最基础的东西,但你还要懂一些在实际开发中要用的东西,比如基本的HTML ...
- 阿里云上,Ubuntu下配置Nginx,在tomcat中加了https协议就不可以了
问题 阿里云上,Ubuntu服务器,本来部署的是tomcat,并且使用了https 协议.后来为了静态资源分离集成了 nginx,nginx代理跳转到 tomcat.刚开始直接访问http 网址发现, ...
随机推荐
- BOOST_PREVENT_MACRO_SUBSTITUTION
[BOOST_PREVENT_MACRO_SUBSTITUTION] 用于防止函数被macro替换的问题. 例如: 参考: 1.https://blog.csdn.net/yanxiangtianji ...
- 认识一下margin
标签(空格分隔): margin <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- 使用netty HashedWheelTimer构建简单延迟队列
背景 最近项目中有个业务,需要对用户新增任务到期后进行业务处理.使用定时任务定时扫描过期时间,浪费资源,且不实时.只能使用延时队列处理. DelayQueue 第一想到的是java自带的延时队列del ...
- Appium 学习三:Appium-Desktop 填坑
之前使用appium执行自动化脚本,现使用appium-desktop 遇到了一些问题 1.使用name定位的报错 Locator Strategy 'name' is not supporte ...
- windows 10 透明毛玻璃,winform和wpf方法
win10的透明毛玻璃,winform和wpf方法,win7.8不能用,只是win10 public partial class Form1 : Form { [DllImport("use ...
- 百度ueditor的图片上传,前后端交互使用
百度ueditor的使用 一个文本编辑器,看了网上很多文档写的很乱,这里拾人牙慧,整理下怎么使用. 这个东西如果不涉及到图片附件上传,其实很简单,就是几个前端文件,直接引用,然后配置下ueditor. ...
- Servlet第二篇(介绍、ServletConfig;ServletContext)
什么是Serlvet? Servlet其实就是一个遵循Servlet开发的java类.Serlvet是由服务器调用的,运行在服务器端. 为什么要用到Serlvet? 我们编写java程序想要在网上实现 ...
- Spring BeanDefinitionRegistryPostProcessor BeanPostProcessor作用
写博客,写博客,把自己知道的小知识点全部记录,
- Eclipse使用相关配置
问题描述:The word is not correctly spelled问题分析:eclipse校验单词拼写造成,出现在配置文件中,会影响到程序的正常执行解决方案:在eclipse下的Window ...
- 机器学习入门-BP神经网络模型及梯度下降法-2017年9月5日14:58:16
BP(Back Propagation)网络是1985年由Rumelhart和McCelland为首的科学家小组提出,是一种按误差逆传播算法训练的多层前馈网络,是目前应用最广泛的神经网络模型之一. B ...