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 ...
随机推荐
- Python计算机视觉3:模糊,平滑,去噪
我是一名初学者,如果你发现文中有错误,请留言告诉我,谢谢 图像的模糊和平滑是同一个层面的意思,平滑的过程就是一个模糊的过程. 而图像的去噪可以通过图像的模糊.平滑来实现(图像去噪还有其他的方法) 那么 ...
- 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
python IO操作的时候出现这种错误,检查一些url的目录 这个时候需要注意一下一般的dir举例是:“F:\DOCUMENT\4.7” 需要修改成为:F:/DOCUMENT/4.7
- hdu 1500 Chopsticks
http://acm.hdu.edu.cn/showproblem.php?pid=1500 dp[i][j]为第i个人第j个筷子. #include <cstdio> #include ...
- YY语音从4.0版本开始是基于Qt的开发过程,以及碰到的问题
作者:姚冬链接:http://www.zhihu.com/question/21359230/answer/20127715来源:知乎著作权归作者所有,转载请联系作者获得授权. YY语音从4.0版本开 ...
- Wap touch flispan demo
直接上代码了 仔细看看例子就会明白 简单实用 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8& ...
- CentOS 6.5 CodeBlocks::wxWidgets安装与配置
第一步, #yum install codeblocks codeblocks-contrib codeblocks-devel 第二步,到官方下载源码包,我下的是wxX11的3.0版的. #tar ...
- Implement Stack using Queues 解答
Question Implement the following operations of a stack using queues. push(x) -- Push element x onto ...
- HDOJ-1017 A Mathematical Curiosity(淼)
http://acm.hdu.edu.cn/showproblem.php?pid=1017 # include <stdio.h> int find(int n, int m) { in ...
- kindeditor在sae上传文件修改,适合php
kindeditor在sae上传文件修改,适合php 当前位置: 首页 > 论坛 > 经验共享 用户登录 新用户注册 主题: kindeditor在sae上传文件修改,适合ph ...
- 关于uitableView的Group模式滑动偏移问题
问题: uitableView的group模式自带一个section的headerView,如果不调整,系统就会默认高度,在滑动时就会向下偏移一个单位的scetion高度,我们改变tableView的 ...