2014-09-01 29 views
2

最後我決定在這裏問小時試試,搜索和研究。奇怪的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中的下劃線和斜線在它的末尾,這重定向相當不錯,但如果我不放在最後一個斜槓即使運行條件塊。

回答

2

好的。我終於解決了它,就像我說的是一個非常小的細節(小細節總是最困難的)。

問題出在用於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沒有點'.'時才重定向。

我希望有人能夠用這個經驗解決未來的相關問題。

再見。