手机浏览 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条记录相关文章

常用网站的反向代理页[2013-09-28] (浏览: 66234, 评论: 10)
Yii CDbCriteria的常用方法 (浏览: 56524, 评论: 5)
超详细:在Mac OS X中配置Apache + PHP + MySQL (浏览: 40434, 评论: 1)
将Yiiframework与JQuery easyUI整合使用 (浏览: 38368, 评论: 2)
转:用XML-RPC协议读写WordPress上的文章 (浏览: 35704, 评论: 1)
a2enmod简介 (浏览: 34502, 评论: 0)
Yii:relations update(self::STAT) (浏览: 33930, 评论: 0)
WordPress 500错误 (浏览: 32161, 评论: 6)
利用.htaccess绑定域名到子目录 (浏览: 30311, 评论: 2)
值得收藏的yii2的doc中关于db Query的说明 (浏览: 29417, 评论: 0)

发表评论

评论内容 (必填):