手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

yii:Apache and Nginx configurations

首页 > PHP Framework >

yii的urlmanager可以让项目在访问的时候隐藏index.php,也可以以更优雅的urlrewrite方式来显示,但这一切需要一些配置,在apache上的配置上就相对比较简单,直接参考wordpress的官方配置就完了,但其实很久以来,一直都没有人写过nginx下的配置。大家都是在根据wordpress配置来更改的,比如lnmp项目中,就是:

XML/HTML代码
  1. location / {  
  2. if (-f $request_filename/index.html){  
  3.                 rewrite (.*) $1/index.html break;  
  4.         }  
  5. if (-f $request_filename/index.php){  
  6.                 rewrite (.*) $1/index.php;  
  7.         }  
  8. if (!-f $request_filename){  
  9.                 rewrite (.*) /index.php;  
  10.         }  
  11. }  

不过,这两天在看官方guide的文档,原来这些问题,官方已经提供方案了:http://yii.neatcn.com/doc/guide/1.1/en/quickstart.apache-nginx-config#nginx

1. Apache

Yii is ready to work with a default Apache web server configuration. The .htaccess files in Yii framework and application folders restrict access to the restricted resources. To hide the bootstrap file (usually index.php) in your URLs you can add mod_rewrite instructons to the .htaccess file in your document root or to the virtual host configuration:

XML/HTML代码
  1. RewriteEngine on  
  2.   
  3. # if a directory or a file exists, use it directly  
  4. RewriteCond %{REQUEST_FILENAME} !-f  
  5. RewriteCond %{REQUEST_FILENAME} !-d  
  6. # otherwise forward it to index.php  
  7. RewriteRule . index.php 

2. Nginx

You can use Yii with Nginx and PHP with FPM SAPI. Here is a sample host configuration. It defines the bootstrap file and makes yii catch all requests to unexisting files, which allows us to have nice-looking URLs.

XML/HTML代码
  1. server {  
  2.     set $host_path "/www/mysite";  
  3.     access_log  /www/mysite/log/access.log  main;  
  4.   
  5.     server_name  mysite;  
  6.     root   $host_path/htdocs;  
  7.     set $yii_bootstrap "index.php";  
  8.   
  9.     charset utf-8;  
  10.   
  11.     location / {  
  12.         index  index.html $yii_bootstrap;  
  13.         try_files $uri $uri/ $yii_bootstrap?$args;  
  14.     }  
  15.   
  16.     location ~ ^/(protected|framework|themes/\w+/views) {  
  17.         deny  all;  
  18.     }  
  19.   
  20.     #avoid processing of calls to unexisting static files by yii  
  21.     location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {  
  22.         try_files $uri =404;  
  23.     }  
  24.   
  25.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  26.     #  
  27.     location ~ \.php {  
  28.         fastcgi_split_path_info  ^(.+\.php)(.*)$;  
  29.   
  30.         #let yii catch the calls to unexising PHP files  
  31.         set $fsn /$yii_bootstrap;  
  32.         if (-f $document_root$fastcgi_script_name){  
  33.             set $fsn $fastcgi_script_name;  
  34.         }  
  35.   
  36.         fastcgi_pass   127.0.0.1:9000;  
  37.         include fastcgi_params;  
  38.         fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;  
  39.   
  40.         #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI  
  41.         fastcgi_param  PATH_INFO        $fastcgi_path_info;  
  42.         fastcgi_param  PATH_TRANSLATED  $document_root$fsn;  
  43.     }  
  44.   
  45.     location ~ /\.ht {  
  46.         deny  all;  
  47.     }  
  48. }  

Using this configuration you can set cgi.fix_pathinfo=0 in php.ini to avoid many unnesessary system stat() calls.




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: yii, apache, nginx, wordpress, urlrewrite

« 上一篇 | 下一篇 »

只显示10条记录相关文章

Got error 28 from storage engine 解决方法 (浏览: 68331, 评论: 1)
php pack()函数详解与示例 (浏览: 63499, 评论: 10)
开始启用Yii的redis插件 (浏览: 46176, 评论: 2)
mongodb删除索引 (浏览: 40093, 评论: 0)
linux 查找目录或文件 (浏览: 36921, 评论: 1)
转:一个女软件工程师的征婚PPT (浏览: 36716, 评论: 5)
RabbitVCS 用后感 (浏览: 31778, 评论: 0)
身为WEB开发人员,真是痛苦,ietester解决部分痛苦 (浏览: 28723, 评论: 5)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 28325, 评论: 3)
虚拟机上iptables不支持nat转发? (浏览: 28219, 评论: 1)

发表评论

评论内容 (必填):