ssl - 如何在服务器上配置 Elixir、NGINX、Websockets

标签 ssl nginx websocket elixir phoenix-framework

我正在设置一个带有 Phoenix 应用程序的服务器,该应用程序将使用 websockets。本地 websocket 工作,但我在我的登台服务器上设置它时遇到问题。有人可以帮我在我的服务器上设置 websockets 吗?我的 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;
  }
}

我的 Phoenix 配置是:

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

有人可以帮忙吗?

最佳答案

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

关于ssl - 如何在服务器上配置 Elixir、NGINX、Websockets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47839601/

相关文章:

java - 如何使用websocket获取消息?

java - Spring Boot应用SSL属性配置: getKeyStorePassword before setKeyStorePassword

ssl - 通过显式 TLS/SSL 加密的 FTP 下载(所有)文件

nginx - 使用 nginx 将域添加到 Set-Cookie

html - Nginx try_files 重定向不服务于 css/图像

javascript - 如何检测用户的连接在nodejs websocket(ws)中丢失

java - Websocket 服务器初始化与另一个 websocket 服务器的连接

ssl - 无法检查 TFS 2015 SSL 证书是否已吊销

php - 如何使用 SSL 强制 Wordpress 管理/登录页面?

php - Docker - 安装 php-memcached 扩展不工作?