2017-10-07 81 views
1

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

+0

我不会这样做。我宁愿只为Phoenix提供http请求,让像Nginx这样的http服务器同时处理http和https请求。 Nginx会将它们代理给Phoenix。易于安装和维护。 –

回答

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

我建了一个凤凰的应用程序作为一个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 
+0

使用'url:[scheme:“https”,host:{:system,“HOST”},port:443],''而不是'url:[scheme:“https”,host:System.get_env(“ HOST“),端口:443],'。第二种变体将修复你的配置**编译时环境变量**'HOST'。 '{:system,“HOST”}'在运行时进行评估。 – djinn

+0

@djinn酷,感谢您的帮助提示。 – skwidbreth

相关问题