gpt4 book ai didi

javascript - 自定义域 WebSocket 握手错误

转载 作者:数据小太阳 更新时间:2023-10-29 05:37:44 26 4
gpt4 key购买 nike

我的服务器上不断出现此错误:[error] Could not check origin for Phoenix.Socket transport.

我在 myapp.herokuapp.com 上有一个在 heroku 上运行的 Phoenix 和 JS 应用程序,它使用了 channel 。

我关注了 heroku custom domain在将我的 prod.exs 配置更改为:url: [scheme: "https", host: "myapp.com", port: 443] 后,现在无法加载我的应用程序的 javascript
从: url: [方案: "https", 主机: "myapp.herokuapp.com", 端口: 443]

使用自定义域 myapp.com 访问应用程序时出现此 JS 错误:

WebSocket connection to 'wss://myapp.com/socket/websocket?token=undefined&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 400

随附的 heroku 日志:

2016-11-27T03:27:52.259269+00:00 heroku[router]: at=info method=GET path="/socket/websocket?token=undefined&vsn=1.0.0" host=myapp.com request_id=94bd304b-e8f5-44aa-aff0-9336425e40b3 fwd="2601:602:9301:2b00:3c91:6660:9593:ad2c,2601:0602:9301:2b00:3c91:6660:9593:ad2c,108.162.245.66" dyno=web.1 connect=1ms service=1ms status=400 bytes=119
2016-11-27T03:28:03.671267+00:00 heroku[router]: at=info method=GET path="/socket/websocket?token=undefined&vsn=1.0.0" host=myapp.herokuapp.com request_id=71483e0c-8f2a-4485-8506-5931513de1fb fwd="50.159.116.207" dyno=web.1 connect=0ms service=1ms status=403 bytes=195
2016-11-27T03:28:03.650158+00:00 app[web.1]: 03:28:03.649 [error] Could not check origin for Phoenix.Socket transport.
2016-11-27T03:28:03.650173+00:00 app[web.1]:
2016-11-27T03:28:03.650174+00:00 app[web.1]: This happens when you are attempting a socket connection to
2016-11-27T03:28:03.650175+00:00 app[web.1]: a different host than the one configured in your config/
2016-11-27T03:28:03.650175+00:00 app[web.1]: files. For example, in development the host is configured
2016-11-27T03:28:03.650176+00:00 app[web.1]: to "localhost" but you may be trying to access it from
2016-11-27T03:28:03.650177+00:00 app[web.1]: "127.0.0.1". To fix this issue, you may either:
2016-11-27T03:28:03.650178+00:00 app[web.1]:
2016-11-27T03:28:03.650178+00:00 app[web.1]: 1. update [url: [host: ...]] to your actual host in the
2016-11-27T03:28:03.650179+00:00 app[web.1]: config file for your current environment (recommended)
2016-11-27T03:28:03.650179+00:00 app[web.1]:
2016-11-27T03:28:03.650180+00:00 app[web.1]: 2. pass the :check_origin option when configuring your
2016-11-27T03:28:03.650180+00:00 app[web.1]: endpoint or when configuring the transport in your
2016-11-27T03:28:03.650181+00:00 app[web.1]: UserSocket module, explicitly outlining which origins
2016-11-27T03:28:03.650182+00:00 app[web.1]: are allowed:
2016-11-27T03:28:03.650182+00:00 app[web.1]:
2016-11-27T03:28:03.650183+00:00 app[web.1]: check_origin: ["https://example.com",
2016-11-27T03:28:03.650184+00:00 app[web.1]: "//another.com:888", "//other.com"]

当我访问 myapp.herokuapp.com 时,JS 错误略有不同:

WebSocket connection to 'wss://myapp.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

随附的 heroku 日志:

2016-11-27T03:31:31.615163+00:00 heroku[router]: at=info method=GET path="/socket/websocket?token=undefined&vsn=1.0.0" host=myapp.herokuapp.com request_id=d43b57da-6aef-426b-ba69-7706c95ccc6d fwd="50.159.116.207" dyno=web.1 connect=0ms service=1ms status=403 bytes=195
2016-11-27T03:31:31.580305+00:00 app[web.1]: 03:31:31.579 [error] Could not check origin for Phoenix.Socket transport.
2016-11-27T03:31:31.580319+00:00 app[web.1]:
2016-11-27T03:31:31.580320+00:00 app[web.1]: This happens when you are attempting a socket connection to
2016-11-27T03:31:31.580321+00:00 app[web.1]: a different host than the one configured in your config/
2016-11-27T03:31:31.580322+00:00 app[web.1]: files. For example, in development the host is configured
2016-11-27T03:31:31.580322+00:00 app[web.1]: to "localhost" but you may be trying to access it from
2016-11-27T03:31:31.580323+00:00 app[web.1]: "127.0.0.1". To fix this issue, you may either:
2016-11-27T03:31:31.580324+00:00 app[web.1]:
2016-11-27T03:31:31.580325+00:00 app[web.1]: 1. update [url: [host: ...]] to your actual host in the
2016-11-27T03:31:31.580325+00:00 app[web.1]: config file for your current environment (recommended)
2016-11-27T03:31:31.580326+00:00 app[web.1]:
2016-11-27T03:31:31.580326+00:00 app[web.1]: 2. pass the :check_origin option when configuring your
2016-11-27T03:31:31.580327+00:00 app[web.1]: endpoint or when configuring the transport in your
2016-11-27T03:31:31.580327+00:00 app[web.1]: UserSocket module, explicitly outlining which origins
2016-11-27T03:31:31.580328+00:00 app[web.1]: are allowed:
2016-11-27T03:31:31.580329+00:00 app[web.1]:
2016-11-27T03:31:31.580329+00:00 app[web.1]: check_origin: ["https://example.com",
2016-11-27T03:31:31.580330+00:00 app[web.1]: "//another.com:888", "//other.com"]

当我将配置保留为 url: [scheme: "https", host: "myapp.herokuapp.com", port: 443] 访问 myapp.herokuapp.com 时我没有收到任何错误,但是当我访问 myapp.com 时,我在 JS 控制台中收到以下错误:

WebSocket connection to 'wss://myapp.com/socket/websocket?token=undefined&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

随附的 heroku 日志:

2016-11-27T03:34:55.671996+00:00 heroku[router]: at=info method=GET path="/socket/websocket?token=undefined&vsn=1.0.0" host=myapp.com request_id=ab595797-26ab-4952-ac77-2ab31bf18384 fwd="50.159.116.207,50.159.116.207,108.162.245.248" dyno=web.1 connect=0ms service=1ms status=403 bytes=214
2016-11-27T03:34:55.661334+00:00 app[web.1]: This happens when you are attempting a socket connection to
2016-11-27T03:34:55.661335+00:00 app[web.1]: a different host than the one configured in your config/
2016-11-27T03:34:55.661321+00:00 app[web.1]: 03:34:55.661 [error] Could not check origin for Phoenix.Socket transport.
2016-11-27T03:34:55.661333+00:00 app[web.1]:
2016-11-27T03:34:55.661347+00:00 app[web.1]: files. For example, in development the host is configured
2016-11-27T03:34:55.661348+00:00 app[web.1]: to "localhost" but you may be trying to access it from
2016-11-27T03:34:55.661349+00:00 app[web.1]: "127.0.0.1". To fix this issue, you may either:
2016-11-27T03:34:55.661350+00:00 app[web.1]: 1. update [url: [host: ...]] to your actual host in the
2016-11-27T03:34:55.661352+00:00 app[web.1]: endpoint or when configuring the transport in your
2016-11-27T03:34:55.661351+00:00 app[web.1]: config file for your current environment (recommended)
2016-11-27T03:34:55.661349+00:00 app[web.1]:
2016-11-27T03:34:55.661351+00:00 app[web.1]:
2016-11-27T03:34:55.661352+00:00 app[web.1]: 2. pass the :check_origin option when configuring your
2016-11-27T03:34:55.661354+00:00 app[web.1]: are allowed:
2016-11-27T03:34:55.661355+00:00 app[web.1]:
2016-11-27T03:34:55.661353+00:00 app[web.1]: UserSocket module, explicitly outlining which origins
2016-11-27T03:34:55.661355+00:00 app[web.1]: check_origin: ["https://example.com",
2016-11-27T03:34:55.661356+00:00 app[web.1]: "//another.com:888", "//other.com"]

我尝试将 check_origin 设置为 false,但它也不起作用。访问 myapp.com 时,heroku 日志不再显示错误。

heroku[router]: at=info method=GET path="/socket/websocket?token=undefined&vsn=1.0.0" host=myapp.com request_id=379b61c9-94be-4a1c-ac43-419e9554ac51 fwd="2601:602:9301:2b00:3c91:6660:9593:ad2c,2601:0602:9301:2b00:3c91:6660:9593:ad2c,108.162.245.122" dyno=web.1 connect=1ms service=1ms status=400 bytes=119

但我仍然在 JS 控制台中得到这个:

WebSocket connection to 'wss://myapp.com/socket/websocket?token=undefined&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 400

我的应用端点的完整配置

config :my_app, MyApp.Endpoint,
http: [port: {:system, "PORT"}],
url: [scheme: "https", host: "myapp.com", port: 443],
check_origin: false,
force_ssl: [rewrite_on: [:x_forwarded_proto]],
cache_static_manifest: "priv/static/manifest.json",
secret_key_base: System.get_env("SECRET_KEY_BASE")

编辑:为了值得,我将 cloudflare 用于我的 dns,并将 ssl 选项设置为 full。


编辑 2:如果我在我的配置中保留 check_origin: false 并且我更改我的 socket.js 文件,我可以加载 myapp.com:

let socket = new Socket("/socket", {params: {token: window.userToken}})

到:

let socket = new Socket("wss://myapp.herokuapp.com/socket", {params: {token: window.userToken}})

因为我不想让 check_origin 保持错误,所以我尝试将其更新为:

url: [scheme: "https", host: "myapp.com", port: 443],
check_origin: ["myapp.com", "myapp.herokuapp.com", "//myapp.com", "//myapp.herokuapp.com"],

使用这些新设置重新编译应用程序后,当我尝试访问 myapp.com 时,它在控制台中留下了这个 JS 错误:

WebSocket connection to 'wss://myapp.herokuapp.com/socket/websocket?token=undefined&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 503

最佳答案

第一个问题在于使用 heroku 和 cloudflare。 This article说它不能用自定义url来完成,你只能用默认的heroku url来使用它 https://support.cloudflare.com/hc/en-us/articles/205893698-Configure-Cloudflare-and-Heroku-over-HTTPS

也就是说,在没有使用 cloudflare 的情况下,我也遇到了问题。对我有用的是- 将 check_origin 参数设置为您正在访问的 url 的完整路径。对我来说,我也在使用 www,所以我的配置看起来像这样

url: [scheme: "https", host: "myapp.com", port: 443],
check_origin: [
"https://myapp.com",
"https://www.myapp.com"
],

(我的其他配置和你的一样)

关于javascript - 自定义域 WebSocket 握手错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40825702/

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