Greetings,
I had been learning Elixir/Phoenix a couple of years ago, but had to stop. I am getting back into it again and lots of it seem familiar. Unfortunately, some of my test examples got lost somehow and I need help getting the initial connection working on HTTPS. My website is not hosted on Phoenix. Instead, the web server is hosted on Nginx and I intend to use Phoenix as an API server, connecting with sockets - No HTML will be hosted on the Phoenix side. The website is uses HTTPS only. I was able to get WS partially working (console complains about mixed HTTP/HTTPS content, but I am seeing the socket connection attempt on Phoenix, so I know something is making it through). However, I cannot get a WSS connection to work.
Here is my Nginx configuration:
upstream exchat {
server 127.0.0.1:4000 max_fails=5 fail_timeout=60s;
}
server {
server_name ex.mysite.com;
listen [::]:443 ssl; # managed by Certbot
listen 443 ssl; # managed by Certbot
location / {
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 https://exchat;
}
ssl_certificate /etc/letsencrypt/live/ex.mysite.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/ex.mysite.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
server_name ex.mysite.com;
listen 80;
return 301 https://ex.mysite.com$request_uri;
}
The subdomain “ex.mysite.com” is intended only for the Phoenix backend for API and websockets.
Endpoint.ex has something like this:
socket "/socket", ExchatWeb.UserSocket,
websocket: true,
longpoll: false
A socket connection attempt on my website (client-side javascript), like the following seems to show up on the Phoenix end:
const exampleSocket = new WebSocket("ws://ex.mysite.com/socket/websocket?token=undefined&vsn=2.0.0");
but not:
const exampleSocket = new WebSocket("wss://ex.mysite.com/socket/websocket?token=undefined&vsn=2.0.0");
I tried messing around with the config/dev.exs as well, but I get access denied / permission errors due to the LetsEncrypt certificate being owned by root. Not sure if any of this is even required for an API-only phoenix app with websockets:
config :exchat, ExchatWeb.Endpoint,
force_ssl: [hsts: true],
https: [
port: 4000,
cipher_suite: :strong,
keyfile: "/etc/letsencrypt/live/ex.mysite.com/privkey.pem",
certfile: "/etc/letsencrypt/live/ex.mysite.com/fullchain.pem"
],
debug_errors: true,
code_reloader: true,
check_origin: ["//www.mysite.com", "//ex.mysite"],
watchers: [
node: [
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
"--watch-stdin",
cd: Path.expand("../assets", __DIR__)
]
]
Do I even need LetsEncrypt on the Phoenix end for wss? Let me know what I should do here or if some of my configs should be changed to something more appropriate for my API setting.
Thanks