求助-yii2在nginx上的rewrite配置

作者: php 发布时间: 2017-05-22 浏览: 4476 次 编辑

本文讲的是nginx配置yii2.0重写的例子, apache和nginx比起来,我更倾向nginx,占用资源少,且性能高,不管是开发还是生产环境都很好用。那么,如何配置nginx让其支持yii2.0的访问呢? 配置范例如下:

server {
  listen       80;
  server_name  www.xxxx.com;    #修改成你自己的网站域名
  location / {
          root   /www/wwwroot/yii2.0;    #修改成你网站的root目录
          index  index.html index.php;
          if (!-e $request_filename){
               rewrite ^/(.*) /index.php last;
          }
  }
  location ~ \.php$ {
          root           /www/wwwroot/yii2.0;   #修改成你网站的root目录
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
  }
}

扩展一下,如果想让yii2.0在nginx下支持rewrite路由(pathinfo)的形式访问的话

一.配置yii2.0。

打开config目录下的web.php,在$config = [ 'components'=>[ 加到这里 ] ]中加入:

'urlManager' => [
   'enablePrettyUrl' => true,
   'showScriptName' => false,
   'rules' => [
   ],
],

yii配置pathinfo

此时,yii2.0已经支持以pathinfo的形式访问了,如果此时访问不了,继续往下看。

二.配置web服务器。

1.如果是apache,在入口文件(index.php)所在的目录下新建一个文本文件,接着另存为.htaccess,用记事本打开此文件加入:

 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . index.php

保存即可。

2.如果是nginx,在nginx配置文件中加入:

server {
  listen       80;
  server_name  www.xxxx.com;    #修改成你自己的网站域名
  location / {
          root   /www/wwwroot/yii2.0;    #修改成你网站的root目录
          index  index.html index.php;
          if (!-e $request_filename){
               rewrite ^/(.*) /index.php last;
          }
  }
  location ~ \.php$ {
          root           /www/wwwroot/yii2.0;   #修改成你网站的root目录
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
  }
}

三:重启web服务器。

systemctl restart nginx.service

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索服务器 , 文件 配置 nginx yii url重写、yii2 重写 nginx、yii2.0 url重写、yii2.0 nginx rewrite、yii2.0 nginx配置,以便于您获取更多的相关知识。