Having difficulty configuring unique paths for multiple APIs using Socket.IO, ExpressJS, and Nginx

I am currently working on setting up multiple APIs on a single VPS and serving them through Nginx. My goal is to have all of them organized in separate sub-locations, as shown in the example below:


For Express remote paths:

[myhost].com/apps/app1/api
[myhost].com/apps/app2/api

And for Express local paths:

localhost:[app1 port]
localhost:[app2 port]

When it comes to Socket.IO, the remote paths are structured like this:

[myhost].com/apps/app1/api/socket.io
[myhost].com/apps/app2/api/socket.io

While the local paths look like:

localhost:[app1 port]/socket.io
localhost:[app2 port]/socket.io

Express is functioning properly, and I can access it locally using the following command:

curl -ivL http://localhost:[app1 port]

However, due to the 'path' property in Socket.IO, Socket.IO responses are only accessible at:

curl -ivL http://localhost:[app1 port]/apps/app1/api/socket.io/?EIO=4

Without the 'path' specified, remote access to Socket.IO becomes unavailable.

const ioServer = new socketIo.Server(httpServer, {
  ...
  path: "/apps/app1/api/socket.io", <---- this 'path' property
});

In terms of Nginx configuration:

  ......
  location /apps {
    index _;
    autoindex on;

    location /apps/app1/api/ {
      proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header    Host             $http_host;
      proxy_pass          http://localhost:[app1 port];
      proxy_http_version  1.1;
      proxy_set_header    Upgrade          $http_upgrade;
      proxy_set_header    Connection       "upgrade";
    }
    location /apps/app1 {
      index index.html;
    }
    ....

I am seeking a solution to remove the /apps/app1/api part from the localhost Socket.IO setup without compromising the ability for remote access. Any suggestions would be greatly appreciated. Thank you.

Answer №1

It's really quite simple: just make sure to include a full URI with a trailing slash in the proxy_pass parameter, otherwise it will not be taken into account.

When proxy_pass is used without a URI, the request URI is forwarded to the server as-is from the client, or the full normalized request URI is sent if the URI has been altered:

location /some/path/ {
  proxy_pass http://127.0.0.1;
}

referenced from Nginx documentation

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The for loop does not pause until the ajax response is received

When I enter the for loop, an ajax call is made. However, the for loop does not wait for the ajax call to receive a response before incrementing the value, causing the response to update to the wrong div element. For example: Let's say we have ' ...

Run the function once the page has completed downloading and bootstrapping

After experimenting with $evalAsync and $viewContentLoaded, I've found that they only trigger after Angular has completed populating the template. My goal is to determine, from within a directive: Is the template fully replaced by Angular? Have all ...

Sending search queries from a frontend built with React.js to a backend in Express.js: What is the best approach?

I have been attempting to develop a basic search bar using react.js that will communicate with my express.js backend in order to retrieve the accurate data from the database and display it on the front-end. However, I am struggling to grasp how to transmit ...

How do you transform data stored in map to string format?

The main objective is to take a large txt file and replace all words according to the information in a csv file. This process will generate a new txt file as well as a new csv file that shows the frequency of each word. I'm currently struggling with ...

The MongoClient object does not possess the 'open' method

I recently started working on a project using Node.js, Express.js, and MongoDB. I've encountered some issues while trying to set up the database configuration. Below is a snippet of code from my index.js file: var http = require('http'), ...

my combination of express and socket.io run on separate ports

My Express 4 application always starts on port 3000, ignoring socketIO in this port. When I explicitly listen to a different port after binding my Express app with Socket.IO, Express + Socket start on the specified port (e.g., 3030) and I can run my app on ...

Retrieve the value of a field from a Formik form integrated with Material UI

I've been working on a way to disable a checkbox group depending on the selected value of a radio group. I took inspiration from the technique outlined in the final section of the Formik tutorial. Using React context has definitely helped clean up the ...

Calculate the sum of floating point or decimal numbers from a textarea using JavaScript

I'm trying to work with a text area that contains decimal/float numbers. However, the code I found online seems to be ignoring commas and periods when summing up the values. Is there a way to handle decimal/float numbers correctly in this scenario? ...

Creating operations in Angular using the Model View Controller (MVC)

What is the procedure for performing an Add operation in MVC using Angular? var addProductModule = angular.module("addProductModule", []); addProductModule.factory("addProductService", ['$http', function ($http) { return { function savePro ...

What is the best way to modify an image in a column when I hover over a table row that was dynamically inserted using a button function?

Is there a way to dynamically change an image in a column when hovering over a table row that was added through a button function? Here is the current code that I am using, but it does not seem to work as intended when I hover over the row. This function ...

What is the process for executing a single flight in Express JS?

When using an API to retrieve data that takes 10 seconds to respond, how can we ensure that only the first request is processed and then the results are distributed to all subsequent requests with the same parameter? In Golang, this can be easily achieve ...

Building a dynamic cities dropdown menu in ReactJs based on the chosen country

I have an array called countryList that looks like this: export const countryList = [ {name: 'Singapore', code: 'SG', cities:[ "Ang Mo Kio New Town", "Ayer Raja New Town", ...

Using PHP script to retrieve MySQL table values and dynamically update a live graph in a Javascript function

I've been researching similar issues for quite some time now, but to no avail. Currently, I'm utilizing Highcharts to update a graph every 3 seconds with the latest entry from a specific MySQL table. I am referring to the example Javascript code ...

Navigating with React Router v4 using NavLink to highlight the active route

Currently, I am in the process of transitioning my project from utilizing v3 of react-router to v4 now referred to as react-router-dom. The issue stems from having a MenuBar component that is completely independent of the routing logic. In the previous ver ...

Implementing Promises in AngularJS Controller: A Comprehensive Guide

I'm currently working on implementing a basic function using promises in one of my controllers to make sure it works correctly before adding more complex functionality. I keep running into a "TypeError: undefined is not a function" error when trying t ...

Tips for postponing the initiation of multiple carousels on a single page?

On my webpage, there are several bootstrap 5.3 carousels that I want to start with a delay. The plan is for each carousel to begin at different intervals – the first one after 1 second, the second after 2 seconds, the third after 3 seconds, and so forth. ...

Develop a schema for an array of arrays in NodeJS using mongoose

Looking to establish a database schema for storing the following data: { name : "xyz", admin : "admin", expense : [ jan: [{expenseObject},{expenseObject}], feb: [[{expenseO ...

A guide to incorporating border radius to images within a composite image with sharp.js in Node.js

In my Node.js project, I am using the sharp library to combine a collection of images into a single image. Although I have successfully created the composite image, I now need to add a border radius to each of the images in the grid. Here is the code snip ...

Steer clear of Cross-Site Request Forgery through

As someone who is still learning about web security, I am curious about the best practices for using tokens on JavaScript requests to prevent CSRF attacks. Is it possible for someone to provide a code example? I already know how to implement this properly ...

Difficulty arises when collapsed text in Bootstrap clashes with the footer design

Hey there! I'm working on this website using Bootstrap, and I've encountered a problem. When you click the "See Wikipedia" button, the content expands and overlaps the footer in a strange way without changing the page height. I've tried adju ...