PHPIN.NET

 找回密码
 立即注册
查看: 5655|回复: 0

[基础应用] 配置Apache及Nginx实现Yii框架的URL美化

[复制链接]

469

主题

31

回帖

5509

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5509
发表于 2015-1-19 21:48:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
配置Apache及Nginx实现Yii框架的URL美化
1. Apache

Yii已经做好了默认以Apache服务器运行的配置部署。Yii框架和应用文件夹中的.htaccess 文件严格控制受限制资源的访问, 如果想要在URL中隐藏入口脚本 (通常是 index.php),可以在应用根目录下的.htaccess文件中添加 mod_rewrite 功能,或者配置虚拟主机来实现URL的美化:
  1. RewriteEngine on

  2. # prevent httpd from serving dotfiles (.htaccess, .svn, .git, etc.)
  3. RedirectMatch 403 /\..*$
  4. # if a directory or a file exists, use it directly
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteCond %{REQUEST_FILENAME} !-d
  7. # otherwise forward it to index.php
  8. RewriteRule . index.php
复制代码
2.Nginx
这里是一个简单的Nginx配置示例,里面定义了启动文件并且使得yii可以获取所有不存在的文件请求,从而实现URL的美化。
  1. server {
  2.     set $host_path "/www/mysite";
  3.     access_log  /www/mysite/log/access.log  main;

  4.     server_name  mysite;
  5.     root   $host_path/htdocs;
  6.     set $yii_bootstrap "index.php";

  7.     charset utf-8;

  8.     location / {
  9.         index  index.html $yii_bootstrap;
  10.         try_files $uri $uri/ /$yii_bootstrap?$args;
  11.     }

  12.     location ~ ^/(protected|framework|themes/\w+/views) {
  13.         deny  all;
  14.     }

  15.     #avoid processing of calls to unexisting static files by yii
  16.     location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
  17.         try_files $uri =404;
  18.     }

  19.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  20.     #
  21.     location ~ \.php {
  22.         fastcgi_split_path_info  ^(.+\.php)(.*)$;

  23.         #let yii catch the calls to unexising PHP files
  24.         set $fsn /$yii_bootstrap;
  25.         if (-f $document_root$fastcgi_script_name){
  26.             set $fsn $fastcgi_script_name;
  27.         }

  28.         fastcgi_pass   127.0.0.1:9000;
  29.         include fastcgi_params;
  30.         fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

  31.         #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI
  32.         fastcgi_param  PATH_INFO        $fastcgi_path_info;
  33.         fastcgi_param  PATH_TRANSLATED  $document_root$fsn;
  34.     }

  35.     # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
  36.     location ~ /\. {
  37.         deny all;
  38.         access_log off;
  39.         log_not_found off;
  40.     }
  41. }
复制代码
使用这个配置你可以在php.ini设置 cgi.fix_pathinfo=0 来避免许多不必要的系统stat() 调用.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|PHPIN.NET ( 冀ICP备12000898号-14 )|网站地图

GMT+8, 2024-4-25 21:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表