2016-07-06 44 views
1

我試圖設置網絡套接字通過Nginx的鳳凰應用程序,但不斷得到403錯誤。任何人都可以建議正確的配置,使這項工作在生產 - 開發env是好的。如何在Nginx背後使用Phoenix設置Websockets?

我的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:

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 

我可以提供任何額外的信息,如果有必要的要求。

該應用程序可以很好地到達域名,最後和唯一剩下的問題是讓網絡套接字工作。

非常感謝任何能指引我走向正確方向的人。

回答

2

我跟着從鳳凰網站的指導。 Exrm Releases - Phoenix

你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' failed: Error during WebSocket handshake: Unexpected response code: 403

這大概是這error.You應該嘗試啓動服務器內部控制檯:

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

爲pass:check_origin選項,您的端點配置或UserSocket模塊:

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

,然後嘗試部署again.Hope,可以幫助您!

+0

是的,我錯過了地圖聲明。所以我已經將它添加到該域的nginx conf中,並重新啓動了nginx,但仍然收到相同的錯誤。我有一個與我的服務器配置相匹配的vagrant虛擬機,並在此基礎上構建exrm版本,如果出現構建問題,我將無法訪問它,但事實並非如此。確切的問題可以在這裏看到[link](phoenix.hollyer.me.uk),如果你去到聯繫頁面,並檢查瀏覽器控制檯,你會看到錯誤。 –

+0

正確的md鏈接:[link](http://phoenix.hollyer.me.uk/contact) –

+0

我編輯了答案。 – TheAnh