nginx下载地址

一、新增http模块
1、 新建文件夹以及文件


2、 修改ngx_module.c文件,将新增的文件名加进去:


代码

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>static ngx_int_t ngx_ext_sunx_handler(ngx_http_request_t *r);static char* ngx_ext_sunx(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static ngx_command_t ngx_ext_sunx_commands[] = {{ngx_string("hello_world"),NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | \NGX_CONF_NOARGS,ngx_ext_sunx,NGX_HTTP_LOC_CONF_OFFSET,0,NULL},ngx_null_command
};static ngx_http_module_t ngx_ext_sunx_module_ctx = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL
};ngx_module_t ngx_ext_sunx_module = {NGX_MODULE_V1,&ngx_ext_sunx_module_ctx,ngx_ext_sunx_commands,NGX_HTTP_MODULE,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NGX_MODULE_V1_PADDING
};static void testList(ngx_pool_t *pool);static char *ngx_ext_sunx(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_ext_sunx_handler;testList(cf->pool);return NGX_CONF_OK;
}static ngx_int_t ngx_ext_sunx_handler(ngx_http_request_t *r) {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("Sunx 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);
}static void testList(ngx_pool_t *pool)
{ngx_list_t *testlist = ngx_list_create(pool, 4, sizeof(ngx_str_t));ngx_str_t* str = ngx_list_push(testlist);str->len = sizeof("test");str->data = "test";str = ngx_list_push(testlist);str->len = sizeof("test2");str->data = "test2";str = ngx_list_push(testlist);str->len = sizeof("test2");str->data = "test2";ngx_list_part_t* part = &testlist->part;str = part->elts;for (int i = 0;; i++){if (i >= part->nelts){if (part->next == NULL){break;}part = part->next;i = 0;}printf("list element : %s\n", str[i].data);}}static void testBuf(ngx_pool_t *pool)
{ngx_buf_t buf;
}

3、 修改conf/nginx.conf

4、 编译并运行nginx,然后在浏览器上输入:127.0.0.1:8082/sunx,结果如下:

二、 新增http过滤模块
1、新加代码

ngx_http_myfilter.c

#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>/*
HTTP过滤模块的地位、作用与正常的HTTP处理模块是不同的,它所做的工作是对发送给用户的HTTP响应包做一些加工
HTTP过滤模块不会去访问第三方服务
*/typedef struct
{ngx_flag_t     enable;
} ngx_http_myfilter_conf_t;typedef struct
{ngx_int_t      add_prefix;
} ngx_http_myfilter_ctx_t;static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;//将在包体中添加这个前缀
static ngx_str_t filter_prefix = ngx_string("[my filter prefix]");static void* ngx_http_myfilter_create_conf(ngx_conf_t *cf);
static char *
ngx_http_myfilter_merge_conf(ngx_conf_t *cf, void *parent, void *child);static ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf);
static ngx_int_t
ngx_http_myfilter_header_filter(ngx_http_request_t *r);
static ngx_int_t
ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in);static ngx_command_t  ngx_http_myfilter_commands[] =
{{ngx_string("add_prefix"),NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_FLAG,ngx_conf_set_flag_slot,NGX_HTTP_LOC_CONF_OFFSET,offsetof(ngx_http_myfilter_conf_t, enable),NULL},ngx_null_command
};static ngx_http_module_t  ngx_http_myfilter_module_ctx =
{NULL,                                  /* preconfiguration方法  */ngx_http_myfilter_init,                /* postconfiguration方法 */NULL,                                  /*create_main_conf 方法 */NULL,                                  /* init_main_conf方法 */NULL,                                  /* create_srv_conf方法 */NULL,                                  /* merge_srv_conf方法 */ngx_http_myfilter_create_conf,         /* create_loc_conf方法 */ngx_http_myfilter_merge_conf           /* merge_loc_conf方法 */
};ngx_module_t  ngx_http_myfilter_module =
{NGX_MODULE_V1,&ngx_http_myfilter_module_ctx,     /* module context */ngx_http_myfilter_commands,        /* module directives */NGX_HTTP_MODULE,                /* module type */NULL,                                  /* init master */NULL,                                  /* init module */NULL,                                  /* init process */NULL,                                  /* init thread */NULL,                                  /* exit thread */NULL,                                  /* exit process */NULL,                                  /* exit master */NGX_MODULE_V1_PADDING
};/*
遍历访问所有的HTTP过滤模块时,这个单链表中的元素是怎么用next指针连接起来的呢?很简单,每个HTTP过滤模块在初始化时,会先找到链表
的首元素ngx_http_top_header_filter指针和ngx_http_top_body_filter指针,再使用static静态类型的ngx_http_next_header_filter和
ngx_http_next_body_filter指针将自己插入到链表的首部,这样就行了。下面来看一下在每个过滤模块中ngx_http_next_ header_ filter和ngx_http_next- body_filter据针的
定义:
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;注意,ngx_http_next_header_filter和ngx_http_next_body_filter都必须是static静态变量,为什么呢?因为static类型可以让上面两
个变量仅在当前文件中生效,这就允许所有的HTTP过滤模块都有各自的ngx_http_next_header_filter和ngx_http_next_body_filter指针。
这样,在每个HTTP过滤模块初始化时,就可以用上面这两个指针指向下一个HTTP过滤模块了。例如,可以像下列代码一样将当前HTTP过滤模块
的处理方法添加到链表首部。
ngx_http_next_header_filter = ngx_http_top_header filter;
ngx_http_top_header filter = ngx_http_myfilter_header_filter;
ngx_ht tp_next_body_f ilter = ngx_http_top_body_f ilter ;
ngx_http_top_body_f ilt er = ngx_ht tp_myf ilter_body_f ilter ;速样,在初始化到本模块时,自定义的ngx_http_myfilter_header_filter与ngx_http_myfilter_body_filter方法就暂时加入到了链表的首部,
而且本模块所在文件中static类型的ngx_http_next_header_filter指针和ngx_http_next_body_filter指针也指向了链表中原来的首部。在实际使用中,
如果需要调用下一个HTTP过滤模块,只需要调用ngx_http_next_header_filter(r)或者ngx_http_next_ body_filter(r,chain)就可以了。
*///处理流程ngx_http_send_header中执行ngx_http_myfilter_header_filter,然后在ngx_http_myfilter_header_filter中执行下一个filter,也就是
//ngx_http_next_header_filter中存储之前的filter,这样依次循环下去,那么所有的filter函数都会得到执行//包体通过ngx_http_output_filter循环发送
static ngx_int_t ngx_http_myfilter_init(ngx_conf_t *cf)
{//插入到头部处理方法链表的首部ngx_http_next_header_filter = ngx_http_top_header_filter; //ngx_http_next_header_filter指针零时保存ngx_http_top_header_filter。ngx_http_top_header_filter = ngx_http_myfilter_header_filter; //在ngx_http_myfilter_header_filter中继续处理下一个filter//插入到包体处理方法链表的首部ngx_http_next_body_filter = ngx_http_top_body_filter;ngx_http_top_body_filter = ngx_http_myfilter_body_filter;return NGX_OK;
}static ngx_int_t ngx_http_myfilter_header_filter(ngx_http_request_t *r)
{ngx_http_myfilter_ctx_t   *ctx;ngx_http_myfilter_conf_t  *conf;//如果不是返回成功,这时是不需要理会是否加前缀的,直接交由下一个过滤模块
//处理响应码非200的情形if (r->headers_out.status != NGX_HTTP_OK){return ngx_http_next_header_filter(r);}//获取http上下文ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);if (ctx){//该请求的上下文已经存在,这说明
// ngx_http_myfilter_header_filter已经被调用过1次,
//直接交由下一个过滤模块处理return ngx_http_next_header_filter(r);}//获取存储配置项的ngx_http_myfilter_conf_t结构体conf = ngx_http_get_module_loc_conf(r, ngx_http_myfilter_module);//如果enable成员为0,也就是配置文件中没有配置add_prefix配置项,
//或者add_prefix配置项的参数值是off,这时直接交由下一个过滤模块处理if (conf->enable == 0){return ngx_http_next_header_filter(r);}//构造http上下文结构体ngx_http_myfilter_ctx_tctx = ngx_pcalloc(r->pool, sizeof(ngx_http_myfilter_ctx_t));if (ctx == NULL){return NGX_ERROR;}//add_prefix为0表示不加前缀ctx->add_prefix = 0;//将构造的上下文设置到当前请求中ngx_http_set_ctx(r, ctx, ngx_http_myfilter_module);//myfilter过滤模块只处理Content-Type是"text/plain"类型的http响应if (r->headers_out.content_type.len >= sizeof("text/plain") - 1&& ngx_strncasecmp(r->headers_out.content_type.data, (u_char *) "text/plain", sizeof("text/plain") - 1) == 0){//1表示需要在http包体前加入前缀ctx->add_prefix = 1;//如果处理模块已经在Content-Length写入了http包体的长度,由于
//我们加入了前缀字符串,所以需要把这个字符串的长度也加入到
//Content-Length中if (r->headers_out.content_length_n > 0)r->headers_out.content_length_n += filter_prefix.len;}//交由下一个过滤模块继续处理return ngx_http_next_header_filter(r);
}/*
本节通过一个简单的例子来说明如何开发HTTP过滤模块。场景是这样的,用户的请求由static静态文件模块进行了处理,它会根据URI返回磁盘
中的文件给用户。而我们开发的过滤模块就会在返回给用户的响应包体前加一段字符串:”[my filter prefix]”。需要实现的功能就是这么
简单,当然,可以在配置文件中决定是否开启此功能。
*/
static ngx_int_t
ngx_http_myfilter_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{ngx_http_myfilter_ctx_t   *ctx;ctx = ngx_http_get_module_ctx(r, ngx_http_myfilter_module);//如果获取不到上下文,或者上下文结构体中的add_prefix为0或者2时,都不会添加前缀,这时直接交给下一个http过滤模块处理if (ctx == NULL || ctx->add_prefix != 1){return ngx_http_next_body_filter(r, in);}//将add_prefix设置为2,这样即使ngx_http_myfilter_body_filter
//再次回调时,也不会重复添加前缀ctx->add_prefix = 2;//从请求的内存池中分配内存,用于存储字符串前缀ngx_buf_t* b = ngx_create_temp_buf(r->pool, filter_prefix.len);
//将ngx_buf_t中的指针正确地指向filter_prefix字符串b->start = b->pos = filter_prefix.data;b->last = b->pos + filter_prefix.len;//从请求的内存池中生成ngx_chain_t链表,将刚分配的ngx_buf_t设置到其buf成员中,并将它添加到原先待发送的http包体前面ngx_chain_t *cl = ngx_alloc_chain_link(r->pool);cl->buf = b;cl->next = in;//调用下一个模块的http包体处理方法,注意这时传入的是新生成的cl链表return ngx_http_next_body_filter(r, cl);
}static void* ngx_http_myfilter_create_conf(ngx_conf_t *cf)
{ngx_http_myfilter_conf_t  *mycf;//创建存储配置项的结构体mycf = (ngx_http_myfilter_conf_t  *)ngx_pcalloc(cf->pool, sizeof(ngx_http_myfilter_conf_t));if (mycf == NULL){return NULL;}//ngx_flat_t类型的变量,如果使用预设函数ngx_conf_set_flag_slot
//解析配置项参数,必须初始化为NGX_CONF_UNSETmycf->enable = NGX_CONF_UNSET;return mycf;
}static char *
ngx_http_myfilter_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{ngx_http_myfilter_conf_t *prev = (ngx_http_myfilter_conf_t *)parent;ngx_http_myfilter_conf_t *conf = (ngx_http_myfilter_conf_t *)child;//合并ngx_flat_t类型的配置项enablengx_conf_merge_value(conf->enable, prev->enable, 0);return NGX_CONF_OK;
}

2、修改ngx_module.c文件,将新增的文件名加进去:


3、修改conf/nginx.conf

4、 在nginx安装目录下新建文件夹txt,并新建文件ttt.txt,然后写入内容

5、 编译并运行nginx,然后再浏览器中输入127.0.0.1:8082/ttt.txt,结果如下:

vs2013调试nginx相关推荐

  1. nginx模块_使用gdb调试nginx源码

    工欲善其事必先利其器,如何使用调试工具gdb一步步调试nginx是了解nginx的重要手段. ps:本文的目标人群是像我这样初接触Unix编程的同学,如果有什么地方错误请指正. 熟悉gdb的使用 这里 ...

  2. Nginx学习之十四-GDB调试Nginx初试

    本文的测试环境: Win7+虚拟机VMWareVMware-workstation-full-7.1.4-385536+Ubuntu12.04 Nginx-1.4.0 要想有效的研究Nginx源码,必 ...

  3. 开发机器上利用vs2013调试远程IIS上的c#程序

    当远程IIS上的C#程序出现问题,怎么排错,一般我们通过看日志排查错误的方法,这种方法在程序异常日志都打印出来的情况下是可以解决的,但如果程序日志不详细,或者从日志看不出有用的内容的时候怎么排错? 本 ...

  4. php fpm 调试模式,调试 – nginx php-fpm xdebug netbeans只能启动一个调试会话

    在过去,我使用apache mod_PHP xdebug netbeans进行开发我的网站(服务器是我的本地机器,运行Debian Squeeze),很高兴 – xdebug工作正常,调试会话可以随时 ...

  5. Qt Ctreator搭配VS2013调试——整合QML/C++调试需要的从属调试引擎无法被创建

    另一篇博客讲了VS2013中的环境配置:http://blog.csdn.net/sinat_36053757/article/details/70049372 用Qt创建了一个新的项目Qt Quic ...

  6. 用nodejs 调试nginx rewrite 后输出路径

    由于测试环境前端和后台分开部署,所以会遇到跨域问题. 前端工程是在nginx里面启动的,所以需要配置代理. 代理其实并不难配,主要是写个正则,由于对nginx并不是很熟悉,很难找到rewrite后的输 ...

  7. vs2013调试技巧及快捷键

    VS2013快捷键很多,灵活使用常用快捷键及各项技巧可以让你事半功倍.下面的visual studio 2013快捷键和操作技巧你知道多少? 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一 ...

  8. vs2013调试变量设置

    目录 1.环境变量名 2.目录设置 3.Release模式下进行调试 1.环境变量名 环境变量名    含义 $(SolutionDir)                   解决方案目录:即.sln ...

  9. 轻量级HTTP服务器Nginx(配置与调试Nginx)

    Nginx安装完毕后,会产生相应的安装目录,根据前面的安装路径,Nginx的配置文件路径为/opt/nginx/conf,其中nginx.conf为Nginx的主配置文件.这里重点介绍下nginx.c ...

  10. 轻量级HTTP服务器Nginx(配置与调试Nginx)(转)

    2019独角兽企业重金招聘Python工程师标准>>> 转自:http://www.linuxidc.com/Linux/2012-03/55868p3.htm Nginx安装完毕后 ...

最新文章

  1. 每日一皮:和女朋友争吵与阅读软件许可协议之间的共同点...
  2. MATLAB作二维傅里叶变换所需要注意和知道的东西(im2double、fft2、abs、imshow、二维傅里叶变换的物理意义)
  3. 支持回调处理 php函数,PHP支持回调的函数有哪些
  4. MongoDB最简单的入门教程之五-通过Restful API访问MongoDB
  5. python day5--正则表达式
  6. (转) java 复制文件,不使用输出流复制,高效率,文件通道的方式复制文件
  7. Windows常用shell命令大全
  8. 【C语言】输入一个字符串,统计其中的单词个数,将第一个单词的首字母改为大写,并输出改写后的字符串...
  9. 宝塔修改Nginx服务器类型,宝塔面板nginx更改日志格式的方法
  10. 感情沟通出了问题要怎么解决_感情专家咨询:感情出问题了怎么办?
  11. pandas之Series()菜鸟教程
  12. javaparser - java源码分析修改框架
  13. Codeforces 1092D1. Great Vova Wall (Version 1)
  14. 002输出一个正方形
  15. igraph 牛刀小试
  16. vue3在控制台打印相关变量的值
  17. Vampire:吸血鬼
  18. 谷歌扩展工具下载网址---最新
  19. 文件 服务器属性,去除服务器文件上的RHS属性
  20. 什么是胃肠神经官能症,会有哪些症状出现呢?

热门文章

  1. CSP开发基础--CSP入门简介
  2. 杭州电子科技大学计算机非全日制,杭州电子科技大学全日制和非全日制研究生有何区别?...
  3. GPS定位原理、系统组成及工作频段
  4. session+cookie简单讲解以及持久化登录实现
  5. 查手机服务器ip和端口网站,如何查询服务器ip地址和端口号
  6. 树形结构的处理——组合模式(一)
  7. 网课python程序设计答案_中国大学MOOCPython程序设计网课答案
  8. ROS_Noetic使用handeye-calib进行机械臂手眼标定
  9. Unity 文字转语音
  10. 汉语言01C1501,2019年北京自考汉语言文学(本科)专业(01C1501)要考哪些科目?...