(nginx)任何PHP重定向都可以正常工作,除了ErrorDocument 404中的那些

时间:2014-01-04 23:51:46

标签: php apache http redirect nginx

我正在运行一个从Apache迁移到nginx服务器的站点。在我的网站上, 404 ErrorDocument首先检查所访问的文件是否是缩短的链接。

如果是缩短的链接,我会将用户重定向到它解析的页面。如果没有,他们将留在404.php。

我的重定向看起来像这样:

<?php
    header("Location: $location");
?>

不幸的是,这不适用于404.php,但只适用于任何页面除了 404.php(包括其他ErrorDocuments),所以我认为问题可能在{{1 }或/etc/nginx/sites-available/default

我尝试取消注释与/etc/nginx/sites-available/myDomain.vhost有关的任何内容,但没有任何运气。

通过使用404myDomain.de/err/404.php调用ErrorDocument,一切正常,但是当您致电myDomain.de/404后,它将 正确重定向。

为什么myDomain.de/somethingThatDoesntExistOnTheServer是唯一拒绝正确重定向的网页?

这是配置文件。

的/ etc / nginx的/位点可用/默认值:

ErrorDocument 404

/etc/nginx/sites-available/myDomain.vhost:

server {
    listen   80; ## listen for ipv4; this line is default and implied
    listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    root /usr/share/nginx/www;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name localhost;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ /index.html;
        try_files $uri $uri/ err/404.php;
        # Uncomment to enable naxsi on this location
        # include /etc/nginx/naxsi.rules
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

        # With php5-cgi alone:
        fastcgi_pass 127.0.0.1:9000;
        # With php5-fpm:
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明,此行为不仅会影响404页面。这甚至可能是nginx中的一个错误。 我重新打开这个作为一个新问题,信息更清晰,代码更少。

修改

Phew,解决方案并不容易追踪。 :)

对于遇到此问题的每个人,只需在您配置错误文档的配置中添加“=”,就像我们刚刚在此处找到的那样,在此问题的更清晰版本中:

Redirect instead of 404 Error page - Status Code not working (Nginx)