Nginx YII重写规则

qjp7pelc  于 2022-11-09  发布在  Nginx
关注(0)|答案(2)|浏览(179)

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

'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=/$1 [QSA,L]
</IfModule>

由于这是基于YII框架的,我采用了他们的Nginx示例配置,并以如下方式进行了修改:

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;
}

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

xiozqbni

xiozqbni1#

尝试更改您的

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

到这个

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

3bygqnnd2#

  1. nginx不使用htaccess文件;
    1.检查您的index.php.中的输入数据。在您的index.php.中插入“”。如果输出包含'_url =〉....',ngixn可以正常工作;
    变量转储($_REQUEST);
    1.您也可以随时使用nginx调试信息
    错误日志/路径/目标/日志调试;

相关问题