Nginx学习之一-第一个程序Hello World
本例子实现了一个简单的hello world程序。运行效果:
虚拟机Ubuntu中:
win7中chrome浏览器:
- 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"
- #include <ngx_config.h>
- #include <ngx_core.h>
- #include <ngx_http.h>
- static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
- static char *
- ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
- //处理配置项
- 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,
- 0,
- NULL
- },
- ngx_null_command
- };
- //模块上下文
- static ngx_http_module_t ngx_http_mytest_module_ctx = {
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL,
- NULL
- };
- //新模块定义
- 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
- };
- //配置项对应的回调函数
- 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;
- }
- //实际完成处理的回调函数
- static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
- {
- 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");
- r->headers_out.status = NGX_HTTP_OK;
- 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 = 1;
- ngx_chain_t out;
- out.buf = b;
- out.next = NULL;
- return ngx_http_output_filter(r, &out);
- }
四、编译安装新模块
编译安装新模块的命令如下:
- ./configure --prefix=/usr/local/nginx(指定安装部署后的根目录) --add-module=/home/nginx(新模块存放目录)
- make
- sudo make install
五、参考资料:
《深入理解Ngnix》
from:http://blog.csdn.net/xiajun07061225/article/details/9130237
Nginx学习之一-第一个程序Hello World的更多相关文章
- QT学习之第一个程序
QT学习之第一个程序 目录 手动创建主窗口 居中显示 添加窗口图标 显示提示文本 Message Box的应用 手动连接信号与槽 手动创建主窗口 窗口类型 QMainWindow: 可以包含菜单栏.工 ...
- java学习 之 第一个程序及认识
以前也看过一系列的java方面的程序,但是还没有正式敲过,今天正式学习并且正式敲出代码.在这里记录下来今日所得 写作工具:Notepad++ 在写作工具方面好多人建议用 记事本,但是我还是认为用 No ...
- Spark学习之第一个程序打包、提交任务到集群
1.免秘钥登录配置: ssh-keygen cd .ssh touch authorized_keys cat id_rsa.pub > authorized_keys chmod 600 au ...
- Spark学习之第一个程序 WordCount
WordCount程序 求下列文件中使用空格分割之后,单词出现的个数 input.txt java scala python hello world java pyfysf upuptop wintp ...
- libevent学习笔记 —— 第一个程序:计时器
用libevent写个定时器其实步骤不多: 1.初始化libevent 2.设置事件 3.添加事件 4.进入循环 由于定时事件触发之后,默认自动删除,所以如果要一直计时,则要在回调函数中重新添加定时事 ...
- C语言学习之第一个程序
#include<stdio.h> int main() { printf("This is my first program!\n"); ; } 基本格式大致如下: ...
- 从零开始学习PYTHON3讲义(三)写第一个程序
<从零开始PYTHON3>第三讲 本页面使用了公式插件,因博客主机过滤无法显示的表示抱歉,并建议至个人主页查看原文. 我见过很多初学者,提到编程都有一种恐惧感,起源是感觉编程太难了.其 ...
- OpenGL学习笔记1——第一个程序
学习的参考书基本是按照GL编程指南,如果有消息机制概念,对于GLUT的理解是很自然的.下面就按照自己写的第一个程序详细解释一下GL,还是比较容易上手的. 程序实现的功能是,根据当前随即种子摇出来的结果 ...
- AndroidStudio学习笔记-第一个安卓程序
要带一个本科生做一部分跟安卓有点关系的项目,于是趁着机会学习一下编写安卓程序. 第一篇材料来自谷歌官方,传送门:https://developer.android.com/training/basic ...
随机推荐
- shell练习--批量创建账号
#!/bin/bash #By spinestars #-- #cksum5位数获取方法,可能有重复 #pd="user`head -200 /dev/urandom | cksum | h ...
- c链表结点的删除和添加
#include<stdio.h> #include<stdlib.h> typedef char datetype;/*定义新的数据类型名*/ typedef struct ...
- ajax请求解析springmvc返回的json数据
需要使用的框架 spring3.0 jquery1.9.0(简化ajax开发的js库) Jackson(json处理器):jackson-core-asl-1.9.2.jar,jackson-mapp ...
- 夏宇闻教授谈FPGA工程师的入门学习
1. 必须清楚自己究竟适合不适合做工程师. 看看自己的性格特点,是不是特别安静,又耐得住寂寞.因为FPGA工程师是一个辛苦的工作,不但要通过不断学习研究提升自己的设计水平,还要经常性的熬夜加班敲写代码 ...
- SPI总线的4种工作模式
spi总线的4种工作模式 0 to 4 modes SPI接口的全称是"Serial Peripheral Interface",意为串行外围接口,是Motorola首先在其MC6 ...
- Keil C51.uew
/L15"Keil C51" Line Comment = // Block Comment On = /* Block Comment Off = */ Escape Char ...
- Android高德地图开发具体解释
这段时间开发的时候用到了高德地图,对高德地图开发有心得体会,如今分享给大家.对我开发过百度地图的我来说,整体来说高德地图Demo,没有百度解说的具体 个人更偏向于使用百度地图,可是没办发,项目须要使用 ...
- OpenWrt修改
openwrt如何编译修改界面的顶部.底部信息.LOGO图片 2011-06-02 16:20:03 浏览次 以Atheros71xx为例,修改路径为:trunk/build_dir/target ...
- popToViewController用法
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIn ...
- WIN7 64位系统安装JDK并配置环境变量
本文来自:http://jingyan.baidu.com/article/3c343ff70bc6ea0d377963df.html 工具/原料 JDK 方法/步骤 首先,下载JDK安装包,到官 ...