Nginx YII 重写规则

Nginx YII rewrite rule

我在网站的文档根目录下有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/ [L]
</IfModule>

'public' 子目录的内容:

/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=/ [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 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 last;
    }
    location /public {
        try_files $uri $uri/ /public/index.php?_url=/$uri;
    }

编辑 2 - 解决方案

我替换了这个:

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

通过这个:

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

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

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

  1. nginx 不使用 htaccess 文件;
  2. 将输入数据检查到您的index.php。将“”插入您的 index.php。如果输出包含 '_url => ....',ngixn 工作正常;

var_dump($_REQUEST);

  1. 另外你总是可以使用nginx调试信息

error_log /path/to/log debug;

尝试改变你的

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

到这个

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