如何在Nginx之后的Phoenix中设置Websockets? [英] How to set up Websockets with Phoenix behind Nginx?

查看:88
本文介绍了如何在Nginx之后的Phoenix中设置Websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置网络套接字,以使其通过Nginx到达Phoenix应用程序,但始终出现403错误.任何人都可以建议正确的配置以使其在生产中正常工作-开发环境很好.

I'm trying to set up web sockets to go through Nginx to a Phoenix app but keep getting a 403 error. Can anyone advise the correct configuration to make this work in production - development env is fine.

我的Nginx conf:

My Nginx conf:

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

我的prod.exs conf:

My prod.exs conf:

use Mix.Config

config :logger, level: :info
config :phoenix, :serve_endpoints, true

config :app, App.Endpoint,
  http: [port: 4000],
  url: [host: "127.0.0.1", port: 4000],
  root: '.',
  cache_static_manifest: "priv/static/manifest.json",
  server: true

config :app, App.Repo,
  username: System.get_env("MONGO_USERNAME"),
  password: System.get_env("MONGO_PASSWORD"),
  database: "template",
  hostname: "localhost",
  pool_size: 10

如有必要,我可以根据要求提供任何其他信息.

I can provide any additional info as requested if necessary.

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

The app can be reached fine over the domain name, the last and only remaining problem is getting web sockets to work.

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

Many thanks to anyone who can point me in the right direction.

推荐答案

i遵循了phoenix网站上的指南. Exrm发布-Phoenix

i followed the guide from phoenix site. Exrm Releases - Phoenix

您的nginx.config缺少此信息:

Your nginx.config is missing this:

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

将发行版发送到服务器后,在本地计算机上生成发行版时,我也遇到了一些错误.

I also had some error when generate a release in my local machine after ship the release to my server.

因此,我建议您应在服务器环境中生成发行版.

So i suggest you should generate your release inside your server environment.

浏览器控制台中的错误:

Error in browser console:

ws://phoenix.hollyer.me.uk/socket/websocket?token=&vsn=1.0.0' failed: Error during WebSocket handshake: Unexpected response code: 403

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

This is probably this error.You should try to start your console inside your server:

[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"]

因此您可以像这样重新配置主机:

so you may re-configure your host like:

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

将:check_origin选项传递给端点配置或UserSocket模块:

either pass :check_origin option to your endpoint config or UserSocket module:

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

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

And then try to deploy again.Hope that help you!

这篇关于如何在Nginx之后的Phoenix中设置Websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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