【问题标题】:How to configure Elixir, NGINX, Websockets on server如何在服务器上配置 Elixir、NGINX、Websockets
【发布时间】:2018-05-30 02:12:53
【问题描述】:

我正在设置一个带有 Phoenix 应用程序的服务器,该应用程序将使用 websockets。本地 websocket 工作,但我在登台服务器上设置它时遇到问题。有人可以帮我在我的服务器上设置 websocket。我的 nginx 配置如下:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream my_app {
  server 0.0.0.0:6443;
}

server {
  listen 80;
  listen 443;
  server_name example.com;
  ssl on;
  ssl_certificate /path/to/wildcard.crt;
  ssl_certificate_key /path/to/wildcard.key;
  ssl_prefer_server_ciphers on;

  if ($request_uri ~ "^[^?]*//") {
    rewrite "(.*)" $scheme://$host$1 permanent;
  }
  if ( $scheme = http ){
    rewrite ^ https://$host$request_uri permanent;
  }

  location / {
    allow all;

    proxy_http_version 1.1;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Cluster-Client-Ip $remote_addr;

    # WebSockets
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass https://my_app;
  }
}

我的凤凰配置是:

use Mix.Config

config :my_app_core, MyAppCoreWeb.Endpoint,
  load_from_system_env: false,
  url: [host: "https://example.com", port: {:system, "PORT"}],
  http: [port: {:system, "PORT"}],
  https: [otp_app: :my_app_core, port: 6443, keyfile: "/path/to/wildcard.key", certfile: "/path/to/wildcard.crt"],
  server: true,
  root: ".",
  watchers: [],
  version: Mix.Project.config[:version],
  check_origin: false

config :logger, :console, format: "[$level] $message\n"

config :phoenix, :stacktrace_depth, 2

config :phoenix, :serve_endpoints, true

import_config "workspace.secret.exs"

我正在测试与http://phxsockets.io/ 的连接,我得到了

failed: Error during WebSocket handshake: Unexpected response code: 400

有人可以帮忙吗?

【问题讨论】:

  • 我希望proxy_pass https://my_app; 必须引用本地主机的某个地方,并且当您在 nginx 级别处理 ssl 时,我看不到您通过 https 代理您的应用程序的意义。改用常规的http
  • 我尝试从 proxy_pass https://my_app; 更改为 proxy_pass http://my_app; 并没有改变任何东西。在phoenix 配置文件中,我设置了httphttps,它应该足以与websockets 一起使用(wswss)。至于本地主机,我通过将upstream my_app { server 0.0.0.0:6443;} 更改为upstream my_app { server 0.0.0.0:4020;}(我的http 端口)或同时使用upstream my_app { server 0.0.0.0:6443; server 0.0.0.0:4020} 来测试它。这也导致了同样的问题。
  • 我认为您还需要在proxy_pass上指定websocket端口。否则它将在端口 80 或端口 443 上侦听,具体取决于。你能试试proxy_pass http://my_app:4020
  • 还是同样的问题 :( 我已经像你说的那样添加了端口,我已经尝试过proxy_pass http://0.0.0.0:4020,这也没有用。我认为你设置它没关系proxy_pass 或者你定义upstream my_app { server 0.0.0.0:4020 }
  • 好的,这个配置正在运行。问题在于为此应用更新了错误的 nginx 配置:P

标签: ssl nginx websocket elixir phoenix-framework


【解决方案1】:

上述配置有效。我这边的问题是使用具有旧设置的错误 nginx 配置文件。

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 2018-01-24
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多