Python 部署在 Elastic Beanstalk 上的 Flask 服务器出现 AWS 内部服务错误,日志显示 SECRET_KEY 从未设置?
Python Flask server deployed on Elastic Beanstalk with AWS Internal Service Error, Logs say SECRET_KEY was never set?
我正在尝试使用 Elastic Beanstalk 将我的 Flask 应用程序部署到 AWS。我的应用程序在本地主机上运行完美,但我的应用程序在部署时遇到了问题。我不断收到内部服务错误,当我检查 AWS 上的日志时,他们一直说我的 secret_key 从未设置。以下是来自 AWS 的日志:
Jul 7 05:25:03 ip-172-31-10-200 web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py",
line 1821, in handle_user_exception Jul 7 05:25:03 ip-172-31-10-200
web: reraise(exc_type, exc_value, tb) Jul 7 05:25:03 ip-172-31-10-200
web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/_compat.py",
line 39, in reraise Jul 7 05:25:03 ip-172-31-10-200 web: raise value
Jul 7 05:25:03 ip-172-31-10-200 web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py",
line 1950, in full_dispatch_request Jul 7 05:25:03 ip-172-31-10-200
web: rv = self.dispatch_request() Jul 7 05:25:03 ip-172-31-10-200
web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py",
line 1936, in dispatch_request Jul 7 05:25:03 ip-172-31-10-200 web:
return self.view_functionsrule.endpoint Jul 7
05:25:03 ip-172-31-10-200 web: File "/var/app/current/application.py",
line 94, in submit_temperature Jul 7 05:25:03 ip-172-31-10-200 web:
flash('Scores generated successfully!') Jul 7 05:25:03
ip-172-31-10-200 web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/helpers.py",
line 423, in flash Jul 7 05:25:03 ip-172-31-10-200 web:
session["_flashes"] = flashes Jul 7 05:25:03 ip-172-31-10-200 web:
File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/werkzeug/local.py",
line 350, in setitem Jul 7 05:25:03 ip-172-31-10-200 web:
self._get_current_object()[key] = value Jul 7 05:25:03
ip-172-31-10-200 web: File
"/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/sessions.py",
line 103, in _fail Jul 7 05:25:03 ip-172-31-10-200 web: "The session
is unavailable because no secret " Jul 7 05:25:03 ip-172-31-10-200
web: RuntimeError: The session is unavailable because no secret key
was set. Set the secret_key on the application to something unique
and secret.
我已经研究了为什么它可以在本地主机上运行而不是在生产环境中运行,之前我在“if name ==”中设置了 application.secret_key main':" 块,而 Elastic Beanstalk 运行我的应用程序的方式会跳过该块。我的目录结构如下所示:
application/
__init__.py
static/
templates/
application.py
dbhelpers.py
rankgraph.py
scoring_algorithm.py
config.py
我更改了设置密钥的位置,并且尝试了两种不同的方法。这是我设置密钥的地方。
from tinydb import TinyDB
from flask import Flask, render_template, redirect, url_for, request, flash, make_response, session
import os, time, dbhelpers, rankgraph, scoring_algorithm, config
application = app = Flask(__name__)
application.debug = True
application.secret_key = 'asfwoewb09bew'
print(application.config)
db = TinyDB('db.json')
我也在这里设置它,因为第一种方法对我来说仍然没有用,这是我的 init.py.
from flask import Flask
application = Flask(__name__)
application.config.from_object('config')
这是我的 config.py
SECRET_KEY = 'dsaf0897sfdg45sfdgfdsaqzdf98sdf0a'
这里还有 link 到存储库,以防我没有提供足够的信息。 https://github.com/human37/swmac_flask_project
根据评论。
问题的原因尚未确定。
但是,在 Microsoft Azure
上部署相同的应用程序时找到了解决方案。
我正在尝试使用 Elastic Beanstalk 将我的 Flask 应用程序部署到 AWS。我的应用程序在本地主机上运行完美,但我的应用程序在部署时遇到了问题。我不断收到内部服务错误,当我检查 AWS 上的日志时,他们一直说我的 secret_key 从未设置。以下是来自 AWS 的日志:
Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception Jul 7 05:25:03 ip-172-31-10-200 web: reraise(exc_type, exc_value, tb) Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise Jul 7 05:25:03 ip-172-31-10-200 web: raise value Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request Jul 7 05:25:03 ip-172-31-10-200 web: rv = self.dispatch_request() Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request Jul 7 05:25:03 ip-172-31-10-200 web: return self.view_functionsrule.endpoint Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/current/application.py", line 94, in submit_temperature Jul 7 05:25:03 ip-172-31-10-200 web: flash('Scores generated successfully!') Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/helpers.py", line 423, in flash Jul 7 05:25:03 ip-172-31-10-200 web: session["_flashes"] = flashes Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/werkzeug/local.py", line 350, in setitem Jul 7 05:25:03 ip-172-31-10-200 web: self._get_current_object()[key] = value Jul 7 05:25:03 ip-172-31-10-200 web: File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/flask/sessions.py", line 103, in _fail Jul 7 05:25:03 ip-172-31-10-200 web: "The session is unavailable because no secret " Jul 7 05:25:03 ip-172-31-10-200 web: RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
我已经研究了为什么它可以在本地主机上运行而不是在生产环境中运行,之前我在“if name ==”中设置了 application.secret_key main':" 块,而 Elastic Beanstalk 运行我的应用程序的方式会跳过该块。我的目录结构如下所示:
application/
__init__.py
static/
templates/
application.py
dbhelpers.py
rankgraph.py
scoring_algorithm.py
config.py
我更改了设置密钥的位置,并且尝试了两种不同的方法。这是我设置密钥的地方。
from tinydb import TinyDB
from flask import Flask, render_template, redirect, url_for, request, flash, make_response, session
import os, time, dbhelpers, rankgraph, scoring_algorithm, config
application = app = Flask(__name__)
application.debug = True
application.secret_key = 'asfwoewb09bew'
print(application.config)
db = TinyDB('db.json')
我也在这里设置它,因为第一种方法对我来说仍然没有用,这是我的 init.py.
from flask import Flask
application = Flask(__name__)
application.config.from_object('config')
这是我的 config.py
SECRET_KEY = 'dsaf0897sfdg45sfdgfdsaqzdf98sdf0a'
这里还有 link 到存储库,以防我没有提供足够的信息。 https://github.com/human37/swmac_flask_project
根据评论。
问题的原因尚未确定。
但是,在 Microsoft Azure
上部署相同的应用程序时找到了解决方案。