手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:urlrewrite

yii:Apache and Nginx configurations

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