首页 > 解决方案 > error_handlers 页面在部署时不显示

问题描述

我是谷歌应用引擎的新手,想在上面托管我的 php 网站。我发现有一个 error_handlers 选项可以将错误(缺少页面)重定向到自定义错误页面,但是在部署时,错误页面永远不会显示。

我已经使用 dev_appserver.py app.yaml 在本地服务器上进行了尝试,它可以工作。如果我输入一个不存在的文件,它将重定向到错误页面。但是当部署到网络上时,它就不起作用了。

这是我的 app.yaml。如果 URL 不是 php 或 html 文件,只需简单地重定向到 index.php。但是如果是php或者html文件但是没有找到,就会去error.html。

runtime: php55
api_version: 1

handlers:
# Serve images as static resources.
- url: /(.+\.(gif|png|jpg))$
  static_files: \1
  upload: .+\.(gif|png|jpg)$
  application_readable: true

# Serve php scripts.
- url: /(.+\.(php\html))$
  script: \1

- url: /.*
  script: index.php

error_handlers:
  - file: error.html

在本地服务器中,它工作正常并显示错误页面。部署时,缺少 php 或 html 会出现 404 错误。error.html 永远不会显示,即使 URL 是 (domain).com/error.html。error.html 将显示 404 错误

我想知道是否可以将丢失的页面重定向到自定义错误页面。

标签: phpgoogle-app-engineerror-handlinggoogle-cloud-platformapp.yaml

解决方案


在您的app.yaml中,我认为您缺少这样\的内容:- url: /(.+\.(php\html))$/(.+\.(php\\html))$

error_handlers仅适用于超时、超过配额和 dos api 拒绝错误。

在我看来,您正在将所有 url 重定向到 index.php 并为每个 url 创建适当的处理程序。在这种情况下,您应该使用set_error_handler捕获那里的错误。

对于 404,我将推荐以下内容:

handlers:
 -url:/
  script: index.php

 -url:/*.
  script: 404_custom_error.php

在这种情况下,任何无法在 index.php 中处理的 url 都将由 404 自定义错误 php 脚本处理,如app.yaml 示例所示。

我找到了两个讨论这个话题的公共问题跟踪器。第一个包含有关功能请求的信息,第二个说明基础设施不会拦截有效的 404 响应。


推荐阅读