如何在服务器上配置Elixir,NGINX,Websockets [英] How to configure Elixir, NGINX, Websockets on server

查看:169
本文介绍了如何在服务器上配置Elixir,NGINX,Websockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Phoenix应用程序设置服务器,该应用程序将使用websockets.在本地进行websocket工作,但是在暂存服务器上进行设置时遇到问题.有人可以帮助我在服务器上设置websockets吗?我已经像这样配置了nginx:

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

我的凤凰配置是:

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/测试连接,我得到

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

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

有人可以帮忙吗?

推荐答案

上面的配置有效.我这方面的问题是使用了具有旧设置的错误的nginx配置文件.

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

这篇关于如何在服务器上配置Elixir,NGINX,Websockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆