user2725682
user2725682

Reputation: 139

phoenix websocket behind nginx connection refused

Cannot make phoenix channel work behind nginx with also apache reverse proxy before nginx.

the website is ok but the socket connection cannot be established. socket is ok in dev mode where everything is ok. there is no authorization setup in phoenix

defp authorized?(_payload) do
    true
 end

the error message is

2017/04/29 09:40:50 [error] 10451#10451: failed (111: Connection refused) 
while connecting to upsteam, client: 192.168.0.10, 
server: somehost.com, 
request: "GET /socket/websocket?token=undefined&vsn=1.0.0 HTTP/1.1",
upstream:"http://phoenix3/socket/websocket?token=undefined&vsn=1.0.0", 
host: "somehost.com"

following the guides on this topic here is the setup I came up with

in user_socket.ex

transport :websocket, Phoenix.Transports.WebSocket, check_origin: ["//somehost.com"]

in endpoint

config :myapp, MyApp.Endpoint,                                               
  http: [port: {:system, "PORT"}],                                                   
  url: [host: "somehost.com", port: 80],                               
  cache_static_manifest: "priv/static/manifest.json"  

in nginx

nb X-Real-IP or X-Cluster-Client-IP both fail

upstream phoenix3 {                                                                  
   server localhost:4020 max_fails=5 fail_timeout=60s;                           
}                                                                                    
map $http_upgrade $connection_upgrade {                                             
 default upgrade;                                                            
 '' close;                                                                   
}                                                                                    
server {                                                                             
  listen 80;                                                                   
  listen [::]:80;                                                              
  server_name somehost.com;                                      

   access_log /home/user1/appdir/log/access.log;                             
   error_log  /home/user1/appdir/log/error.log;                              
   location / {                                                                 

   # Proxy Headers                                                              
   proxy_set_header X-Real-Ip $remote_addr;                                    
   # proxy_set_header X-Cluster-Client-Ip $remote_addr;                         
   proxy_http_version 1.1;                                                     
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                
   proxy_set_header Host $http_host;                                           


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

 proxy_pass http://phoenix3;                                                 
 proxy_redirect off;                                                         

 }                                                                            

in apache (gets the real world connection)

<VirtualHost *:80>      
  ServerName somehost.com
   ProxyPreserveHost On                                                             
   ProxyRequests off                                                                
   ProxyPass / http://192.168.0.26/                                                 
   ProxyPassReverse / http://192.168.0.26/
</VirtualHost>                                           

Upvotes: 1

Views: 1069

Answers (1)

user2725682
user2725682

Reputation: 139

the problem was the apache configuration. the nginx configuration is ok and the phoenix configuration is also ok

in apache virtual host, needed to add

ProxyPass /socket/ ws://192.168.0.26/socket/                                     
ProxyPassReverse /socket/ ws://192.168.0.26/socket/     

before the first block, and then add an additionnal apache module

a2enmod proxy_wstunnel
service apache2 restart  

Upvotes: 1

Related Questions