【问题标题】:How to get Phoenix to listen on IPv6?如何让 Phoenix 监听 IPv6?
【发布时间】:2016-09-21 02:08:26
【问题描述】:

我正在努力寻找有关让 Phoenix(以及 Cowboy 和 Ranch)同时监听 IPv4 和 IPv6 的信息。

我正在使用 Ubuntu 16.04 的 VPS 上运行。它同时具有 IPv4 和 IPv6 地址。 当运行netstat -tulpn 时,它显示beam 只在IPv4 上侦听,端口在Phoenix 配置中指定。 而其他进程,如 epmd,能够同时监听 IPv4 和 IPv6。

我尝试在config.exs 中指定类似"::4000" 的端口,但由于无法解析为整数而崩溃。

有人能指出我正确的方向吗?我是否需要以某种方式添加自定义 Cowboy 侦听器?

我宁愿不必在前面运行 Nginx 来监听 IPv6 流量。

我试过的配置:

config :my_app, MyApp.Endpoint,
  http: [port: "[::]:4000"]

堆栈跟踪(看起来 Phoenix 只需要一个整数端口):

** (Mix) Could not start application my_app: MyApp.start(:normal, []) returned an error: shutdown: failed to start child: MyApp.Endpoint
    ** (EXIT) shutdown: failed to start child: Phoenix.Endpoint.Server
        ** (EXIT) an exception was raised:
            ** (ArgumentError) argument error
                :erlang.binary_to_integer("[::]:4000")
                (phoenix) lib/phoenix/endpoint/server.ex:32: Phoenix.Endpoint.Server.to_port/1
                (phoenix) lib/phoenix/endpoint/server.ex:28: Phoenix.Endpoint.Server.default/3
                (phoenix) lib/phoenix/endpoint/server.ex:17: anonymous fn/5 in Phoenix.Endpoint.Server.init/1
                (elixir) lib/enum.ex:1623: Enum."-reduce/3-lists^foldl/2-0-"/3
                (phoenix) lib/phoenix/endpoint/server.ex:15: Phoenix.Endpoint.Server.init/1
                (stdlib) supervisor.erl:294: :supervisor.init/1
                (stdlib) gen_server.erl:328: :gen_server.init_it/6
                (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3

这也是凤凰{:phoenix, "~> 1.2.0"} 和牛仔{:cowboy, "~> 1.0"}

【问题讨论】:

标签: ipv6 phoenix-framework cowboy


【解决方案1】:
config :my_app, MyApp.Endpoint,
  http: [port: 4000, ip: {0, 0, 0, 0, 0, 0, 0, 0}]

这告诉 Phoenix 监听所有 IPv6 地址。它还侦听 IPv4。

我最初认为这是仅限 IPv6,因为 netstat -tulpn 将 beam 显示为仅 tcp6 而不是 tcp。但是经过测试,很明显它也在监听 IPv4 作为“IPv4 映射的 IPv6 地址”。

IP元组格式为Erlang类型inet:ip6_address()。

【讨论】:

    【解决方案2】:

    根据https://hexdocs.pm/phoenix/endpoint.html,您也可以只使用:inet6。这对我有用:

    config :my_app, MyApp.Endpoint,
      http: [:inet6, port: 4000],
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-18
    • 1970-01-01
    • 2018-05-27
    • 1970-01-01
    • 2021-09-21
    • 2018-02-28
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多