Nginx YII重写规则

时间:2015-07-22 13:15:27

标签: .htaccess nginx yii

我在网站的文档根目录下有3个文件夹:

/usr/share/nginx/example.com/public_html# ls -la
drwxr-xr-x 5 root root  73 Jul 17 07:28 app
drwxr-xr-x 4 root root  76 Jul 17 07:28 base
-rw-r--r-- 1 root root 211 Jul  8 14:32 .htaccess
drwxr-xr-x 4 root root  57 Jul 22 11:10 public

.htaccess的内容:

/usr/share/nginx/example.com/public_html# cat .htaccess
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule  (.*) public/$1 [L]
</IfModule>

&#39;公共&#39;的内容子目录:

/usr/share/nginx/example.com/public_html/public# ls -la
drwxr-xr-x 2 root root   27 Jul 17 07:28 css
-rw-r--r-- 1 root root  219 Jun 23 10:17 .htaccess
-rw-r--r-- 1 root root 6860 Jul 17 07:15 index.php
drwxr-xr-x 2 root root   26 Jul 17 07:15 js

最后是基础.htaccess的内容:

/usr/share/nginx/example.com/public_html/public# cat .htaccess 
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

由于这是基于YII框架,我采用了their Nginx sample configuration并以这种方式进行了修改:

server {
    access_log  /usr/share/nginx/example.com/logs/access.log  main;
    error_log  /usr/share/nginx/example.com/logs/error.log;
    rewrite_log on;

    server_name  example.com www.example.com;
    root   /usr/share/nginx/example.com/public_html;

    charset utf-8;

    index  index.php index.html;


    location / {
        rewrite ^(.*)$ /public$1 last;
    }
    location /public {
        try_files $uri $uri/ /public/index.php?_url=/$uri;
    }

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

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

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

        #let yii catch the calls to unexising PHP files
        set $fsn /index.php;
        if (-f $document_root$fastcgi_script_name){
            set $fsn $fastcgi_script_name;
        }

        fastcgi_pass   unix://var/run/php-fpm.sock;
        include fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

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

    # prevent nginx from serving dotfiles (.htaccess, .svn, .git, etc.)
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

这表示我在网站上访问的任何页面,每次只显示索引页面。

任何帮助表示赞赏。感谢。

修改1

为了使问题更清楚,我需要实现的是重写上面的htaccess文件,以便它在Nginx中正常工作。

这部分是我想出的:

    location / {
        rewrite ^(.*)$ /public$1 last;
    }
    location /public {
        try_files $uri $uri/ /public/index.php?_url=/$uri;
    }

编辑2 - 解决方案

我取而代之:

location / {
    rewrite ^(.*)$ /public$1 last;
}
location /public {
    try_files $uri $uri/ /public/index.php?_url=/$uri;
}

由此:

location / {
    rewrite ^(.*)$ /public/$1 last;
}

location /public {
    rewrite ^/public/(.*)$ /public/index.php?_url=$1;
}

我在第一次重写时添加了一个斜杠符号,并在第二次规则的情况下使用了重写而不是try_files。

2 个答案:

答案 0 :(得分:1)

尝试更改

location / {
    rewrite ^(.*)$ /public$1 last;
}
location /public {
    try_files $uri $uri/ /public/index.php?_url=/$uri;
}

到这个

location / {
rewrite ^/public/(.*)$   /public/index.php?$args;

}

答案 1 :(得分:0)

  1. nginx不使用htaccess文件;
  2. 将输入数据检入index.php。插入&#34;&#34;进入你的index.php。如果输出包含&#39; _url =&gt; ....&#39;,ngixn正常工作;
  3.   

    的var_dump($ _ REQUEST);

    1. 您也可以使用nginx调试信息
    2.   

      error_log / path / to / log debug;