【问题标题】:How do I configure phoenix /elixir apps to accept https requests如何配置 phoenix /elixir 应用程序以接受 https 请求
【发布时间】:2017-10-10 22:14:44
【问题描述】:

我创建了一些 phoenix 应用程序,默认情况下似乎都不接受 https 请求。我收到错误[error] Cowboy returned 400 and there are no headers in the connection.。 Http 请求按预期返回数据。

【问题讨论】:

  • 我不会这样做。我宁愿只用 Phoenix 服务 http 请求,让一些 http 服务器像 Nginx 服务 http 和 https 请求。 Nginx 会将它们代理到 Phoenix。易于设置和维护。

标签: elixir phoenix-framework


【解决方案1】:

我认为您需要在相关的config.exs 中配置您的应用程序的端点 - 类似于:

config :my_app, MyApp.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [scheme: "https", host: System.get_env("HOST"), port: 443],
  force_ssl: [rewrite_on: [:x_forwarded_proto]]

System.get_env("HOST") 是您的应用程序的网址...localhost:4000,在本地开发的情况下,我想。

文档 - http://wsmoak.net/phoenix/Phoenix.Endpoint.html

然后您需要添加 CORS 插件 - https://github.com/mschae/cors_plug

我构建了一个 Phoenix 应用程序来充当 API,并按照文档的说明,或多或少地像他们一样在我的路由器中设置它:

pipeline :api do
  plug CORSPlug, [origin: Application.get_env(:my_app, :client_url)]
  plug :accepts, ["json"]
end


scope "/api", PhoenixApp do
  pipe_through :api

  resources "/articles", ArticleController
  options   "/articles", ArticleController, :options
  options   "/articles/:id", ArticleController, :options
end

【讨论】:

  • 最好使用url: [scheme: "https", host: {:system, "HOST"}, port: 443], 而不是url: [scheme: "https", host: System.get_env("HOST"), port: 443],。第二个变体会将您的配置修复为构建时间环境变量 HOST。 {:system, "HOST"} 在运行时评估。
猜你喜欢
  • 2015-06-10
  • 2017-06-19
  • 2017-01-04
  • 2020-02-05
  • 2018-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多