How to configure Elixir, NGINX, Websockets on server

我与影子孤独终老i 提交于 2019-12-10 10:23:30

问题


I'm setting up a server with a Phoenix app that will use websockets. Locally websocket work but I have problems with setting it up on my staging server. Can someone help me with setting up websockets on my server. I have nginx configured like this:

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;
  }
}

and my phoenix config is:

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"

I'm testing the connection with http://phxsockets.io/ and I get

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

Can someone help with this?


回答1:


The above configuration works. The issue on my side was with using the wrong nginx config file that had the old settings.



来源:https://stackoverflow.com/questions/47839601/how-to-configure-elixir-nginx-websockets-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!