gpt4 book ai didi

elixir - 如何在负载均衡器上将流量从 Http 重定向到 Phoenix Elixir for SSL 中的 Https?

转载 作者:行者123 更新时间:2023-12-04 14:23:37 25 4
gpt4 key购买 nike

我需要将流量从 Http 转发到 Https,SSL 在负载均衡器上,所以我需要做的就是使用 plug_ssl 转发 header 。所以在我的 prod.exs我补充说:

config :web, Web.Endpoint,
force_ssl: [rewrite_on: [:x_forwarded_proto]]

在我的 prod.exs并删除 config :web, Web.Endpoint 上的所有其他内容

它不起作用。

我究竟做错了什么?

我需要把 force_ssl在我的 router ?

先感谢您。

最佳答案

您可能需要启用 force_ssl仅插入不用于健康检查或与之相关的端点。

我在 GKE 上的 k8s Ingress 遇到了类似的问题,解决方案是放置 force_ssl 在路由器上 只针对我的申请路线,让 /healthz (配置为我的健康检查路由)使用 HTTP。
config/prod.exs的例子:

# Tells if we need to enforce SSL for our endpoints.
# Only production/staging should enforce
config :my_app, force_ssl: true

config :my_app, MyAppWeb.Endpoint,
load_from_system_env: true,
url: [host: "${APP_HOST}", port: 80],
server: true,
http: [port: "${PORT}"],
url: [scheme: "https", host: "${APP_HOST}", port: 443],
secret_key_base: "${SECRET_KEY_BASE}"

这是 lib/my_app_web/router.ex 的示例:

defmodule MyAppWeb.Router do
use MyAppWeb, :router

# This route will NOT enforce SSL
get("/healthz", MyAppWeb.HealthCheckController, :show)

pipeline :api do
plug(:accepts, ["json"])

if Application.get_env(:my_app, :force_ssl) do
plug(Plug.SSL, rewrite_on: [:x_forwarded_proto], host: nil)
end
end

scope "/", MyAppWeb do
pipe_through(:api)

# My routes will go here, and will be enforced if the
# application flag `force_ssl` is enabled.
end
end

这调试起来有点棘手。如果您正在使用 kubernetes ,尝试使用 kubectl describe ingress YOUR-INGRESS-HERE ,并在进行测试时始终检查请求和响应的 header 。

关于elixir - 如何在负载均衡器上将流量从 Http 重定向到 Phoenix Elixir for SSL 中的 Https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50752079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com