fast_cgi模块详解
nginx–location
语法规则: location [=||*|^~] /uri/ { … }
= 开头表示精确匹配
- ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
- 开头表示区分大小写的正则匹配
~* 开头表示不区分大小写的正则匹配
!和!*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
例子,有如下匹配规则:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ .(gif|jpg|png|js|css)$ {
#规则D
}
location ~* .png$ {
#规则E
}
location !~ .xhtml$ {
#规则F
}
location !~* .xhtml$ {
#规则G
}
location / {
#规则H
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A
访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
访问 http://localhost/static/a.html 将匹配规则C
访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C
访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。
访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在
安装与配置
准备文件:
lighttpd-1.4.15.tar.gz php-4.4.2.tar.gz mysql-5.0.20a.tar.gz
开始:
1 编译安装lighttpd
# tar zxvf lighttpd-1.4.15.tar.gz # cd lighttpd-1.4.15 # ls # ./configure --prefix=/usr/local/lighttpd # make # make install 创建网站根目录 # mkdir /usr/local/lighttpd/htdocs 创建配置文件放置目录 #mkdir /usr/local/lighttpd/etc创建日志目录#mkdir /usr/local/lighttpd/logs将配置文件拷贝到/usr/local/lighttpd/etc#cp doc/lighttpd.conf /usr/local/lighttpd/etc 启动lighttpd #/usr/local/lighttpd/bin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf
2 安装 MYSQL
# tar zxvf mysql-5.0.20a.tar.gz # cd mysql-5.0.20a# ./configure --prefix=/usr/local/mysql# make;make install# groupadd mysql# useradd -g mysql mysql# cp support-files/my-medium.cnf /etc/my.cnf# cd /usr/local/mysql/初始化# bin/mysql_install_db --user=mysql 运行 bin/mysqld_safe --user=mysql &设置自动启动(略)
3 安装 php (fast-cgi)
# tar zxvf php-4.4.2.tar.gz# cd php-4.4.2编译,加 --enable-fastcgi 和 --enable-force-cgi-redirect 选项# ./configure --prefix=/usr/local/php-fastcgi --enable-fastcgi --enable-force-cgi-redirect --with-zlib --with-gd --with-ttf --with-png --with-expat-dir=/usr --with-gmp --with-xml --with-mysql=/usr/local/mysql --with-charset=utf-8 --disable-debug --disable-posix --disable-rpath --enable-safe-mode --enable-magic-quotes --disabl e-dmalloc --enable-bcmath --enable-dio --enable-gd-native-ttf --enable-sysvsem --enable-sysvshm --enable-wddx --enable-versioning --enable-pic --enable-inline-optimization --enable-memory-limit --enable-mbstring --enable-m bregex --enable-mbstr-enc-trans --with-config-file-path=/usr/local/lib --enable-ftp --disable-debug --enable-tr ack-vars=yes --with-jpeg-dir --with-freetype-dir --enable-gd-native-ttf --enable-dl# make;make install
4 配置lighttpd
#vim /usr/local/lighttpd/etc/lighttpd.conf
修改一下内容
---------------- 加载cgi,fastcgi模块-------------14 server.modules = ( 15 "mod_rewrite", 16 # "mod_redirect", 17 # "mod_alias", 18 "mod_access", 19 # "mod_cml", 20 # "mod_trigger_b4_dl", 21 # "mod_auth", 22 # "mod_status", 23 # "mod_setenv", 24 "mod_fastcgi", 25 # "mod_proxy", 26 # "mod_simple_vhost", 27 # "mod_evhost", 28 # "mod_userdir", 29 "mod_cgi", 30 # "mod_compress", 31 # "mod_ssi", 32 # "mod_usertrack",----------------------------网站根目录--------------------------------- 38 ## a static document-root, for virtual-hosting take look at the 39 ## server.virtual-* options 40 server.document-root = "/usr/local/lighttpd/htdocs"----------------------------日志目录------------------------------42 ## where to send error-messages to 43 server.errorlog = "/usr/local/lighttpd/logs/lighttpd.error.log"..........115 #### accesslog module116 accesslog.filename = "/usr/local/lighttpd/logs/access.log"-----------------------------默认首页格式------------------------- 45 # files to check for if .../ is requested 46 index-file.names = ( "index.php", "index.html", 47 "index.htm", "default.htm" )-----------------------------FastCgi模块---------------------------206 #### fastcgi module207 ## read fastcgi.txt for more info208 ## for PHP don't forget to set cgi.fix_pathinfo = 1 in the php.ini209 fastcgi.server= (".php" =>210 (( "socket" => "/tmp/php.socket",211 "bin-path" => "/usr/local/php-fastcgi/bin/php",212 "min-procs" => 1,213 "max-procs" => 32,214 "max-load-per-proc" => 4,215 "idle-timeout" => 20216 ))217 )------------------------------CGI模块----------------------------------243 #### CGI module244 cgi.assign = (245 ".sh" => "/bin/bash", #shell script246 ".pl" => "/usr/bin/perl", #Perl247 ".cgi" => "" ) # c/c++248 #
5 修改设置 php.ini
cgi.fix_pathinfo = 1
6 配置完毕,重起lighttpd
查其PID# ps auxww |grep lighttpdkill掉# kill -9 PID启动# /usr/local/lighttpd/bin/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf
7.简单管理脚本
为了方便管理lighttpd,写几个简单的脚本
checkLighttpd.sh(检查lighttpd进程):-------------------------------------#!/bin/bashps auxww |grep lighttpd|grep -v grep-------------------------------------killLighttpd.sh(杀死lighttpd进程) :-------------------------------------#!/bin/bashLIGHTTPD_PID=`ps auxww |grep lighttpd |grep -v grep | awk '{print $2}'` kill -9 $LIGHTTPD_PID-------------------------------------startLighttpd.sh (启动lighttpd):------------------------------------- #!/bin/bash /usr/local/lighttpd/***in/lighttpd -f /usr/local/lighttpd/etc/lighttpd.conf-------------------------------------# chmod +x startLighttpd.sh killLighttpd.sh checkLighttpd.sh
8 测试:
用c,shell,perl,php分别写几个小程序作测试(略)
注: 经测试, php 要用