一个Nginx错误页面来统治所有

时间:2017-11-12 05:42:30

标签: php nginx error-handling

梦想

我有许多服务器块随风而来。我真的想创建和维护一个PHP模板来处理所有与服务器相关的错误(404,403等)...无论在哪个块上或在哪个域下发生,并且无需设置标准服务器错误页面逐块地。

我正在详细说明我的设置,认为它也可能是其他人认为有用且想要实施的解决方案。

设置

/etc/nginx/我有一个名为errors.conf的文件,应该尝试将错误传递给error.php

中的/var/www/admin/public文件
# All the errors...

error_page 401      /error.php?c=401;
error_page 403      /error.php?c=403;
error_page 404      /error.php?c=404;
error_page 500      /error.php?c=500;
error_page 502 503 504  /error.php?c=50x;

# Serve error.php from www/admin/public

location = /error.php {
    root /var/www/admin/public;
    include fastcgi.conf;
    auth_basic off;
}

errors.conf包含在每个服务器块中,通常看起来像这样。

server { 

    listen       80; 
    server_name  demosite.domain.com; 
    root   /var/www/demosite/public; 
    access_log  /var/www/demosite/logs/demosite.domain.com.access.log; 
    error_log  /var/www/demosite/logs/demosite.domain.com.error.log; 

    location = /favicon.ico { 
        return          204; 
        access_log      off; 
        log_not_found   off; 
    } 

    location / { 
        index  index.html index.htm index.php; 
    } 

    include errors.conf; 

    location ^~ /public/errors { 
        auth_basic      off;             
        allow           all; 
    } 

    # Pass the PHP scripts to FastCGI server listening on the php-fpm socket    
    location ~ \.php$ { 
        try_files       $uri =404; 
        fastcgi_pass    unix:/run/php/php7.0-fpm.sock; 
        fastcgi_index   index.php; 
        fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name; 

}

实际错误页面(error.php)的一个非常简单,非样式的版本可能看起来像这样......

    <!DOCTYPE html>

      <?php

      // Map error code to message string
      $_err = Array("401" => "Authorization Required",
          "403" => "Access Restricted",
          "404" => "File Not Found",
          "500" => "Some Sort of Server Error",
          "50x" => "Temporarily Unavailable");

      // Get the error string
      function errorString($error)
      {
          global $_err;

          foreach ($_err as $key => $val) {
              if ($key == $error)
                  return $val;
          }

          return "Unknown Error";
      }

      isset($_GET['c']) or die("Unknown Error");
      $error = $_GET['c'];
      $str = errorString($error);

      ?>


    <html>
      <head>
        <meta charset="UTF-8">
        <title><?= $error ?> - <?= $str ?></title>
      </head>

      <body>

      <main>
        <h1><?= $error ?> – <?= $str ?></h1>
        <h3>Something went wrong. <b>Horribly wrong.</b></h3>
      </main>

      </body>
    </html>

结果

现在发生的事情,例如,404错误是error.php文件未执行,但已下载。并且下载文件的标题是URL的最后一段。

因此,在尝试访问时,demosite.domain.com/blah/some_file.html将启动名为 some_file.html error.php文件的下载。

所以我们很接近!我相信修复相对简单。任何帮助或见解将不胜感激。

更新

fastcgi.conf如下......

    fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;
    fastcgi_param  REQUEST_SCHEME     $scheme;
    fastcgi_param  HTTPS              $https if_not_empty;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;

    # PHP only, required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

0 个答案:

没有答案