etrace跟踪Nginx代码+ FASTCGI
http://blog.csdn.net/jianqiangchen/article/details/29175285
http://blog.csdn.net/jianqiangchen/article/details/29201415
http://blog.csdn.net/allenlinrui/article/details/19419721
nginx安装:
[root@monitor ~]# pwd
/root wget http://nginx.org/download/nginx-1.2.9.tar.gz tar -zxvf nginx-1.2..tar.gz
[root@slave1 nginx-1.2.]# pwd //configure
/root/nginx-1.2.
[root@slave1 nginx-1.2.]# ll
total
drwxr-xr-x Jun : auto
-rw-r--r-- May CHANGES
-rw-r--r-- May CHANGES.ru
drwxr-xr-x Jun : conf
-rwxr-xr-x May configure
drwxr-xr-x Jun : contrib
drwxr-xr-x Jun : html
-rw-r--r-- May LICENSE
drwxr-xr-x Jun : man
-rw-r--r-- May README
drwxr-xr-x Jun : src
[root@slave1 nginx-1.2.]# cd src //源代码目录
[root@slave1 src]# ll
total
drwxr-xr-x Jun : core
drwxr-xr-x Jun : event
drwxr-xr-x Jun : http
drwxr-xr-x Jun : mail
drwxr-xr-x Jun : misc
drwxr-xr-x May os
[root@slave1 src]# pwd
/root/nginx-1.2./src
Nginx的configure
1 ./configure -with-debug --with-cc-opt='-g -O0 -finstrument-functions'
[root@slave1 nginx-1.2.]# ll //产生objs目录 在objs产生真正Makefile文件
total
drwxr-xr-x Jun : auto
-rw-r--r-- May CHANGES
-rw-r--r-- May CHANGES.ru
drwxr-xr-x Jun : conf
-rwxr-xr-x May configure
drwxr-xr-x Jun : contrib
drwxr-xr-x Jun : html
-rw-r--r-- May LICENSE
-rw-r--r-- root root Jun : Makefile
drwxr-xr-x Jun : man
drwxr-xr-x root root Jun : objs
-rw-r--r-- May README
drwxr-xr-x Jun : src
2. 下载etrace
[root@slave1 ~]# pwd
/root
[root@slave1 ~]git clone https://github.com/elcritch/etrace.git
3. 将etrace代码加入Ngnix
将ptrace.c拷贝至Nginx源码core目录下.
core目录:
[root@slave1 core]# pwd
/root/nginx-1.2.9/src/core ptrace.c目录:
[root@slave1 src]# pwd
/root/etrace/src
[root@slave1 src]# ll
total
-rwxr-xr-x root root Jun : etrace
-rwxr-xr-x root root Jun : etrace.pl
-rw-r--r-- root root Jun : ptrace.c cp /root/etrace/src/ptreace.c /root/nginx-1.2.9/src/core/
cp /root/etrace/src/etrace /sbin/
cp /root/etrace/src/ptrace.pl /sbin/ 保证NGINX 与 etrace与etrace.pl在一个目录,目的是读取nginx产生的TRACE文件,然后转换为函数树
修改 /root/nginx-1.2.9/objs/Makefile
objs/nginx: objs/src/core/nginx.o \
objs/src/core/ptrace.o \ $(LINK) -o objs/nginx \
objs/src/core/nginx.o \
objs/src/core/ptrace.o \ //要加入的值 objs/src/core/nginx.o: $(CORE_DEPS) \
src/core/nginx.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/nginx.o \
src/core/nginx.c objs/src/core/ptrace.o: $(CORE_DEPS) \ //要加入值
src/core/ptrace.c
$(CC) -c $(CFLAGS) $(CORE_INCS) \
-o objs/src/core/ptrace.o \
src/core/ptrace.c 注意:拷贝后注意每个大项的下面,要重新以TAB健开头,空格不等于TAB
3.make
4.make install
/usr/local/nginx
/usr/local/nginx/sbin
/usr/local/nginx/sbin/nginx
/usr/local/nginx/html
/usr/local/nginx/html/index.html
/usr/local/nginx/html/50x.html
/usr/local/nginx/conf
/usr/local/nginx/conf/fastcgi_params.default
/usr/local/nginx/conf/mime.types
/usr/local/nginx/conf/scgi_params.default
/usr/local/nginx/conf/scgi_params
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/fastcgi.conf
/usr/local/nginx/conf/koi-win
/usr/local/nginx/conf/koi-utf
/usr/local/nginx/conf/nginx.conf.default
/usr/local/nginx/conf/mime.types.default
/usr/local/nginx/conf/uwsgi_params.default
/usr/local/nginx/conf/fastcgi_params
/usr/local/nginx/conf/uwsgi_params
/usr/local/nginx/conf/win-utf
/usr/local/nginx/conf/fastcgi.conf.default
/usr/local/nginx/logs
默认目录结构:
主目录:/usr/local/nginx/
配置目录:/usr/local/nginx/conf/
root目录:/usr/local/nginx/html/
可执行文件路径:/usr/local/nginx/sbin/
cp /usr/local/nginx/sbin/nginx /sbin/
5.启动 - 默认和特殊
/usr/local/nginx/sbin/nginx (默认启动方式)
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (指定配置文件启动)
6.
vi /usr/local/nginx/conf/nginx.conf 加入 daemon off;
master_process off;
7.开3个窗口
窗口1
cd /sbin
touch TRACE
etrace nginx 或者 etrace nginx >log
窗口2
cd /sbin
./nginx
窗口三
curl 127.0.0.1
研究一下CGI: http://www.cnblogs.com/skynet/p/4173450.html root@monitor ~]# cat /usr/local/nginx/conf/nginx.conf worker_processes ; root : ? :: nginx: master process ./nginx nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
nobody : ? :: nginx: worker process
spawn_fastcgi的安装、部署与配置
wget https://github.com/lighttpd/spawn-fcgi/archive/v1.6.3.tar.gz
[root@monitor spawn-fcgi-1.6.3]# pwd
/root/spawn-fcgi-1.6.3 [root@monitor spawn-fcgi-1.6.3]# ./autogen.sh
[root@monitor spawn-fcgi-1.6.3]# ./configure
[root@monitor spawn-fcgi-1.6.3]# make
编译好以后,将可执行文件移动到nginx的sbin目录下
cp ./src/spawn-fcgi /usr/local/nginx/sbin/ //(cp到nginx的安装目录下)
安装fastcgi开发包: [root@monitor ~]# wget http://down1.chinaunix.net/distfiles/fcgi-2.4.0.tar.gz
[root@monitor ~]# tar -zxvf fcgi-2.4.0.tar.gz
[root@monitor fcgi-2.4.0]# pwd
/root/fcgi-2.4.0 1./configure 2.make 3.make install //安装了什么
/usr/bin/install -c -m 644 fastcgi.h /usr/local/include/fastcgi.h
/usr/bin/install -c -m 644 fcgi_stdio.h /usr/local/include/fcgi_stdio.h
/usr/bin/install -c -m 644 fcgiapp.h /usr/local/include/fcgiapp.h
/usr/bin/install -c -m 644 fcgimisc.h /usr/local/include/fcgimisc.h
/usr/bin/install -c -m 644 fcgio.h /usr/local/include/fcgio.h
/usr/bin/install -c -m 644 fcgios.h /usr/local/include/fcgios.h
/usr/bin/install -c -m 644 fcgi_config.h /usr/local/include/fcgi_config.h 安装fastcgi开发包,报以下错误: fcgio.cpp: In destructor 'virtual fcgi_streambuf::~fcgi_streambuf()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::overflow(int)':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::sync()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp:: error: 'EOF' was not declared in this scope
fcgio.cpp: In member function 'virtual int fcgi_streambuf::underflow()':
fcgio.cpp:: error: 'EOF' was not declared in this scope
make[]: *** [fcgio.lo] Error
make[]: Leaving directory `/root/downloads/fcgi-2.4.-SNAP-/libfcgi'
make[]: *** [all-recursive] Error
make[]: Leaving directory `/root/downloads/fcgi-2.4.-SNAP-'
make: *** [all] Error 解决办法:
在/include/fcgio.h文件中加上 #include <stdio.h>,然后再编译安装就通过了。 CGI程序:
[root@monitor ~]# vi demo.cc
#include <fcgi_stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
int count = 0;
while (FCGI_Accept() >= 0) {
printf("Content-type: text/html\r\n"
"\r\n"
""
"FastCGI Hello!"
"Request number %d running on host%s "
"Process ID: %d\n", ++count, getenv("SERVER_NAME"), getpid());
}
return 0;
} [root@monitor ~]# ./demo
./demo: error while loading shared libraries: libfcgi.so.0: cannot open shared object file: No such file or directory 解决:
1.cp /usr/local/lib/libfcgi.so.0 /usr/lib
在/etc/ld.so.conf中加入
include /usr/lib 2./sbin/ldconfig -v [root@monitor ~]# ldd demo
linux-vdso.so.1 => (0x00007fff0a4f1000)
libfcgi.so.0 => /usr/lib/libfcgi.so.0 (0x00007f560e1cf000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007f560dec9000)
libm.so.6 => /lib64/libm.so.6 (0x0000003c4fc00000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f560dcb2000)
libc.so.6 => /lib64/libc.so.6 (0x0000003c4ec00000)
libnsl.so.1 => /lib64/libnsl.so.1 (0x0000003c51400000)
/lib64/ld-linux-x86-64.so.2 (0x0000003c4e400000) mkdir -p /usr/local/nginx/cgibin/
cp /root/deme /usr/local/nginx/cgibin/demo 指定spawn-fcgi与 demo 这个CGI关联
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088
spawn-fcgi: no FastCGI application given [root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23692 参数含义如下:
-f <fcgiapp> 指定调用FastCGI的进程的执行程序位置,根据系统上所装的情况具体设置。
-a <addr> 绑定到地址addr。
-p <port> 绑定到端口port。
-s <path> 绑定到unix socket的路径path。
-C <childs> 指定产生的FastCGI的进程数,默认为5。(仅用于PHP)
-P <path> 指定产生的进程的PID文件路径。
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,CentOS下可以使用apache用户,其他的根据情况配置,如nobody、www-data等。 浏览器上:
http://120.24.160.93/demo.cgi (.cgi不能省) FastCGI Hello!Request number 4 running on host Process ID: 23692 服务器上:
[root@monitor cgibin]# ps -ef|grep demo
root 23692 1 0 15:46 ? 00:00:00 /usr/local/nginx/cgibin/demo
demo 这个CGI一致在运行 还可以监听其他端口:
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23700
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9001 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23702
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9002 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23704
[root@monitor cgibin]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 9003 -f /usr/local/nginx/cgibin/demo
spawn-fcgi: child spawned successfully: PID: 23706 ctive Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:9003 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8088 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:9002 0.0.0.0:* LISTEN [root@monitor cgibin]# ps -ef|grep demo
root 23692 1 0 15:46 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23700 1 0 15:57 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23702 1 0 15:58 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23704 1 0 15:58 ? 00:00:00 /usr/local/nginx/cgibin/demo
root 23706 1 0 15:59 ? 00:00:00 /usr/local/nginx/cgibin/demo
重启nginx或者重新加载配置文件
重新加载配置文件
1.kill -HUP [pid]
2.重启nginx
service nginx restart
3.killall nginx ./nginx
kill -HUP 是让进程挂起,睡眠;
kill -9 六亲不认的杀掉
kill -15 正常的退出进程
killall命令用于杀死指定名字的进程(kill processes by name)
1. yum install php
PHP-CGI是PHP自带的FastCGI管理器
php-cgi -b 127.0.0.1:9000
php-cgi变更php.ini配置后需重启php-cgi才能让新的php-ini生效,不可以平滑重启
直接杀死php-cgi进程,php就不能运行了。(PHP-FPM和Spawn-FCGI就没有这个问题,守护进程会平滑从新生成新的子进程。)
2.Spawn-FCGI,PHP-FPM 都是fastcgi管理器
yum install php-fpm
PHP-FPM是一个PHP FastCGI管理器 专门服务PHP
Spawn-FCGI是一个通用的FastCGI管理服务器 通用的
PHP-FPM、Spawn-FCGI都是守护php-cgi的进程管理器
php-fpm,Spawn-FCGI的管理对象是php-cgi
fastcgi是协议
php-cgi只是解释PHP脚本的程序 [root@monitor ~]# /usr/sbin/php-fpm
[09-Jun-2016 17:59:33] NOTICE: fpm is running, pid 23943
[09-Jun-2016 17:59:33] NOTICE: ready to handle connections [root@monitor ~]# ps -ef|grep php-fpm
root 23943 23848 0 17:59 pts/0 00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache 23944 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23945 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23946 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23947 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23948 23943 0 17:59 pts/0 00:00:00 php-fpm: pool www
root 23964 23900 0 18:01 pts/1 00:00:00 grep php-fpm
[root@monitor ~]# kill -9 23943
[root@monitor ~]# ps -ef|grep php-fpm
apache 23944 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23945 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23946 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23947 1 0 17:59 pts/0 00:00:00 php-fpm: pool www
apache 23968 1 0 18:02 pts/0 00:00:00 php-fpm: pool www
root 23970 23900 0 18:02 pts/1 00:00:00 ps -ef spawn-fcgi管理php-cgi: [root@monitor ~]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 2000 -C 5 -f /usr/bin/php-cgi [root@monitor ~]# ps -ef|grep php-cgi
root 24007 1 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24008 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24009 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24010 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24011 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24012 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24017 23900 0 18:22 pts/1 00:00:00 grep php-cgi
[root@monitor ~]# kill -9 24008
[root@monitor ~]# ps -ef|grep php-cgi
root 24007 1 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24009 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24010 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24011 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24012 24007 0 18:19 ? 00:00:00 /usr/bin/php-cgi
root 24018 24007 0 18:23 ? 00:00:00 /usr/bin/php-cgi
root 24020 23900 0 18:23 pts/1 00:00:00 grep php-cgi [root@monitor ~]# /usr/local/nginx/sbin/spawn-fcgi -a 127.0.0.1 -u nobody -g nobody -p 2010 -C 5 -f /usr/bin/php-cgi
ps -ef|grep nobody
nobody 24023 1 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24024 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24025 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24026 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24027 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
nobody 24028 24023 0 18:26 ? 00:00:00 /usr/bin/php-cgi
etrace跟踪Nginx代码+ FASTCGI的更多相关文章
- etrace 跟踪 nginx之HTTP请求流程
curl 127.0.0.1 | | | \--ngx_epoll_process_events | | | | \--ngx_time_update | | | | | \--ngx_gmtime ...
- Nginx中FastCGI配置优化
FastCGI: FastCGI是从CGI发展改进而来的.传统CGI接口方式的主要缺点是性能很差,因为每次HTTP服务器遇到动态程序时都需要重新启动脚本解析器来执行解析,然后结果被返回给HTTP服务器 ...
- Linux上配置Nginx+PHP5(FastCGI)
原为地址:http://www.laruence.com/2009/07/28/1030.html Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,以事件驱动的方式编写,所以有非常好的性能,同时 ...
- Nginx 中 FastCGI 配置示例
nginx 中 FastCGI 参数:主要是在 http 层 :保证PHP环境的高校运行 主要对PHP用来解析 fastcgi_cache_path /tmp/fastcgi_cache levels ...
- nginx+python+fastcgi环境配置(flup版本)
昨天花了一整天的时间研究搭建了nginx+python+fastcgi环境,并测试没问题,由于是第一次,并且参考了网上很多东西,网上也有很多,但还是把自己的过程记录下. 主要感谢这位兄弟的文章给了我很 ...
- nginx、fastCGI、php-fpm关系梳理(转)
前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装php-fpm扩展并启动php-fpm守护进程,nginx ...
- Nginx + CGI/FastCGI + C/Cpp
接着上篇<Nginx安装与使用>,本篇介绍CGI/FASTCGI的原理.及如何使用C/C++编写简单的CGI/FastCGI,最后将CGI/FASTCGI部署到nginx.内容大纲如下: ...
- nginx、fastCGI、php-fpm关系梳理(转载 http://blog.sina.com.cn/s/blog_6df9fbe30102v57y.html)
前言: Linux下搭建nginx+php+memached(LPMN)的时候,nginx.conf中配需要配置fastCGI,php需要安装 php-fpm扩展并启动php-fpm守护进程, ...
- 利用Nginx+Mono+Fastcgi代替IIS对Asp.Net进行反向代理
Nginx的好处相信我不必多说了,它作为一个相当轻量级的开源Web 服务器以及反向代理服务器而深受欢迎.越来越多的公司已经对它产生兴趣,包括我们公司的许多部门,利用它进行负载均衡和资源管理,之前写过一 ...
随机推荐
- Xcode6插件开发
工欲善其事必先利其器,Xcode是我们做iOS Dev必须掌握的一款开发工具. Xcode本身也是一门Cocoa程序,与其来说它是一个Cocoa程序,是不是意味着,我们可以去动态去让它做某件事,或者监 ...
- IOC(控制反转)与DI(依赖注入)的个人理解。
控制反转IOC(Inversion of Control)的三个需要理清问题: 1.谁控制了谁,控制了什么东西?IOC容器控制了依赖对象的创建. 2.谁得到了反转? 一般的应用程序是,直接创建依赖于该 ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- maven web项目不能创建src/main/java等文件夹的问题
eclipse创建maevn web项目,在选择maven_archetype_webapp原型后,默认只有src/main/resources这个Source Floder. 按照maven目录结 ...
- Spring(一)——总体介绍
spring框架,是进行对象管理,对象关联,解耦的一个中间层框架.SSH(Struts+Spring+hibernate)三大Spring在中间就起着一个承上启下的作用.好,首先我们先来 ...
- rust尝鲜
官网 http://www.rust-lang.org/ 下载链接 http://static.rust-lang.org/dist/rust-0.10-i686-unknown-linux-gnu. ...
- ios6,ios7,ios7.1下设置UISearchbar的背景色
ios系统升级到7.1后,原来在7.0下显示正常的UISearchbar现在又出现问题了.究其原因,是由于UISearchbar的subview又做修改了. float version = [[[ U ...
- 用JQUERY为INPUT的TXT类型赋值及取值操作
注意和纯JS操作的区别,一个是对象,一个是字串,如下说明: 在Jquery中,用$("#id")来获得页面的input元素,其相当于document.getElementById( ...
- Zlib压缩算法在Java与Delphi间交互实现(压缩XML交互)
一个典型应用中,使用delphi作为客户端,J2EE服务端,两者之间用XML作为数据交换,为了提高效率,对XML数据进行压缩,为此需要找到一种压缩/解压算法能够两个平台之间交互处理,使用ZLIB算法就 ...
- Linux的IP设置参考
位置:etc/network/interfaces 内容: 第一段是网口1自动从DHCP处获得IP 第二段是网口2静态分配IP 如果是IPv6,请把 iface eth0 inet dhcp(stat ...