jjzjj

javascript - 自定义域 WebSocket 握手错误

coder 2025-01-20 原文

我的服务器上不断出现此错误:[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/

有关javascript - 自定义域 WebSocket 握手错误的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  3. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  9. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  10. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

随机推荐