如何在服务器上配置Elixir,NGINX,Websockets

时间:2017-12-15 20:41:01

标签: ssl nginx websocket elixir phoenix-framework

我正在使用将使用websockets的Phoenix应用程序设置服务器。本地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;
  }
}

我的凤凰配置是:

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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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