简体   繁体   English

适用于静态文件的Google App Engine自定义404页面

[英]Google App Engine custom 404 page for static files

I'm working on a GAE application which largely consists of static content. 我正在开发一个GAE应用程序,它主要由静态内容组成。 I've configured the following handlers: 我已经配置了以下处理程序:

- url: /content/(.*\..*)
  static_files: static/content/\1
  upload: static/content/(.*)

- url: /content/(.+)
  static_files: static/content/\1.html
  upload: static/content/(.*)\.html

The first handler is used to serve images, stylesheets, etc.; 第一个处理程序用于提供图像,样式表等; the second handles plain URLs like /content/zoo/monkeys/george and serves a corresponding HTML file. 第二个处理像/content/zoo/monkeys/george这样的普通URL,并提供相应的HTML文件。

Right now, GAE is returning an empty page if there is no corresponding static file for a URL. 现在,如果没有相应的URL静态文件,GAE将返回一个空页面。 I'd like to set up a custom 404 page for these cases, but apparently this is not straightforward. 我想为这些案例设置一个自定义的404页面,但显然这并不简单。

Answers to similar questions suggested putting a "catch-all" handler on the bottom of my app.yaml , with a RequestHandler that generates the error page. 对类似问题的回答建议在app.yaml的底部放置一个“catch-all”处理程序,并使用生成错误页面的RequestHandler However, /content/(.+) matches all URLs under /content/ , valid or not, which means such a handler won't get invoked. 但是, /content/ /content/(.+)匹配/content/下的所有URL,有效与否,这意味着不会调用这样的处理程序。

I can only think of two other solutions: 我只能想到另外两个解决方案:

  1. Route all requests through a dynamic handler, which writes out content for valid URLs, or an error page for invalid ones. 通过动态处理程序路由所有请求,动态处理程序为有效URL写入内容,或为无效URL写入错误页面。 I don't like this, because it is far less efficient than letting GAE serve the static files. 我不喜欢这个,因为它比让GAE提供静态文件效率低得多。
  2. Declare a separate static handler that explicitly matches each static file, and then put a "catch-all" handler at the bottom -- I don't like this either, because it would result in a long list of handlers. 声明一个单独的静态处理程序,它显式匹配每个静态文件,然后在底部放置一个“catch-all”处理程序 - 我也不喜欢这样,因为它会导致一长串处理程序。

Is there another way to set up a proper 404 page for this case? 还有另一种方法可以为这种情况设置一个合适的404页面吗?

Answer to an old thread for those that would find it useful. 回答那些会发现它有用的旧线程。

  1. Add require_matching_file: true as the last property of the handler. 添加require_matching_file: true作为处理程序的最后一个属性。

    If there is no corresponding file, the next handler in the waterfall will be invoked, instead of generating the default 404 page. 如果没有相应的文件,将调用瀑布中的下一个处理程序,而不是生成默认的404页面。

  2. Then add a new catch-all section at the bottom of app.yaml with a dynamic handler that processes invalid URLs 然后在app.yaml底部添加一个新的catch-all部分,其中包含一个处理无效URL的动态处理程序

Example: 例:

- url: /content/(.*\..*)
  static_files: static/content/\1
  upload: static/content/(.*)
  require_matching_file: true

- url: /content/(.+)
  static_files: static/content/\1.html
  upload: static/content/(.*)\.html
  require_matching_file: true

- url: /.*
  script: auto

This way GAE will serve all existing static assets through the first 2 handlers. 这样GAE将通过前两个处理程序为所有现有的静态资产提供服务。 The dynamic handler will be invoked only for non-existing URLs. 将仅针对不存在的URL调用动态处理程序。 You will need to write the code for that obviously... 你需要为此编写代码......

One big disclaimer here : require_matching_file is undocumented. 这里有一个很大的免责声明: require_matching_file没有记录。 But it turns out that GAE adds that field automatically to your app.yaml when you upload it. 但事实证明,当您上传时,GAE会自动将该字段添加到您的app.yaml

Try 'error_handlers'. 试试'error_handlers'。

error_handlers:
 - file: custom_404.html

From GAE App.yaml documentation - Each file entry indicates a static file that should be served in place of the generic error response. GAE App.yaml文档 - 每个文件条目指示应该提供的静态文件来代替通用错误响应。 If you specify a file element without a corresponding error_code element, the static file will be the default error page for your app. 如果指定的文件元素没有相应的error_code元素,则静态文件将成为应用程序的默认错误页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM