如何与Nginx背后的Phoenix建立WebSocket?

如何与Nginx背后的Phoenix建立WebSocket?,nginx,websocket,elixir,phoenix-framework,phoenix-channels,Nginx,Websocket,Elixir,Phoenix Framework,Phoenix Channels,我正在尝试设置web套接字,以便通过Nginx连接到Phoenix应用程序,但不断出现403错误。任何人都可以建议正确的配置,使这项工作在生产-开发环境是好的 我的Nginx配置: upstream phoenix { server 127.0.0.1:4000 max_fails=5 fail_timeout=60s; } server { server_name <app-domain>; listen 80; location / { allow a

我正在尝试设置web套接字,以便通过Nginx连接到Phoenix应用程序,但不断出现403错误。任何人都可以建议正确的配置,使这项工作在生产-开发环境是好的

我的Nginx配置:

upstream phoenix {
  server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
}

server {
  server_name <app-domain>;
  listen 80;

  location / {
    allow all;

    # Proxy Headers
    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;

    # The Important Websocket Bits!
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";

    proxy_pass http://phoenix;
  }
}
如有必要,我可以按要求提供任何其他信息

该应用程序可以通过域名很好地访问,最后也是唯一剩下的问题是如何让web套接字正常工作


非常感谢任何能为我指明正确方向的人。

我遵循凤凰城网站的指南。

您的nginx.config缺少以下内容:

# The following map statement is required
# if you plan to support channels. See https://www.nginx.com/blog/websocket-nginx/
map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
在将发行版发送到服务器后,在本地计算机中生成发行版时,我也遇到了一些错误

因此,我建议您应该在服务器环境中生成版本

编辑:

浏览器控制台中出现错误:

ws://phoenix.hollyer.me.uk/socket/websocket?token=&vsn=1.0.0'失败:websocket握手期间出错:意外响应代码:403

这可能是此错误。您应该尝试在服务器内启动控制台:

[error] Could not check origin for Phoenix.Socket transport.

This happens when you are attempting a socket connection to
a different host than the one configured in your config/
files. For example, in development the host is configured
to "localhost" but you may be trying to access it from
"127.0.0.1". To fix this issue, you may either:

  1. update [url: [host: ...]] to your actual host in the
     config file for your current environment (recommended)

  2. pass the :check_origin option when configuring your
     endpoint or when configuring the transport in your
     UserSocket module, explicitly outlining which origins
     are allowed:

        check_origin: ["https://example.com",
                        "//another.com:888", "//other.com"]
因此,您可以重新配置主机,如:

[url: [host: "http://www.phoenix.hollyer.me.uk"]]
将:check_origin选项传递到端点配置或UserSocket模块:

check_origin: ["http://www.phoenix.hollyer.me.uk"]

然后再次尝试部署。希望对您有所帮助

是的,我错过了地图说明。因此,我将它添加到该域的nginx conf中,并重新启动了nginx,但仍然得到相同的错误。我有一个与我的服务器配置相匹配的虚拟机,并在此基础上构建了exrm版本,如果存在构建问题,我想我将无法访问它,但事实并非如此。确切的问题可以在这里[link](phoenix.hollyer.me.uk)看到,如果你转到联系人页面,检查浏览器控制台,你会看到错误。这修复了它,谢谢,我已经接受了你的正确答案。对我来说,我将check_origin设置为false,因为我也将从移动应用程序连接到套接字。很高兴能帮助你!
check_origin: ["http://www.phoenix.hollyer.me.uk"]