2017-12-15 21 views
2

웹 소켓을 사용할 Phoenix 앱이있는 서버를 설정하고 있습니다. 로컬로 websocket 작업을하지만 내 준비 서버에 설정하는 데 문제가 있습니다. 누군가 내 서버에서 웹 소켓을 설정하는 데 도움을 줄 수 있습니까? 나는 nginx를 다음과 같이 구성되어있다 :서버에서 Elixir, NGINX, Websockets을 구성하는 방법

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/와의 연결을 테스트 그리고 난 다른 사람이 도와 드릴

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

를 얻을 수 ?

+0

'proxy_pass https : // my_app;'는 localhost를 참조해야하며, nginx 레벨에서 ssl을 처리 할 때 https를 통해 앱을 프록시한다는 사실을 알 수 없습니다. 대신에'http'를 정기적으로 사용해보십시오. –

+0

'proxy_pass https : // my_app;'에서'proxy_pass http : // my_app;'로 변경하려고 시도했지만 아무 것도 변경하지 않았습니다. phoenix 설정 파일에서 나는'http'와'https'를 설정했고, 웹 소켓 ('ws'와'wss')으로도 충분할 것입니다. localhost에 관해서는,'upstream my_app {server 0.0.0.0:6443;}'을'upstream my_app {server 0.0.0.0:4020;}'(http 용 my port)로 변경하거나'upstream my_app {서버 0.0.0.0:6443; 서버 0.0.0.0:4020}'. 이것은 또한 동일한 문제가 발생했습니다. – kabebop

+0

proxy_pass에서 websocket 포트를 지정해야한다고 생각합니다. 그렇지 않으면 포트 80 또는 포트 443에서 수신 대기합니다. 'proxy_pass http : // my_app : 4020' –

답변

0

위의 구성이 작동합니다. 내 편이던 문제는 이전 설정이있는 잘못된 nginx 설정 파일을 사용하는 것과 관련이 있습니다.