奇怪的php重定向与nginx(工作正常在阿帕奇)

问题描述:

最后我决定在这里问小时试试,搜索和研究。奇怪的php重定向与nginx(工作正常在阿帕奇)

几个月前,我将我的网站迁移到一个较新的版本,一切工作正常与我在Apache的旧托管。

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

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

重定向在apache(本地和remotelly)中工作得很好,但在nginx中却不行。

奇怪的行为是,例如,当我尝试使用URL fakesite.tk/Section/index.php apache重定向到fakesite.tk/Section/,但Nginx返回404错误,如果我尝试URL虚假。 tk/Section/index.php /神秘地工作(注意斜线在最后)并重定向到fakesite.tk/Section//(注意双斜杠)

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

重要的是,我的VPS在Ubuntu中运行(我的主机是iquals),我在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; 
} 

解决此问题。

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

编辑:我宁愿在URL的末尾添加尾部斜杠(以我干脆到发送的代码错误,如参数,以允许对网站进行管理,就像这个fcgi说避免404错误)使用适当的nginx虚拟主机配置文件,因为PHP无法正常工作。我只在server块中添加了以下行:#Adding trailing slash at the end rewrite ^([^.]*[^/])$ $1/ permanent;

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

我希望有人能够用这个经验解决未来的相关问题。

再见。