Nginx configures PHP's Yii and CakePHP framework's rewrite rule examples
- 2020-05-10 23:30:56
- OfStack
The Yii Nginx rewrite
The following is a rewrite of nginx yii
server {
set $host_path "/data/site/www.ofstack.com";
access_log /data/logs/nginx/www.ofstack.com_access.log main;
server_name ofstack.com www.ofstack.com;
root $host_path/htdocs;
set $yii_bootstrap "index.php";
# define charset
charset utf-8;
location / {
index index.html $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; # Change it to your equivalent 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;
}
}
Don't forget to restart Nginx once you've configured it.
Nginx rewrite rules for CakePHP
Again, the code example is straightforward. The nginx rewrite rule is as follows
server {
listen 80;
server_name www.ofstack.com;
root /data/site/www.ofstack.com;
index index.php;
access_log /data/logs/nginx/www.ofstack.com_accerss.log;
error_log /data/logs/nginx/www.ofstack.com_error.log;
# main cakephp rewrite rule
location / {
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ .php$ {
root /data/site/www.ofstack.com;
try_files $uri =404;
fastcgi_pass unix:/tmp/php5-fpm.sock; # Change it to your equivalent 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;
}
}
Just restart nginx