如何修复错误'无法检查Phoenix.Socket传输的来源'? (凤凰城1.2.1)

时间:2017-04-03 15:12:20

标签: elixir phoenix-framework subdomain

每次访问https://team_abc.dev.myapp.me/时都会出现以下错误

This issue might be specific for subdomains. Not very sure in what other contexts this issue arrises.

    07:26:06.498 [error] Could not check origin for Phoenix.Socket transport.

This happens when you are attempting a socket connection to
a different host than the one configured in your config/
files. For example, in development the host is configured
to "localhost" but you may be trying to access it from
"127.0.0.1". To fix this issue, you may either:

  1. update [url: [host: ...]] to your actual host in the
     config file for your current environment (recommended)

  2. pass the :check_origin option when configuring your
     endpoint or when configuring the transport in your
     UserSocket module, explicitly outlining which origins
     are allowed:

        check_origin: ["https://example.com",
                       "//another.com:888", "//other.com"]

07:26:20.174 [error] Could not check origin for Phoenix.Socket transport.

This happens when you are attempting a socket connection to
a different host than the one configured in your config/
files. For example, in development the host is configured
to "localhost" but you may be trying to access it from
"127.0.0.1". To fix this issue, you may either:

我在endpoint.ex

中有以下内容
plug Plug.Session,
store: :cookie,
key: "_myapp_key",
signing_salt: "RHWasYaA",
domain: ".dev.myapp.me"

并在我的prod.exs中跟随

http: [port: {:system, "PORT"}],
url: [host: "dev.myapp.me", port: 80]

我还在prod.exs中添加了

config :myapp, MyApp.Endpoint,
check_origin: ["https://dev.myapp.me", "https://myapp.me"]

有什么方法可以解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:9)

您应该在check_origin中添加解析到主机的所有域: check_origin: [..., "//*.myapp.me"]

编辑: 在阅读check_origin功能的源代码之后,似乎应该可以设置这样的通配符域名: match

更多详情:https://github.com/phoenixframework/phoenix/blob/da9f7653b9daf29a4c415be52a19ee6f4473e083/lib/phoenix/socket/transport.ex#L444