小知识:Nginx配置PHP的Yii与CakePHP框架的rewrite规则示例

Yii的Nginx rewrite

如下为nginx yii的重写

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
server {
set $host_path “/data/site/www.tuohang.net“;
access_log /data/logs/nginx/www.tuohang.net_access.log main;
server_name tuohang.net www.tuohang.net;
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; # 改成你对应的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重写规则如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
server {
listen 80;
server_name www.tuohang.net;
root /data/site/www.tuohang.net;
index index.php;
access_log /data/logs/nginx/www.tuohang.net_accerss.log;
error_log /data/logs/nginx/www.tuohang.net_error.log;
# main cakephp rewrite rule
location / {
try_files $uri $uri/ /index.php?$uri&$args;
}
location ~ .php$ {
root /data/site/www.tuohang.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即可

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:PHP的Symfony和CodeIgniter框架的Nginx重写规则配置

2023-5-2 1:41:25

建站知识

小知识:在 Linux 上使用 kill 和 killall 命令来管理进程

2023-5-2 1:54:52

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索