Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例
Yii的Nginxrewrite
如下为nginxyii的重写
server{ set$host_path"/data/site/www.nhooo.com"; access_log/data/logs/nginx/www.nhooo.com_access.logmain; server_namejb51.netwww.nhooo.com; root$host_path/htdocs; set$yii_bootstrap"index.php"; #definecharset charsetutf-8; location/{ indexindex.html$yii_bootstrap; try_files$uri$uri//$yii_bootstrap?$args; } #denyaccesstoprotecteddirectories location~^/(protected|framework|themes/w+/views){ denyall; } #avoidprocessingofcallstounexistingstaticfilesbyyii location~.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)${ try_files$uri=404; } #preventnginxfromservingdotfiles(.htaccess,.svn,.git,etc.) location~/.{ denyall; access_logoff; log_not_foundoff; } #php-fpmconfigurationusingsocket location~.php{ fastcgi_split_path_info^(.+.php)(.*)$; #yiicatchesthecallstounexisingPHPfiles set$fsn/$yii_bootstrap; if(-f$document_root$fastcgi_script_name){ set$fsn$fastcgi_script_name; } fastcgi_passunix:/tmp/php5-fpm.sock;#改成你对应的FastCGI includefastcgi_params; fastcgi_paramSCRIPT_FILENAME$document_root$fsn; #PATH_INFOandPATH_TRANSLATEDcanbeomitted,butRFC3875specifiesthemforCGI fastcgi_paramPATH_INFO$fastcgi_path_info; fastcgi_paramPATH_TRANSLATED$document_root$fsn; ##Tweakfastcgibuffers,justincase. fastcgi_buffer_size128k; fastcgi_buffers2564k; fastcgi_busy_buffers_size256k; fastcgi_temp_file_write_size256k; } }
配置完了别忘了重启Nginx。
CakePHP的Nginx重写规则
依然简单粗暴,直接上代码例子,nginx重写规则如下
server{ listen80; server_namewww.nhooo.com; root/data/site/www.nhooo.com; indexindex.php; access_log/data/logs/nginx/www.nhooo.com_accerss.log; error_log/data/logs/nginx/www.nhooo.com_error.log; #maincakephprewriterule location/{ try_files$uri$uri//index.php?$uri&$args; } location~.php${ root/data/site/www.nhooo.com; try_files$uri=404; fastcgi_passunix:/tmp/php5-fpm.sock;#改成你对应的FastCGI fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; includefastcgi_params; fastcgi_buffer_size128k; fastcgi_buffers2564k; fastcgi_busy_buffers_size256k; fastcgi_temp_file_write_size256k; } }
重启nginx即可