【发布时间】:2019-07-05 04:51:04
【问题描述】:
我正在使用 Cloudflare 的灵活 SSL 来保护我的网站,它在运行我的 vue.js 前端时运行良好。本质上,它所做的是与 50 个随机客户共享 SSL 证书,并且通过他们的边缘网络管道我的 DNS 来做到这一点。基本上,它完成了我需要的事情并且很好,但现在我试图将它绑定到 phoenix/elixir 后端,它正在崩溃。
问题是你不能从 ssl 页面内发出 http 请求,因为你会得到这个错误:
Blocked loading mixed active content
这是有道理的 - 如果加载时使用 ssl,则需要一直使用 ssl。所以现在我需要给 Elixir 添加 SSL。
这个网站 (https://elixirforum.com/t/run-phoenix-https-behind-cloudflare-without-key-keyfile/12660/2) 似乎有解决方案!他们的回答是:
configs = Keyword.put(config, :http, [:inet6, port: "80"])
|> Keyword.put(:url, [scheme: "https", host: hostname, port: "443"])
所以我做了这样的配置:
config :albatross, AlbatrossWeb.Endpoint,
http: [:inet6, port: "4000"],
url: [scheme: "https", host: "my.website", port: "443"],
secret_key_base: "SUPERSECRET",
render_errors: [view: AlbatrossWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Albatross.PubSub,
adapter: Phoenix.PubSub.PG2]
这只允许我访问 http:etc!所以我也试过这个:
config :albatross, AlbatrossWeb.Endpoint,
http: [:inet6, port: "4000"],
https: [ :inet6, port: "4443"],
url: [scheme: "https", host: "my.website", port: "443"],
secret_key_base: "SUPERSECRET",
render_errors: [view: AlbatrossWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Albatross.PubSub,
adapter: Phoenix.PubSub.PG2]
这当然行不通,因为没有 PEM 文件。因为我只使用 elixir 作为 API(而不是 DNS),所以我不能使用这样的解决方案(http://51percent.tech/blog/uncategorized/serving-phoenix-apps-ssl-and-lets-encrypt/),因为letsencrypt 不允许仅对 IP 地址进行身份验证(https://www.digitalocean.com/community/questions/ssl-for-ip-address)。
所以在这一点上我很困惑。有人有建议吗?
编辑:
有人提到你可以转到 crypto>Origin Certificates>Create Certificate 继续使用 cloudflare 并生成 TLS 证书。我这样做了,下载了文件,将它们保存在我的项目中并运行:
config :albatross, AlbatrossWeb.Endpoint,
http: [:inet6, port: "4000"],
https: [ port: "4443",
keyfile: "priv/ssl/cloudflare/private.key",
certfile: "priv/ssl/cloudflare/public.pem"],
url: [scheme: "https", host: "website.me", port: "443"],
secret_key_base: "SUPERSECRET",
render_errors: [view: AlbatrossWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Albatross.PubSub,
adapter: Phoenix.PubSub.PG2]
那么查询后端的所有可能方式的结果是什么?
我正在运行 docker-compose,所以 https://backendservice:4443 是我从前端查询的内容。这给了我 -
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://backendservice:4443/getComments?postnum=6. (Reason: CORS request did not succeed).[Learn More]
value of error : Error: "Network Error"
exports https://mywebsi.te/js/chunk-vendors.4345f11a.js:49:15423
onerror https://mywebsi.te/js/chunk-vendors.4345f11a.js:65:538540
actions.js:61:12
所以这显然行不通。
我可以去http://my.ip.address:4000,但我不能去https://my.ip.address:4443。
据我所知,cloudflare TLS 证书不起作用。
或者,更有可能的是,我在编写 elixir 配置时做了一些愚蠢的事情。
进一步澄清:
是的,上面有一个 CORS 标头错误。 然而请注意,它仅为 https 请求而不是 http 请求触发。为什么会发生这种情况非常令人困惑。我的应用程序入口点中有一个用于 elixir 的 cors 插件,该插件当前允许 * 传入请求。就是这样 - 它应该非常简单:
plug CORSPlug, origin: "*"
可以在此处找到更多信息 (https://github.com/mschae/cors_plug)。
【问题讨论】:
-
我可能是错的,但在这样的设置中,cloudflare 不会终止 SSL 并仅向您发送 HTTP 请求吗?这意味着您无需在 Phoenix 中设置任何内容。您可能只需要确保您的前端发送的是 HTTPS 请求而不是 HTTP 请求?
-
不,这不正确。如果我没有做任何特别的事情,我的后端将解析为MY.IP.ADDRESS/route。如果我 https 请求那里什么都没有,如果我 http 请求我得到
Blocked loading mixed active content错误。
标签: ssl networking elixir phoenix-framework cloudflare