使用Phoenix/Elixir在本地设置SSL时出现问题

Rob*_*Rob 3 ssl openssl elixir phoenix-framework

我在OSX上创建了一个新的Phoenix Web应用程序,我正在尝试让SSL在localhost上运行.为此,我阅读并执行了本文的步骤.所以现在我有一个server.key,server.crt和server.csr文件.这些文件不是二进制文件,并且是可读的形式.我将这些文件放在priv文件夹中,就像Phoenix文档所建议的那样.

我的配置文件如下所示:

  config :{{name}}, {{name}}.Endpoint,
  http: [port: 4000],
  debug_errors: true,
  code_reloader: true,
  cache_static_lookup: false,
  check_origin: false,
  watchers: [node: ["node_modules/brunch/bin/brunch", "watch", "--stdin"]],
  https: [port: 4043,
          otp_app: :{{name}},
          keyfile: System.get_env("server.key"),
          certfile: System.get_env("server.crt"),
          # OPTIONAL Key for intermediate certificates
          # cacertfile: System.get_env("INTERMEDIATE_CERTFILE_PATH")
        ]
Run Code Online (Sandbox Code Playgroud)

当我运行mix phoenix.server时,我收到以下错误:

** (Mix) Could not start application {{name}}: {{name}}.start(:normal, []) returned an error: shutdown: failed to start child: {{name}}.Endpoint
    ** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Server
        ** (EXIT) shutdown: failed to start child: {:ranch_listener_sup, {{name}}.Endpoint.HTTPS}
            ** (EXIT) shutdown: failed to start child: :ranch_acceptors_sup
                ** (EXIT) an exception was raised:
                    ** (MatchError) no match of right hand side value: {:error, {:options, {:certfile, nil}}}
                        (ranch) src/ranch_acceptors_sup.erl:30: :ranch_acceptors_sup.init/1
                        (stdlib) supervisor.erl:243: :supervisor.init/1
                        (stdlib) gen_server.erl:306: :gen_server.init_it/6
                        (stdlib) proc_lib.erl:237: :proc_lib.init_p_do_apply/3
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?我是凤凰新手,我正在开发的项目需要在localhost上使用SSL来防止跨域问题.

Ari*_*ird 6

看来凤凰无法找到你的证书.要解决此问题,您可以提供绝对路径,也可以利用otp_app来使用凤凰可以搜索证书的相对路径.如果你提供otp_app,phoenix将在你的应用程序根目录中查找证书.

如果你想提供一个绝对路径,你可以这样做:

    keyfile: Path.expand("../../../some/path/to/ssl/cer.key", __DIR__),
    certfile: Path.expand("../../../some/path/to/ssl/cer.crt", __DIR__)
Run Code Online (Sandbox Code Playgroud)

如果要利用otp_app,请创建两个env变量,例如KEY_HOME和CERT_HOME.转到控制台并触发这两个命令.您应该稍后将它们添加到您的bashrc文件中.

export KEY_HOME=priv/ssl/server.key
export CERT_HOME=priv/ssl/server.crt
Run Code Online (Sandbox Code Playgroud)

你必须在这里包含priv目录

现在您的配置看起来像这样

  https: [port: 443,
  otp_app: :hello_phoenix,
  keyfile: System.get_env("KEY_HOME"),
  certfile: System.get_env("CERT_HOME") 
  ]
Run Code Online (Sandbox Code Playgroud)

不要忘记在priv/ssl中复制文件.