Chinaunix首页 | 论坛 | 博客
  • 博客访问: 749933
  • 博文数量: 116
  • 博客积分: 923
  • 博客等级: 准尉
  • 技术积分: 1635
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 21:43
个人简介

一直帮老板搬运代码!!!

文章分类
文章存档

2013年(47)

2012年(69)

分类: LINUX

2013-02-20 19:10:51

//错误总结

//运行前(init循环前和发送数据到客户端前,会组合头)
//程序运行的时候,对发送消息头进行初始化组合,反向代理时调用
ngx_init_cycle(ngx_cycle_t *old_cycle)
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
rv = cmd->set(cf, cmd, conf);// 调度指令的钩子 set
ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
rv = ngx_http_merge_servers(cf, cmcf, module, mi); //组合成http
rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],cscfp[s]->ctx->loc_conf[ctx_index]);
ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {

//未解决问题:
1、什么情况会出错,然后转向发送错误页面?(可能有时间因素)

//第一种情况
tomcat为例子
n = recv(c->fd, buf, size, 0);返回0,errno为11
//读不到数据的时候
connectionTimeout :  为20000毫秒,由于tomcat 配置如果时间内没有发送数据,那么导致服务器关闭,接受失败
先说结论:connectionTimeout应该是server在read socket时候的超时时间。
如果说建立怜接的超时,对于客户端来说更有意义。因为socket的connect行为会阻塞,这个超时有意义。

//其他情况
//其他就是超时的时候,会发送错误
当然会通过读取1个字节来判断客户端是否关闭链接

2、服务器给客户端发送数据的时候,发送两次,具体什么情况?
   nginx从后端获取数据后,进行解析,分为消息头和消息体,分别给客户端发送数据(writev),也就发送了两次(错误的时候发两次);其他情况,合成一个包,发送;或多次writev(内容比较多时)。

3、错误页面发送,发送后,具体是什么发送?(发送体是不是需要保存到一个临时文件,头比体数据大//数据量问题)(先发头再发体)
//组合发送的头多次checker

default:
   status = NGX_HTTP_BAD_GATEWAY;

//走过
name->data = 0x725b01 "/usr/local/nginx/html/50x.html"
fd = ngx_open_file(name, NGX_FILE_RDONLY|NGX_FILE_NONBLOCK,NGX_FILE_OPEN, 0); fd = 10

//先发送头,之前进行了发送头组合,这里的消息头是nginx自己组合的
//连接超时 我们向服务器发送请求 由于服务器当前链接太多,导致服务器方面无法给于正常的响应,产生此类报错
0x725cf0 "HTTP/1.1 502 Bad Gateway\r\nServer: nginx/1.0.12\r\nDate: Tue, 19 Feb 2013 05:39:00 GMT\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 537\r\nConnection: keep-alive\r\n\r\n"
rc = writev(c->fd, header.elts, header.nelts);

//再发送体,这里是实体html文件
rc = sendfile(c->fd, file->file->fd, &offset, file_size); //体是一个文件 file->file->fd = 10

4、客户端转发请求前,是不是需要解析?(需要解析,其实是重写url,然后发送到后端服务器上)
ngx_http_process_request_line(ngx_event_t *rev)
n = ngx_http_read_request_header(r);
n = recv(c->fd, buf, size, 0);
r->header_in->last = 0x775180 "GET /examples/jsp/jsp2/simpletag/hello.jsp HTTP/1.1\r\nHost: localhost:8888\r\nUser-Agent: ...

//开始解析request请求头部。
rc = ngx_http_parse_request_line(r, r->header_in);

//其实是重写url,在读取客户端后进行重写(或其他适合后端服务器的数据)
ngx_http_core_rewrite_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph)

r->header_in pos = 0x00000000007751b5
0x7751b5 "Host: localhost:8888\r\nUser-Agent: Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.9) Gecko/20110412 CentOS/3.6.9-2.el6.centos Firefox/3.6.9\r\nAccept: text/html,application/xhtml+xml,application/xml;"...    
r->uri.data = 0x775184 "/examples/jsp/jsp2/simpletag/hello.jsp HTTP/1.1\r\nHost: localhost:8888\r\nUser-Agent: Moz ...

5、正常发送是什么情况?
  获得客户端请求数据后,进行url解析rewrite后(生成适合发送给后端服务器的数据),建立后端链接,
  发送给后端up服务器,后端获得数据后,先读取请求头,再读取请求体,然后合成发送数据包(可能多次发送),然后转发给客户端

6、up发送前再读一次的作用(确保客户端活动)
wev->handler = 0x43c4b5
//紧接着就是第二部分,这部分的工作就是预读取1个字节,然后来判断是否连接已经被client断掉。
n = recv(c->fd, buf, 1, MSG_PEEK);//读取1个字节
err = ngx_socket_errno;

7、是不是接受后端两次啊?客户端给服务器发送的时候是不是也发送两次啊

//代理读两次

//代理读取请求头 recv
u->buffer
{pos = 0x726480 "HTTP/1.1 200 OK\r\nServer: Apache-Coyote/1.1\r\nSet-Cookie: JSESSIONID=89B390E44D0E9225A26A7BEE5F285628; Path=/examples\r\nContent-Type: text/html\r\nContent-Length: 1159\r\nDate: Thu, 21 Feb 2013 01:34:14 GMT\r"..., last = 0x7269e5 "", file_pos = 0, file_last = 0, start = 0x726480 "HTTP/1.1 200 OK\r\nServer: Apache-Coyote/1.1\r\nSet-Cookie: JSESSIONID=89B390E44D0E9225A26A7BEE5F285628; Path=/examples\r\nContent-Type: text/html\r\nContent-Length: 1159\r\nDate: Thu, 21 Feb 2013 01:34:14 GMT\r"..., end = 0x72a480 "", tag = 0x6e99a0, file = 0x0, shadow = 0x0, temporary = 1, memory = 0, mmap = 0, recycled = 0, in_file = 0, flush = 0, sync = 0, last_buf = 0, last_in_chain = 0, last_shadow = 0, temp_file = 0, num = 0}


//代理读第二次,读取内容(体)第二次确保有没有问题
n = p->upstream->recv_chain(p->upstream, chain);
n = readv(c->fd, (struct iovec *) vec.elts, vec.nelts); c->fd = 10


合成后发送前:

recv //可以看到,消息头和消息体都在里面
buf = {pos = 0x726480 "HTTP/1.1 200 OK\r\nServer: Apache-Coyote/1.1\r\nContent-Type: text/html\r\nContent-Length: 1159\r\nDate: Tue, 19 Feb 2013 07:54:00 GMT\r\nConnection: close\r\n\r\n
阅读(5208) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~