最新消息:

nginx: cache loader process 进程分析和header头输出

cache admin 4460浏览 0评论

背景:
Nginx模块fastcgi_cache后会在启动时候出现cache manager process和cache loader process 进程,查了下得知这玩意是用来作header使用的,PHP输出后由经它后给替换了header,有点像NAT出去时经过外网的路由时给把IP头给替换一个道理,这样有它的好处,也就是cache控制由前端nginx而不是由php本身,起到分层的作用:

[root@jackxiang conf]# ps aux|grep nginx  
root     30054  0.1  0.0 308184  1740 ?        Ss   20:44   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf  
www      30060  3.6  0.3 329396 23208 ?        S    20:44   0:00 nginx: worker process                                            
www      30062  3.5  0.3 329396 23208 ?        S    20:44   0:00 nginx: worker process                                                                                 
www      30069  0.0  0.0 308184  2224 ?        S    20:44   0:00 nginx: cache manager process                                     
www      30070  0.0  0.0 308184  2112 ?        S    20:44   0:00 nginx: cache loader process

程序代码是Discuz!论坛, 随便开启测试了几下,发现/dev/shm/nginx_cache/下没有任何目录建立,也没有文件创建。调试的http header响应头里的X-Cache-CFC 结果一直是MISS。从服务器进程上来看,Nginx cache manager process 跟Nginx cache loader process 进程也正常运行:

1  root      3100     1  0 14:52 ?        00:00:00 nginx: master process /usr/sbin/nginx
2  www-data  3101  3100  0 14:52 ?        00:00:00 nginx: worker process
3  www-data  3102  3100  0 14:52 ?        00:00:00 nginx: cache manager process
4  www-data  3103  3100  0 14:52 ?        00:00:00 nginx: cache loader process

不知道为何会这样,为何没有cache成功,我以为我配置参数有问题,只好阅读WIKI。发现fastcgi_ignore_headers 参数下解释有这么一段
fastcgi_ignore_headers
Syntax: fastcgi_ignore_headers field …
Default:
Context: http
server
location
Reference: fastcgi_ignore_headers

This directive forbids processing of the named headers from the FastCGI-server reply. It is possible to specify headers like “X-Accel-Redirect”, “X-Accel-Expires”, “Expires” or “Cache-Control”.

也就是说这个参数的值,将会被忽略掉,同样被忽略掉的响应头比如”X-Accel-Redirect”, “X-Accel-Expires”, “Expires” or “Cache-Control”,而nginx配置中并没有fastcgi_ignore_headers参数的设定,那么问题会不会出现在FASTCGI响应结果里包含了类似”X-Accel-Redirect”, “X-Accel-Expires”, “Expires” or “Cache-Control”这几个响应头呢?用strace抓包,看了下nginx与fpm进程通讯的数据
1  ####为了确保准确抓到处理该http请求的进程,我把nginx 、fpm都只开启了一个进程处理。
2  //strace -ff -tt -s 1000 -o xxx.log -p PHPFPM-PID
3  14:52:07.837334 write(3, “\1\6\0\1\0\343\5\0X-Powered-By: PHP/5.3.10-1ubuntu3.5\r\nExpires: Thu, 19 Nov 1981 08:52:00 GMT\r\nCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\nPragma: no-cache\r\nContent-type: text/html\r\n\r\nHello cfc4n1362034327\0\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0”, 256) = 256
4
5  //strace -ff -tt -s 1000 -o xxx.log -p Nginx-PID
6  15:05:13.265663 recvfrom(12, “\1\6\0\1\0\343\5\0X-Powered-By: PHP/5.3.10-1ubuntu3.5\r\nExpires: Thu, 19 Nov 1981 08:52:00 GMT\r\nCache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\nPragma: no-cache\r\nContent-type: text/html\r\n\r\nHello cfc4n1362035113\0\0\0\0\0\1\3\0\1\0\10\0\0\0\0\0\0\0\0\0\0”, 4023, 0, NULL, NULL) = 256

从抓取的数据包里可以看到,fpm确实返回了包含“Expires”、“Cache-Control”头的http 响应头信息。那么疑问来了:

nginx的fastcgi_cache没缓存这条http响应,是因为响应头里包含“Expires”、“Cache-Control”的原因吗?
程序里并没有输出“Expires”、“Cache-Control” http header的代码,这是谁输出的呢?
既然是fpm响应的时候,就已经有了,那么是php的core模块,还是其他拓展模块输出的?
“Expires:”时间为何是“Thu, 19 Nov 1981 08:52:00 GMT”?

疑问比较多,一个一个查起,先从Nginx的fastcgi_cache没缓存这条http响应查起。我根据测试环境nginx版本1.1.9(ubuntu 12.04默认的),到nginx官方下了对应版本的源码,搜索了fastcgi参数使用的地方,在http\ngx_http_upstream.c找到了。虽然不能很流程的读懂nginx的代码,但粗略的了解,根据了解的情况加以猜测,再动手测试实验,也得出了结论,确定了nginx的fastcgi_cache的规则。

转载请注明:爱开源 » nginx: cache loader process 进程分析和header头输出

您必须 登录 才能发表评论!