经过数小时的尝试、搜索和研究,我最终决定在这里提问。
几个月前,我将网站迁移到更新版本,并且在 apache 中的旧托管上一切正常。
最近我迁移到一个 VPS,我决定使用 Nginx 作为服务器,并且正在研究细节。
我的问题是我正在尝试使用 PHP 脚本进行一些特定的重定向。
重定向在 apache(本地和远程)中工作得很好,但在 nginx 中则不然。
奇怪的行为是,例如当我尝试使用 URL fakesite.tk/Section/index.php apache 重定向到 fakesite.tk/Section/但 Nginx 返回 404 错误并且如果我尝试 URL fakesite.tk/Section/index.php/神秘地工作(注意末尾的斜杠)并重定向到 fakesite.tk/Section//(注意双斜杠)
我尝试在所有 URL 末尾添加斜杠,但这种重定向在 Nginx 中同样不起作用。
如果重要的话,我在 Ubuntu 中运行的 VPS(相当于我的主机)并且我在 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中手动添加下划线并在其末尾添加斜杠时,重定向效果非常好,但如果我不在末尾添加斜杠,即使运行条件 block ,它也不起作用。
最佳答案
好的,伙计们。我终于解决了,就像我说的一个很小的细节(小细节总是最难的)。
问题出在 nginx 虚拟主机文件中配置的 PHP fcgi
上。
在原始文件(如下)中,我对fcgi
说,尝试找到该文件,如果没有触发404错误
,这是我的错误,因为它触发了他自己错误并且不允许我的网站管理此错误,因此不会执行重定向脚本。
#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
block 中添加了以下行: #Adding Trailing斜杠 at the end rewrite ^([^.]*[^/])$ $1/permanent;
请注意,仅当 URL 没有点 '.'
时才会重定向。
我希望将来有人可以利用这次经验来解决相关问题。
到时候见。
关于php - 使用 nginx 进行奇怪的 php 重定向(在 apache 中工作正常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597696/