Gunicorn应用在GAE上异常:静态文件正常但接口请求返回503错误
问题:GAE Python环境下静态文件可访问但Entrypoint未运行
- 静态文件能正常访问,但配置的entrypoint完全未执行;曾尝试将
start.sh设为entrypoint并添加echo "Starting gunicorn"语句,日志中无任何相关输出 - 本地测试代码无问题,未使用Gunicorn时应用可正常运行
- 预期API接口与静态前端均能正常工作,但目前仅静态文件可用
原app.yaml配置
runtime: python39 entrypoint: gunicorn --worker-class eventlet -t 4 -w 1 --timeout 120 main:app -b :$PORT manual_scaling: instances: 1 network: session_affinity: true handlers: # API handlers - url: /api/.* script: auto secure: always redirect_http_response_code: 301 # Static file handlers - url: /(.*\.(html|css|js|png|jpg|jpeg|gif|ico|json|webp|svg)) static_files: frontend/dist/\1 upload: frontend/dist/(.*\.(html|css|js|png|jpg|jpeg|gif|ico|json|webp|svg)) secure: always # Catch-all handler for other static files and single-page app support - url: /.* static_files: frontend/dist/index.html upload: frontend/dist/index.html secure: always
解决方法
问题出在路由处理逻辑上,改为在Python应用中托管静态文件。针对单页应用(SPA),添加如下路由处理代码:
# Catch-all for SPA @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): if re.match("(.*\.(html|css|js|png|jpg|jpeg|gif|ico|json|webp|svg))", path): return app.send_static_file(path) return app.send_static_file("index.html")
内容的提问来源于stack exchange,提问作者Lukas Dobbles
相关产品推荐
相关产品推荐

