Reputation: 73
I'm setting up a server with a Phoenix app that will use websockets. Locally websocket work but I have problems with setting it up on my staging server. Can someone help me with setting up websockets on my server. I have nginx configured like this:
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;
}
}
and my phoenix config is:
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"
I'm testing the connection with http://phxsockets.io/ and I get
failed: Error during WebSocket handshake: Unexpected response code: 400
Can someone help with this?
Upvotes: 4
Views: 1381
Reputation: 73
The above configuration works. The issue on my side was with using the wrong nginx config file that had the old settings.
Upvotes: 2