使用nginx进行奇怪的php重定向(在apache中运行良好)


Strange php redirections with nginx (Works fine in apache)

经过数小时的尝试、搜索和研究,我终于决定问这里。

几个月前,我将我的网站迁移到了一个新版本,在apache中的旧主机上一切都很好。

最近我迁移到一个VPS,在那里我决定使用Nginx作为服务器,我正在研究细节。

我的问题是,我试图使用PHP脚本进行一些特定的重定向。

重定向在apache(本地和远程)中运行得很好,但在nginx中则不然。

奇怪的行为是,例如,当我尝试使用URL fakesite.tk/Section/index.php时,apache重定向到fakesite.tek/Section/,但Nginx返回404错误,如果我尝试URL fakesite.tk/Section/index.php/神秘地工作(注意末尾的斜杠)并重定向到faksite.tk/Sections//(注意双斜杠)

我试着在所有URLS的末尾添加一个斜杠,但这种重定向在Nginx中同样不起作用。

如果重要的话,我的VPS在Ubuntu中运行(这就是我的主机),我在Windows机器上进行测试。

这里有我的Nginx站点配置文件:

server
{
         server_name *.fakesite.tk;
         return 301 $scheme://www.fakesite.tk$request_uri;
}
#Redirect non www to www site version
server
{
         server_name fakesite.tk;
         return 301 $scheme://www.fakesite.tk$request_uri;
}
server
{
        listen 80;
        listen [::]:80;
        root /var/www/SiteFolder;
        index index.php;
        server_name www.fakesite.tk;
        #Stabilishing error 404 and 403 error pages
        error_page 404 /?error=404;
        error_page 403 /?error=403;
        #Friendly URLs
        location /
        {
        location /
        {
                try_files $uri $uri/ =404;
                rewrite ^/([^/]*)/$ /?sect=$1 last;
                rewrite ^/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2 last;
                rewrite ^/([^/]*)/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2&cont=$3 last;
                rewrite ^/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ /?sect=$1&lang=$2&cont=$3&subcont=$4 last;
        }
        #Adding expire header
        location ~* '.(?:ico|css|js|gif|jpe?g|png|eot|svg|ttf|woff)$
        {
                expires 30d;
                add_header Pragma public;
                add_header Cache-Control "public";
        }
        #Enabling PHP
        location ~ '.php$
        {
                # With php5-fpm:
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
#                fastcgi_index index.php;
                include fastcgi_params;
        }
        #deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        location ~ /'.ht
        {
                deny all;
        }
}

还有我的php重定向脚本:

<?php 
    ini_set('display_errors', true);//Si estamos en local se muestran con normalidad los errores.
    error_reporting(E_ALL);
    $url = "http://$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]";
    $newurl = $url;
    $newurl = str_replace('_', '-', $url);//Reeplace all _ with -

    $newurl = str_replace('index.php', '', $newurl);//Removing a index.php
    $newurl = str_replace('.php', '', $newurl);//Removing all .php
    $newurl = str_replace('/Intro', '', $newurl);//Removing all intro sections
    $extens = preg_match('/'.(jpg|gif|png|jpeg|js|css|woff|html|eot|svg|ttf|xml|map|min|txt)/', $newurl);
    if($extens !== 1 && $newurl[strlen($newurl) - 1] !== '/') //Trying to put a slash at the end
    {
        $newurl.='/';
    }
    if($url !== $newurl)
    {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: $newurl");
    }
?>

我想这可能是我甚至看不到的非常小的细节,所以我感谢你的帮助。非常感谢。

编辑:我验证了var$newurl有效地更改了,并进入了if条件,但header(....);行没有执行,我在之后放了一个exit();命令,它正在执行,这意味着header(...);行实际上没有执行。

第2版:当我手动在URL中添加下划线并在其末尾添加斜线时,重定向效果很好,但如果我不在末尾添加斜线,即使在运行条件块时也不起作用。

好的,伙计们。我终于解决了,就像我说的是一个很小的细节(小细节总是最难的)。

问题是在nginx的虚拟主机文件中配置的用于PHP的fcgi

在原始文件(下面)中,我对fcgi说,尝试找到文件,如果不触发404 error,这是我的错误,因为它会触发他的自我错误,不允许我的网站管理它,所以重定向脚本不会执行。

#Enabling PHP
location ~ '.php$
{
    # With php5-fpm:
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

解决问题。我只是简单地对fcgi说,发送代码错误就像一个参数,允许网站管理它,就像这样:

#Enabling PHP
location ~ '.php$
{
    # With php5-fpm:
    #try_files $uri =404; #Mistake here!!
    try_files $uri /?error=404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

编辑:我更喜欢使用正确的nginx虚拟主机配置文件在URL的末尾添加尾部斜杠(以避免404错误),因为PHP无法正常工作。我只在server块内添加了以下行:#Adding trailing slash at the end rewrite ^([^.]*[^/])$ $1/ permanent;

请注意,只有当URL没有点'.'时,它才会重定向。

我希望将来有人能利用这段经历来解决一个相关的问题。

到时见。