为了学习高性能并发服务器,打算研究一下Nginx的实现。按照惯例,最开始都要写一个hello world的程序,所以接下来就是介绍如何在Nginx的框架下编写一个简单的HTTP模块来打印”Hello World“。
定义hello配置项的处理
首先,我们需要定义一个commands数组用来定义模块的配置文件参数。每一个数组元素都是ngx_command_t类型,数组的结尾用ngx_null_command结尾。
Nginx在解析配置文件中的一个配置项时首先会遍历所有的模块,对于每个模块而言,即通过遍历commands数组进行。每一个ngx_command_t结构体定义了自己感兴趣的一个配置项。该结构定义如下:
<code><span>struct</span> ngx_command_s { <span>/* 配置项名称 */</span> ngx_str_t name; <span>/* 指定配置项可以出现的位置 */</span> ngx_uint_t type; <span>/* 出现了name中指定的配置项后,将会调用set方法处理配置项的参数 */</span><span>char</span> *(*<span>set</span>)(ngx_conf_t *cf, ngx_command_t *cmd, <span>void</span> *conf); ngx_uint_t conf; <span>/* 在配置文件中的偏移量 */</span> ngx_uint_t offset; <span>/* 配置项读取后的处理过程,必须是ngx_conf_post_t结构的指针 */</span><span>void</span> *post; }; <span>#define ngx_null_command { ngx_null_string, 0, NULL, 0, 0, NULL }</span></code>
了解了commands数组后,我们定义hello配置项的处理:
<code><span>static</span> ngx_command_t ngx_http_hello_commands[] = { { ngx_string(<span>"hello"</span>), NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, ngx_http_hello, NGX_HTTP_LOC_CONF_OFFSET, <span>0</span>, NULL }, ngx_null_command };</code>
其中,ngx_http_hello是ngx_command_t结构体中的set成员,当在某个配置块中出现hello配置项时,Nginx会调用ngx_http_hello方法。下面是ngx_http_hello的实现:
<code><span>static</span><span>char</span> * ngx_http_hello(ngx_conf_t *cf, ngx_command_t *cmd, <span>void</span> *conf) { ngx_http_core_loc_conf_t *clcf; <span>/* 首先找到hello配置项所属的配置块 */</span> clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); <span>/* HTTP框架在处理用户请求进行到NGX_HTTP_CONTENT_PHASE阶段时 * 如果请求的主机域名、URI与hello配置项所在的配置块相匹配 * 则调用ngx_http_hello_handler方法处理这个请求 */</span> clcf->handler = ngx_http_hello_handler; <span>return</span> NGX_CONF_OK; }</code>
定义hello模块
定义一个HTTP模块的方式很简单,只要像下面这样定义一个ngx_moodule_t的结构体:
<code>ngx_module_t ngx_http_hello_module = { NGX_MODULE_V1, &ngx_http_hello_module_ctx, <span>/* module context */</span> ngx_http_hello_commands, <span>/* module directives */</span> NGX_HTTP_MODULE, <span>/* module type */</span> NULL, <span>/* init master */</span> NULL, <span>/* init module */</span> NULL, <span>/* init process */</span> NULL, <span>/* init thread */</span> NULL, <span>/* exit thread */</span> NULL, <span>/* exit process */</span> NULL, <span>/* exit master */</span> NGX_MODULE_V1_PADDING };</code>
则hello模块在编译时将会被加入到ngx_modules全局数组中。
其中ngx_http_hello_commands
就是前一节我们定义的hello配置项的处理。
因为我们定义的是HTTP模块,所以type
要设置成NGX_HTTP_MODULE
。
还有一个重要的成员void* ctx
,对于HTTP模块来说,ctx指针必须指向ngx_http_module_t
接口。
HTTP框架在读取、重载配置文件时定义了由ngx_http_module_t接口描述的8个阶段,HTTP框架在启动的时候会在每个阶段中调用ngx_http_module_t中相应的方法。如果不需要做什么工作,则可以定义为NULL。因为hello模块不需要做什么工作,所以定义如下:
<code><span>static</span> ngx_http_module_t ngx_http_hello_module_ctx = { NULL, <span>/* preconfiguration */</span> NULL, <span>/* postconfiguration */</span> NULL, <span>/* create main configuration */</span> NULL, <span>/* init main configuration */</span> NULL, <span>/* create server configuration */</span> NULL, <span>/* merge server configuration */</span> NULL, <span>/* create location configuration */</span> NULL <span>/* merge location configuration */</span> };</code>
处理用户请求
最后就是处理用户请求了,这里需要一点HTTP的知识,可以参考HTTP协议入门。我们是通过实现ngx_http_hello_handler
方法来处理用户的请求了,该方法定义如下:
<code><span>static</span> ngx_int_t ngx_http_hello_handler(ngx_http_request_t *r)</code>
其中ngx_http_request_t
结构体中包含了请求的所有信息(如方法,URI,协议版本号和头部等),除此之外,还包含了其他很多成员,例如内存池,响应报头等等。
因为我们只处理GET方法和HEAD方法,所以需要做如下判断:
<code><span>if</span> (!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))) { <span>return</span> NGX_HTTP_NOT_ALLOWED; }</code>
接下来因为我们不需要请求中的包体,所以需要丢弃掉包体,方法如下:
<code>ngx_int_t rc = ngx_http_discard_request_body(r); <span>if</span> (rc != NGX_OK) { <span>return</span> rc; }</code>
然后是设置返回的响应包,返回的包体只包含一个”Hello World”字符串:
<code>ngx_str_type = ngx_string(<span>"text/plain"</span>); ngx_str_response = ngx_string(<span>"Hello World"</span>); r->headers_out.status = NGX_HTTP_OK; r->headers_out.content_length_n = response.len; r->headers_out.content_type = type;</code>
最后就是发送响应包的包头和包体了:
<code> rc = ngx_http_send_header(r); <span>if</span> (rc == NGX_ERR || rc > NGX_OK || r->header_only) { <span>return</span> rc; } ngx_buf_t *b; b = ngx_create_temp_buf(r->pool, response.len); <span>if</span> (b == NULL) { <span>return</span> NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_memcpy(b->pos, response.data, response.len); b->last = b->pos + response.len; b->last_buf = <span>1</span>; ngx_chain_t out; out.buf = b; out.next = NULL; <span>/* send the buffer chain of your response */</span><span>return</span> ngx_http_output_filter(r, &out);</code>
完整代码可以在这里查看:hello_module
编译和运行
在代码同目录下新建一个config
文件,添加这样几行:
<code>ngx_addon_name=ngx_http_hello_module HTTP_MODULES=<span>"<span>$HTTP_MODULES</span> ngx_http_hello_module"</span> NGX_ADDON_SRCS=<span>"<span>$NGX_ADDON_SRCS</span><span>$ngx_addon_dir</span>/ngx_http_hello_module.c"</span></code>
然后进入Nginx的源码根目录,运行configure
,记得带上–add-module参数,在参数后面接上我们自己编写的HTTP模块代码所在的路径:
<code>./configure --prefix=<span>/usr/local</span><span>/nginx --add-module=/code</span><span>/nginx-1.8.0/src</span><span>/http/hello</span>_module</code>
configure运行完成后,用make
命令来编译,编译成功后输入make install
完成安装。
修改/usr/local/nginx/conf/nginx.conf
,添加:
<code>http{ <span>...</span> server { <span>...</span> location /hello { hello; } <span>...</span> } <span>...</span> }</code>
运行Nginx,然后在浏览器输入IP/hello
就可以看到显示的”Hello World字符串了“。
参考
《深入理解Nginx》
以上就介绍了 hello模块的编写,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP在现代编程中仍然是一个强大且广泛使用的工具,尤其在web开发领域。1)PHP易用且与数据库集成无缝,是许多开发者的首选。2)它支持动态内容生成和面向对象编程,适合快速创建和维护网站。3)PHP的性能可以通过缓存和优化数据库查询来提升,其广泛的社区和丰富生态系统使其在当今技术栈中仍具重要地位。

在PHP中,弱引用是通过WeakReference类实现的,不会阻止垃圾回收器回收对象。弱引用适用于缓存系统和事件监听器等场景,需注意其不能保证对象存活,且垃圾回收可能延迟。

\_\_invoke方法允许对象像函数一样被调用。1.定义\_\_invoke方法使对象可被调用。2.使用$obj(...)语法时,PHP会执行\_\_invoke方法。3.适用于日志记录和计算器等场景,提高代码灵活性和可读性。

Fibers在PHP8.1中引入,提升了并发处理能力。1)Fibers是一种轻量级的并发模型,类似于协程。2)它们允许开发者手动控制任务的执行流,适合处理I/O密集型任务。3)使用Fibers可以编写更高效、响应性更强的代码。

PHP社区提供了丰富的资源和支持,帮助开发者成长。1)资源包括官方文档、教程、博客和开源项目如Laravel和Symfony。2)支持可以通过StackOverflow、Reddit和Slack频道获得。3)开发动态可以通过关注RFC了解。4)融入社区可以通过积极参与、贡献代码和学习分享来实现。

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Atom编辑器mac版下载
最流行的的开源编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。