[英]How do I solve "Middleware "strapi::session": App keys are required" error in my deployment of Strapi to Heroku?
I'm following the official instructions to deploy my strapi starter app to Heroku.我按照官方说明将我的Strapi 入门应用程序部署到 Heroku。 The app runs fine locally.该应用程序在本地运行良好。 The only thing I left out in my deployment instructions were installing the PG node module (it is already installed because my local app uses Postgresql).我在部署说明中唯一遗漏的是安装 PG 节点模块(它已经安装,因为我的本地应用程序使用 Postgresql)。
Accessing the Heroku logs, I see this:访问 Heroku 日志,我看到:
error: Middleware "strapi::session": App keys are required.
Please set app.keys in config/server.js (ex: keys: ['myKeyA', 'myKeyB'])
Maybe this is an important detail: I followed this process once, and everything worked.也许这是一个重要的细节:我遵循了这个过程一次,一切正常。 I was able to deploy to Heroku.我能够部署到 Heroku。 I tried it again and it didn't work.我又试了一次,还是不行。 I was thinking maybe Heroku had a problem with me re-using an app name, but I tried to name the app something different in Heroku and I still had the same error.我在想也许 Heroku 对我重复使用应用程序名称有问题,但我试图在 Heroku 中为应用程序命名不同的名称,但我仍然遇到相同的错误。
Is heroku looking in the wrong place for my server.js file? heroku 是否在错误的位置查找我的 server.js 文件? Should it be looking in my "./config/env/production" folder instead of my "./config" folder?它应该在我的“./config/env/production”文件夹而不是我的“./config”文件夹中查找吗?
Per the instructions, here is my./config/env/production/database.js根据说明,这里是 my./config/env/production/database.js
const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
connection: {
client: 'postgres',
connection: {
host: config.host,
port: config.port,
database: config.database,
user: config.user,
password: config.password,
ssl: {
rejectUnauthorized: false
},
},
debug: false,
},
});
Here is my./config/env/production/server.js这是我的./config/env/production/server.js
module.exports = ({ env }) => ({
url: env('MY_HEROKU_URL'),
});
And here is my./config/server.js这是我的./config/server.js
module.exports = ({ env }) => ({
host: env('HOST', '0.0.0.0'),
port: env.int('PORT', 1337),
app: {
keys: env.array('APP_KEYS'),
},
});
my package.json for good measure:我的 package.json 很好:
{
"dependencies": {
"@strapi/plugin-graphql": "^4.0.0",
"@strapi/plugin-i18n": "4.0.6",
"@strapi/plugin-users-permissions": "4.0.6",
"@strapi/strapi": "4.0.6",
"lodash.set": "^4.3.2",
"pg": "8.6.0",
"pg-connection-string": "^2.5.0"
},
"name": "backend",
"private": true,
"version": "0.1.0",
"description": "A Strapi application",
"scripts": {
"develop": "strapi develop",
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi"
},
"devDependencies": {},
"author": {
"name": "A Strapi developer"
},
"strapi": {
"uuid": "f64b509e-2d95-4464-8d39-d6f0d1c7a31a",
"template": "@strapi/template-corporate@^1.0.0",
"starter": "@strapi/starter-next-corporate"
},
"engines": {
"node": ">=12.x.x <=16.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}
I'm running Node v14.18.3 and NPM v6.14.15我正在运行节点 v14.18.3 和 NPM v6.14.15
I solved it with this in ./config/env/production/server.js
我在./config/env/production/server.js
中解决了这个问题
module.exports = ({ env }) => ({
url: env("MY_HEROKU_URL"),
proxy: true,
app: {
keys: env.array("APP_KEYS", ["testKey1", "testKey2"]),
},
});
testKey1, testKey2 are just placeholders and need to be replaced by 2 random keys via CONFIG VAR in heroku testKey1、testKey2 只是占位符,需要通过 heroku 中的 CONFIG VAR 替换为 2 个随机密钥
APP_KEYS=someSecret,anotherSecret
proxy: true
was also important. proxy: true
也很重要。 Else it throws a Cannot send secure cookie over unencrypted connection
否则它会抛出Cannot send secure cookie over unencrypted connection
On Heroku, for that particular app, navigate to Settings->Config vars and add your environment variables there.在 Heroku 上,对于该特定应用程序,导航到Settings->Config vars并在那里添加您的环境变量。
just create .env
file in root of your project like this:只需在项目的根目录中创建.env
文件,如下所示:
HOST=0.0.0.0
PORT=1337
APP_KEYS=jP8pb1lYsAhnmURarewxhA==,34xnLMYHY5jiU7ONTstTqQ==
So you just need to create a variable in Heroku settings->config vars called APP_KEYS
.所以你只需要在 Heroku settings->config vars 中创建一个名为APP_KEYS
的变量。 And value of this variable you can get from your .env
file where you should have APP_KEYS
variable with value.这个变量的值你可以从你的.env
文件中得到,你应该有APP_KEYS
变量的值。
Adding the environment variables to your file as @Temo mentioned is not the right solution.正如@Temo 提到的那样,将环境变量添加到您的文件中并不是正确的解决方案。 Although it works it poses quite some security threats.尽管它有效,但它带来了相当多的安全威胁。
What you should do is add the APP_KEYS
to your environment variables on Heroku.您应该做的是将 APP_KEYS 添加到APP_KEYS
上的环境变量中。 You can generate a new key by creating a file with this code:您可以通过使用以下代码创建文件来生成新密钥:
// filename: generateCode.js
const crypto = require('crypto')
console.log(crypto.randomBytes(16).toString('base64'))
and then running it from the console with:然后从控制台运行它:
node generateCode.js
The code it generates looks something like foP7OJcuRhCw1sTR6EfZPw==
.它生成的代码类似于foP7OJcuRhCw1sTR6EfZPw==
。 Use that as your APP_KEY in Heroku.将其用作 Heroku 中的 APP_KEY。
Just remove.env from git ignore.只需从 git 中删除 .env 即可忽略。 Then push again.然后再推。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.