ngx_http_wait_request_handler函数解析
2016-04-20 11:08
603 查看
在普通的http请求过程中,ngx_event_accept函数中ls->handler(c);调用了ngx_http_init_connection函数,ngx_http_init_connection函数会调用ngx_http_wait_request_handler。主要代码和解析如下。
查看事件是否已过期,如果已过期,直接断开连接
查看事件是否已过期,如果已过期,直接断开连接
if (rev->timedout) { ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out"); ngx_http_close_connection(c); return; }调用recv函数,接收数据。函数原型在ngx_recv.c中定义,根据是否使用kqueue(一种i/o复用技术),有两种函数定义。
n = c->recv(c, b->last, size); if (n == NGX_AGAIN) { 尚未有数据道来,由于Nginx的事件驱动机制,程序不会阻塞,注册读事件并返回。 if (ngx_handle_read_event(rev, 0) != NGX_OK) { ngx_http_close_connection(c); return; } 连接有可能什么实质性工作都不做,所以暂且释放内存。 if (ngx_pfree(c->pool, b->start) == NGX_OK) { b->start = NULL; } return; }recv函数返回了错误,直接断开连接并返回。
if (n == NGX_ERROR) { ngx_http_close_connection(c); return; }client主动断开了连接。
if (n == 0) { ngx_log_error(NGX_LOG_INFO, c->log, 0, "client closed connection"); ngx_http_close_connection(c); return; }如果有类似于“listen 80 proxy_protocol;”的配置,尚未仔细研究
if (hc->proxy_protocol) { ... } ngx_reusable_connection(c, 0);调用ngx_http_create_request函数初始化一个ngx_http_request_t结构体。
c->data = ngx_http_create_request(c); if (c->data == NULL) { ngx_http_close_connection(c); return; }将目标连接的读事件回调函数设置为处理处理请求行
rev->handler = ngx_http_process_request_line;获取已经有数据到达,所以马上执行处理请求行函数,在ngx_http_process_request_line中循环调用recv函数,从缓冲区中取东西出来。ngx_thttp_process_request_line函数可能会被调用多次。
ngx_http_process_request_line(rev);
相关文章推荐
- nginx代理指定目录
- 访问Nginx发生SSL connection error的一种情况
- Nginx+Naxsi部署专业级Web应用防火墙
- CentOS 6.2实战部署Nginx+MySQL+PHP
- nginx中http核心模块的配置指令2
- nginx中http核心模块的配置指令3
- nginx中http核心模块的配置指令4
- nginx中http的fastcgi模块的配置指令1
- Nginx 学习笔记(一)
- 网站502与504错误分析
- 用zabbix监控nginx_status状态
- 艰难完成 nginx + puma 部署 rails 4的详细记录
- 把Lua编译进nginx步骤方法
- web 应用中常用的各种 cache详解
- Linux系统上配置Nginx+Ruby on Rails+MySQL超攻略
- window+nginx+php环境配置 附配置搭配说明
- 解析CI即CodeIgniter框架在Nginx下的重写规则
- 将PHP从5.3.28升级到5.3.29时Nginx出现502错误
- 基于Nginx0.8.54+PHP5.3.4+MySQL5.5.8的全新LNMP稳定版架构搭建的VPS