This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>How to use NGINX as a reverse-proxy server for a Node.js application using socket.io | tait.tech</title><linkrel="stylesheet"href="/assets/css/style.css"><metaname="viewport"content="width=device-width, initial-scale=1.0"><metaname="author"content="Tait Hoyem"><metaname="keywords"content=""><metaname="description"content=""></head><body><divid="wrapper"><header><nav><inputtype="checkbox"id="menu"><labelfor="menu">☰</label><divclass="menu-content"><ahref="/"class="nav-link">Home</a><ahref="/blog/"class="nav-link">Blog</a><ahref="https://github.com/TTWNO/"class="nav-link"target="_blank"rel="noopener noreferrer">Github</a></div></nav></header><main><article><header><h1class="post-title">How to use NGINX as a reverse-proxy server for a Node.js application using socket.io</h1><timedatetime="20-05-01"class="post-date">Friday, May 01 2020</time></header><hr><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="language-plaintext 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="language-plaintext 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="language-plaintext highlighter-rouge">projects.tait.tech.conf</code> file:</p><preclass="terminal">
</pre><h3id="explaination">Explaination:</h3><p>For this application, I needed the <codeclass="language-plaintext 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="language-plaintext highlighter-rouge">/socket.io</code> to conenct to my running <codeclass="language-plaintext highlighter-rouge">npm</code> instance. When I tried to route all the traffic through the <codeclass="language-plaintext highlighter-rouge">/trrpg</code> location directive I had no luck whatsoever; <codeclass="language-plaintext 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>For now, the project is alive!!!</p><p>Happy hacking!</p><p><em>P.S. I forgot to mention I also symbolically linked the <codeclass="language-plaintext 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><preclass="terminal">
</pre><p><em>Happy hacking 2.0!</em></p></article></main><hr><footer> This page is mirrored on <ahref="https://beta.tait.tech/2020/05/01/nginx-socket-io-projects.html">beta.tait.tech</a>. </footer></div></body></html>