Google App Engine 和 404 错误
我使用在别处找到的提示在 GAE 上设置了一个静态网站,但不知道如何返回 404 错误.我的 app.yaml 文件看起来像
I've setup a static website on GAE using hints found elsewhere, but can't figure out how to return a 404 error. My app.yaml file looks like
- url: (.*)/
static_files: static1/index.html
upload: static/index.html
- url: /
static_dir: static
所有静态 html/jpg 文件都存储在静态目录下.以上适用于存在的文件,但如果不存在则返回空长度文件.答案可能是编写一个 python 脚本来返回 404 错误,但是你如何设置它来为存在的静态文件提供服务,但为不存在的文件运行脚本?
with all the static html/jpg files stored under the static directory. The above works for files that exist, but returns a null length file if they don't. The answer is probably to write a python script to return a 404 error, but how do you set things up to serve the static files that exist but run the script for files that don't?
这是在开发应用服务器上获取一个不存在的文件 (nosuch.html) 的日志:
Here is the log from fetching a non-existent file (nosuch.html) on the development application server:
ERROR 2008-11-25 20:08:34,084 dev_appserver.py] Error encountered reading file "/usr/home/ctuffli/www/tufflinet/static/nosuch.html":
[Errno 2] No such file or directory: '/usr/home/ctuffli/www/tufflinet/static/nosuch.html'
INFO 2008-11-25 20:08:34,088 dev_appserver.py] "GET /nosuch.html HTTP/1.1" 404 -
您需要注册一个包罗万象的脚本处理程序.将此附加到您的 app.yaml 的末尾:
You need to register a catch-all script handler. Append this at the end of your app.yaml:
- url: /.*
script: main.py
在 main.py 中,您需要输入以下代码:
In main.py you will need to put this code:
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class NotFoundPageHandler(webapp.RequestHandler):
def get(self):
self.error(404)
self.response.out.write('<Your 404 error html page>')
application = webapp.WSGIApplication([('/.*', NotFoundPageHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
用有意义的东西替换.或者更好地使用模板,您可以在此处阅读如何做到这一点.
Replace <Your 404 error html page>
with something meaningful. Or better use a template, you can read how to do that here.
如果您在设置时遇到问题,请告诉我.
Please let me know if you have problems setting this up.