peppy

peppy

Websockets Not Working: Phoenix Scripts On Sub-Domain, Loaded Through Main Domain / Apache

My Apache setup is like this:

##  MAIN WEBSITE  ##
<VirtualHost *:443>
  ServerAdmin admin@mywebsite.com
  ServerName www.mywebsite.com
  ServerAlias mywebsite.com
  DocumentRoot /home/username/mywebsite.com
  ProxyRequests Off
  
  ## SOCKET SETUP FOR MY EXISTING NODE.JS APP THAT RUNS ON PORT 3000
  #####################################################################
  RewriteCond %{QUERY_STRING} transport=polling       [NC]
  RewriteRule /(.*)           http://127.0.0.1:3000/$1 [P]
  RewriteCond %{HTTP:Upgrade} websocket               [NC]
  RewriteRule /(.*)           ws://127.0.0.1:3000/$1  [P]
  <Location /cnode2/>
    ProxyPass http://127.0.0.1:3000/
    ProxyPassReverse http://127.0.0.1:3000/
  </Location>
</VirtualHost>



## SUB-DOMAIN DEDICATED SOLELY FOR PHOENIX PROJECT - PORT 4000
<VirtualHost *:80>
    ServerAdmin admin@mywebsite.com
    ServerName ex.mywebsite.com
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =ex.mywebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
    ServerAdmin admin@mywebsite.com
    ServerName ex.mywebsite.com
    DocumentRoot /home/username/mywebsite.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    RewriteEngine on
    RewriteCond %{HTTP:Upgrade} websocket [NC]
    RewriteCond %{HTTP:Connection} upgrade [NC]
    RewriteRule ^/?(.*) "ws://127.0.0.1:4000/$1" [P,L]

    ProxyRequests Off
    ProxyPass        / http://127.0.0.1:4000/
    ProxyPassReverse / http://127.0.0.1:4000/

    ProxyPass        /socket/ ws://127.0.0.1:4000/socket/
    ProxyPassReverse /socket/ ws://127.0.0.1:4000/socket/
</VirtualHost>

Here is the HTML Source Code for my simple test page at mywebsite.com


<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>Exchat · Phoenix Framework</title>
    <link rel="stylesheet" href="https://ex.mywebsite.com/css/app.css"/>
    <script defer type="text/javascript" src="https://ex.mywebsite.com/js/app.js"></script>
  </head>
  <body>
    <header>
      <section class="container">
        <nav role="navigation">
          <ul>
            <li><a href="https://hexdocs.pm/phoenix/overview.html">Get Started</a></li>

              <li><a href="https://ex.mywebsite.com/dashboard">LiveDashboard</a></li>

          </ul>
        </nav>
        <a href="https://phoenixframework.org/" class="phx-logo">
          <img src="https://ex.mywebsite.com/images/phoenix.png" alt="Phoenix Framework Logo"/>
        </a>
      </section>
    </header>
    <main role="main" class="container">
      <p class="alert alert-info" role="alert"></p>
      <p class="alert alert-danger" role="alert"></p>
<section class="phx-hero">
  <h1>Welcome to Phoenix!</h1>
  <p>Peace of mind from prototype to production</p>
</section>

<section class="row">
  <article class="column">
    <h2>Resources</h2>
    <ul>
      <li>
        <a href="https://hexdocs.pm/phoenix/overview.html">Guides &amp; Docs</a>
      </li>
      <li>
        <a href="https://github.com/phoenixframework/phoenix">Source</a>
      </li>
      <li>
        <a href="https://github.com/phoenixframework/phoenix/blob/v1.5/CHANGELOG.md">v1.5 Changelog</a>
      </li>
    </ul>
  </article>
  <article class="column">
    <h2>Help</h2>
    <ul>
      <li>
        <a href="https://forum.elixirforum.com/c/phoenix-forum">Forum</a>
      </li>
      <li>
        <a href="https://webchat.freenode.net/?channels=elixir-lang">#elixir-lang on Freenode IRC</a>
      </li>
      <li>
        <a href="https://twitter.com/elixirphoenix">Twitter @elixirphoenix</a>
      </li>
      <li>
        <a href="https://elixir-slackin.herokuapp.com/">Elixir on Slack</a>
      </li>
    </ul>
  </article>
</section>

    </main>
  <iframe hidden height="0" width="0" src="https://ex.mywebsite.com/phoenix/live_reload/frame"></iframe></body>
</html>

Basically, on a page on my main domain name “mywebsite.com”, I’m loading up the css, js and images externally from the phoenix project set up on my subdomain “ex.mywebsite.com”. Everything appears to load up fine, but there are still issues with the web sockets not working.

In the developer console, I am getting errors like:

WebSocket connection to 'wss://www.mywebsite.com/socket/websocket?token=undefined&vsn=2.0.0' failed: Error during WebSocket handshake: Invalid status line

In the network “waterfall”, I am seeing these socket requests/responses:

(this one shows up only once for the proper sub-domain "ex.mywebsite.com", the initiator is "frame", which is probably the iframe url in the HTML source code.)

GENERAL:
Request URL: wss://ex.mywebsite.com/phoenix/live_reload/socket/websocket?vsn=2.0.0
Request Method: GET
Status Code: 101 Switching Protocols

RESPONSE:
cache-control: max-age=0, private, must-revalidate
connection: Upgrade
date: Tue, 24 Aug 2021 04:39:43 GMT
sec-websocket-accept: ikc2MBZrO94E7m7sVm0KO6fJEW4=
server: Cowboy
upgrade: websocket

REQUEST:
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: Upgrade
Host: ex.mywebsite.com
Origin: https://ex.mywebsite.com
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: n18b2ofXGa0LGw5y08m+IA==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36 Edg/92.0.902.78

and:

(this is a repeating error, and is NOT using the "ex.mywebsite.com" subdomain either. The initiator is "phoenix.js?31bb:1", which I'm assuming is the main app.js file.)

GENERAL:
Request URL: wss://www.mywebsite.com/socket/websocket?token=undefined&vsn=2.0.0

REQUEST:
Provisional headers are shown
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: no-cache
Connection: Upgrade
Host: www.mywebsite.com
Origin: https://www.mywebsite.com
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: ikzQ6SHJAzRMQWczj+mLug==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36 Edg/92.0.902.78

Reminder: in the apache config above for the main site “mywebsite.com”, I have a web socket setup for an existing node app on port 3000. I’m not sure if this makes a difference or not, if my phoenix app and phoenix socket script is loaded on the “ex.mywebsite.com” subdomain on port 4000. Hopefully it won’t matter. As a test, I commented out the node config in apache anyways, but made no difference at all with my web socket errors, still getting them. There is something else causing the issue.

An explanation and fix would be greatly appreciated. It seems like I’m so close to finally getting my website “desktop” all prepared so I can actually start developing actual apps!

Marked As Solved

derek-zhou

derek-zhou

You app.js probably has something like:

let socket = new Socket("/socket", {params: {token: window.userToken}})

So the client side is connecting to “/socket” which is the related to the host name from the client side of view. And that resolved to wss://www.mywebsite.com/socket/websocket?token=undefined&vsn=2.0.0

Either you proxy this to phoenix, or you change your exposed domain name.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement