<h1>How to use NGINX as a reverse-proxy server for a Node.js application using socket.io</h1>
<h4class="post-date line-under">Friday, May 01 2020</h4>
<divclass="article">
<p>Despite the long name of the article, I have a feeling this may apply to more people than I might think.
If you have a Node.js application which needs socket.io connections that you want to pass throgh nginx’s <codeclass="highlighter-rouge">reverse_proxy</code> directive then this is the article for you!</p>
<p>You <em>must</em> seperate the socket.io sockets and the static resources.</p>
<ul>
<li>The socket connections can be routed through the default <codeclass="highlighter-rouge">$host/socket.io</code> if you want to ease modifications to the source code.</li>
<li>The connections to your main npm Node.js application can be routed through the relevant directory.</li>
</ul>
<p>Here is the relevant part of my <codeclass="highlighter-rouge">projects.tait.tech.conf</code> file:</p>
I needed the <codeclass="highlighter-rouge">/ttrpg</code> directory to connect to my main Node.js instance. This was going to be the root of a ttrpg project.
It was to have static files served form my Node.js application.</p>
<p>I also needed <codeclass="highlighter-rouge">/socket.io</code> to conenct to my running <codeclass="highlighter-rouge">npm</code> instance.
When I tried to route all the traffic through the <codeclass="highlighter-rouge">/trrpg</code> location directive
I had no luck whatsoever;
<codeclass="highlighter-rouge">$host/ttrpg/socket.io/*</code> calls <em>always</em> failed with a 404.</p>
<p>Having two seperate blocks forwarding in different ways seems to fix this.
I am not knowledgable enough to understand how.</p>
<p><em>P.S. I forgot to mention I also symbolically linked the <codeclass="highlighter-rouge">socket.io.js</code> file (that node is supposed to serve automatically) to the static client dir.
For some reson the node instance would not serve this file without that.</em></p>