cookie 存储期望 conn.secret_key_base 至少为 64 字节 - 但它已经

Jer*_*rry 6 elixir phoenix-framework

在生产中我有这个:

use Mix.Config

config :my_app, MyApp.Endpoint,
  secret_key_base: System.get_env("MYAPP_SECRET_KEY_BASE")
Run Code Online (Sandbox Code Playgroud)

该值在 /etc/environment 中设置并且足够长,我是从“mix phoenix.gen.secret”创建的。然而,它仍然抛出一个错误:

** (exit) an exception was raised:
        ** (ArgumentError) cookie store expects conn.secret_key_base to be at least 64 bytes
            (plug) lib/plug/session/cookie.ex:159: Plug.Session.COOKIE.validate_secret_key_base/1
            (plug) lib/plug/session/cookie.ex:152: Plug.Session.COOKIE.derive/3
            (plug) lib/plug/session/cookie.ex:103: Plug.Session.COOKIE.put/4
            (plug) lib/plug/session.ex:92: anonymous fn/3 in Plug.Session.before_send/2
            (elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
            (plug) lib/plug/conn.ex:961: Plug.Conn.run_before_send/2
            (plug) lib/plug/conn.ex:392: Plug.Conn.send_resp/1
            (my_app) web/controllers/home_controller.ex:1: MyApp.HomeController.action/2
Run Code Online (Sandbox Code Playgroud)

Gaz*_*ler 1

是否可以在编译应用程序之前设置该值?

如果您System.get_env("...")在配置内部使用,则在编译时检索该值。如果您按以下顺序执行操作,则可能不会使用您期望的环境变量值:

  1. 设置配置
  2. 编译应用程序
  3. 设置环境变量
  4. 运行应用程序

您可以通过运行来验证这是否是问题所在mix compile --force

如果按以下顺序执行步骤,则将使用正确的环境变量。

  1. 设置环境变量
  2. 设置配置
  3. 编译应用程序
  4. 运行应用程序