首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p>来源 Nginx模块开发与架构解析 陶辉</p><p><br/></p><p>下载解压源码后</p><p>mkdir /root/nginx-1.16.1/src/test</p><p><br/></p><p>vi /root/nginx-1.16.1/src/test/config</p><pre class="brush:plain;toolbar:false">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"</pre><p><br/></p><p>vi /root/nginx-1.16.1/src/test/ngx_http_mytest_module.c<br/></p><pre class="brush:plain;toolbar:false">#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, //类型type ngx_http_mytest, //set 函数 NGX_HTTP_LOC_CONF_OFFSET, //偏移 0, NULL }, ngx_null_command }; //回调函数,出现my_test配置项后调用 static char * ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_http_core_loc_conf_t *clcf; //找到mytest所属配置块 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); //主机域名、URI与mytest所在配置块匹配时,将调用handler clcf->handler = ngx_http_mytest_handler; return NGX_CONF_OK; } //ngx_http_module_t接口,模块上下文 //如果没有什么工作是必须在HTTP框架初始化时完成,则不必实现8个回调 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 ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r) { //请求方法必须是GET或者HEAD,否则返回405/NOT ALLOWED 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 (NGX_OK != rc) { return rc; } //设置返回的Content-Type ngx_string是一个宏可以初始化data字段和len字段 ngx_str_t type = ngx_string("text/plain"); ngx_str_t response = ngx_string("Hello ZhangXiao World"); //响应包体内容和状态码设置 r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = response.len; r->headers_out.content_type = type; //发送http头部 rc = ngx_http_send_header(r); if (rc == NGX_ERROR || rc>NGX_OK || r->header_only) { return rc; } //构造ngx_buf_t结构体准备发送报文 ngx_buf_t *b; b = ngx_create_temp_buf(r->pool, response.len); if (NULL == b) { 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结构体 ngx_chain_t out; out.buf = b; out.next = NULL; //发送响应,结束后HTTP框架会调用ngx_http_finalize_request方法结束请求 return ngx_http_output_filter(r, &out); }</pre><p><br/></p><p>./configure --add-module=/root/nginx-1.16.1/src/test</p><p>make</p><p>sudo make install</p><p><br/></p><p>sudo vi /usr/local/nginx/conf/nginx.conf</p><pre class="brush:plain;toolbar:false">#nginx.conf删去一部分内容 http { server { listen 80; server_name localhost; #########添加部分######### location=/zxtest{ #mytest配置项 mytest; } ######################### location / { proxy_pass http://localhost:8080; } }</pre><p><br/></p><p><br/></p><p><br/></p><p><br/></p><p>参考自<a href="https://blog.csdn.net/zhangge3663/article/details/87716963">https://blog.csdn.net/zhangge3663/article/details/87716963</a></p>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0020520687103271 秒,PHP脚本用时 0.004099 秒,占用内存 0.534 MB,Gzip 已启用