当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

2019年04月20日  | 移动技术网网络运营  | 我要评论
yii的nginx rewrite 如下为nginx yii的重写 server { set $host_path "/data/site/www.jb51.

yii的nginx rewrite

如下为nginx yii的重写

server {
set $host_path "/data/site/www.jb51.net";
 access_log /data/logs/nginx/www.jb51.net_access.log main;
server_name jb51.net www.jb51.net;
root $host_path/htdocs;
 set $yii_bootstrap "index.php";
# define charset
 charset utf-8;
location / {
 index  $yii_bootstrap;
 try_files $uri $uri/ /$yii_bootstrap?$args;
 }
# deny access to protected directories
 location ~ ^/(protected|framework|themes/w+/views) {
 deny all;
 }
#avoid processing of calls to unexisting static files by yii
 location ~ .(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
 try_files $uri =404;
 }
# prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
 location ~ /. {
 deny all;
 access_log off;
 log_not_found off;
 }
# php-fpm configuration using socket
 location ~ .php {
 fastcgi_split_path_info ^(.+.php)(.*)$;
#yii catches the calls to unexising php files
 set $fsn /$yii_bootstrap;
 if (-f $document_root$fastcgi_script_name){
 set $fsn $fastcgi_script_name;
 }
fastcgi_pass unix:/tmp/php5-fpm.sock; # 改成你对应的fastcgi
 include fastcgi_params;
 fastcgi_param script_filename $document_root$fsn;
#path_info and path_translated can be omitted, but rfc 3875 specifies them for cgi
 fastcgi_param path_info $fastcgi_path_info;
 fastcgi_param path_translated $document_root$fsn;
## tweak fastcgi buffers, just in case.
 fastcgi_buffer_size 128k;
 fastcgi_buffers 256 4k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;
 }
}

配置完了别忘了重启nginx。


cakephp的nginx重写规则
依然简单粗暴,直接上代码例子,nginx重写规则如下

server {
 listen 80;
 server_name www.jb51.net;
root /data/site/www.jb51.net;
 index index.php;
access_log /data/logs/nginx/www.jb51.net_accerss.log;
 error_log /data/logs/nginx/www.jb51.net_error.log;
# main cakephp rewrite rule
 location / {
 try_files $uri $uri/ /index.php?$uri&$args;
 }
location ~ .php$ {
 root /data/site/www.jb51.net;
 try_files $uri =404;
 fastcgi_pass unix:/tmp/php5-fpm.sock; # 改成你对应的fastcgi
 fastcgi_index index.php;
 fastcgi_param script_filename $document_root$fastcgi_script_name;
 include fastcgi_params;
 fastcgi_buffer_size 128k;
 fastcgi_buffers 256 4k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;
 }
}

重启nginx即可

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网