我在 Google Cloud App Engine Standard 上设置了一个 Flask Web 应用项目,当我部署它并访问网站时,它总是返回 404。我已经在本地测试了部署,它运行良好。日志没有显示错误。这是我的 app.yaml:
runtime: python39
entrypoint: gunicorn -b :$PORT -w 2 main:app
error_handlers:
- file: error.html
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /static
static_dir: static
# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
script: auto
当我运行“gcloud app logs tail -s default”并访问链接时,我收到以下日志:
2024-02-02 01:40:44 default[20240127t133812] "GET / HTTP/1.1" 200
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [11] [INFO] Starting gunicorn 21.2.0
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [11] [INFO] Listening at: http://0.0.0.0:8081 (11)
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [11] [INFO] Using worker: gthread
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [18] [INFO] Booting worker with pid: 18
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [21] [INFO] Booting worker with pid: 21
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [22] [INFO] Booting worker with pid: 22
2024-02-02 01:40:44 default[20240127t133812] [2024-02-02 01:40:44 +0000] [23] [INFO] Booting worker with pid: 23
2024-02-02 01:40:45 default[20240127t133812] "GET /static/style.css HTTP/1.1" 200
2024-02-02 01:40:45 default[20240127t133812] "GET /static/script.js HTTP/1.1" 200
2024-02-02 01:40:45 default[20240127t133812] "GET /favicon.ico HTTP/1.1" 404
2024-02-02 01:42:03 default[20240127t133812] "GET / HTTP/1.1" 200
这是我的main.py:
from flask import Flask, render_template, jsonify, request
from query_firestore import query
from google.cloud import firestore
@app.route('/')
def index():
locations = list_locations()
return render_template('index.html', locations=list(locations))
...
if __name__ == '__main__':
app.run(host="127.0.0.1", port=8080, debug=True)
我的目录中没有 /static/style.css 或 /static/script.js。它们被命名为 static/main.css 和 static/main.js。我在正确的目录中运行 deploy 命令并指定 app.yaml,但仍然收到此错误。