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

Looking to retrieve country, state, city, and area based on inputting a pincode value using Node.js?

I'm currently working on a web project with nodeJs and ejs. I am looking for a solution that can automatically update the country, state, city, and area fields based on the input of a pin-code (zip-code). Are there any recommended packages in node js ...

What are the steps for creating a new npm package based on an existing one?

I'm a newcomer to the node ecosystem and the npm package system. In my redux/react web app, I currently make use of the photoswipe package alongside react-photoswipe. Recently, I decided to extend the functionality of the photoswipe package by making ...

vuejs default properties initialized with vue-i18n

I am trying to establish a default property from a dictionary in this way: props: { title: { type: String, default: this.$t("basic.confirm"), }, description: { type: String, } }, ... The $t function is part of the vu ...

Unable to trigger onSelect event on the datepicker component

Whenever a date is chosen, I need to trigger a javascript function. Here is the javascript code: $(document).ready(function(){ var date_input=$('input[name="date"]'); //our date input has the name "date" var container=$('.bootstrap- ...

Submit your information discreetly

Is there a way to submit a form to an external website without it causing the page to reload? I am working on automatically logging users into their Google account without interrupting their browsing experience. The following code snippet allows me to pr ...

Displaying a collection of items using ReactJS

Hey there! I have an array of objects containing comments for a particular item, and I only want to display the first 10 on the page. Below that list, I'm looking to add a button that allows users to see the next set of 10 comments when clicked. It&a ...

leaflet.js - Place a marker when clicked and update its position while dragging

Currently working on a small project that requires the ability to place a marker on an image-map powered by leaflet.js, and update its position if it's dragged. I was using the code below for this functionality but keep getting an error 'marker n ...

Create and store a new data entry in the custom taxonomy of a specific post

I have successfully added an upload image feature to my custom post type taxonomy. The post type is portfolio, and the taxonomy is portfolio-category. The input field for adding the image (which opens the WordPress uploader) is functioning properly. Howev ...

Using Express, Node, and JQuery to handle form submissions

I am struggling with form submissions while working on a web app using HTML, Express, and Node.js. Despite being new to these technologies, I have created a script that generates a dynamic form based on certain factors: $FormContainer.html(''); ...

What could be the reason for Express not setting the JSON content type?

I have utilized Express on numerous occasions in the past, and I do not recall encountering this particular issue before. It seems like there might be a mistake on my end, but I am struggling to identify what exactly that could be. My objective is quite s ...

I'm puzzled about what could be behind this error message Error [ERR_HTTP_HEADERS_SENT], especially since I've only sent the response header once. How can I figure out the cause

Here is a snippet of code from my routes file: router.get('/api/', async function(request, response){ let entries = await Entries.find({}, function(error){ if(error) console.log(error); }); let catArray = []; entrie ...

Showcasing a JSON file in the interface using $http in AngularJS

I am a beginner in angularjs (and programming). I am attempting to showcase a json file on my view (home.html) using $http and ngRepeat, but unfortunately, it is not working as expected. Upon inspecting the angular responses, I noticed that there are numer ...

A Simple Guide to Setting a Background Image in React Native with the Nativebase.io Library

What is the process for including a background image in React Native with the help of the Nativebase.io Library? I have a specific screen where I need to incorporate a background image, with all other elements positioned at the center of the image. ...

How do I retrieve the enclosure URL in the Alexa Skill FeedHelper.js file?

I am currently working on a project to create an Alexa feed skill using a blueprint provided by Amazon. The blueprint involves calling RSS feeds from a URL, transforming them into JSON format, and saving them on Amazon S3. The code responsible for this fu ...

Storing tweets in a mongodb database using nodejs

I have successfully written a code using nodejs to fetch data from twitter. Now, I am facing difficulty in storing it in mongodb. Can someone please provide me with step-by-step guidance on how to achieve this? I am unable to do it on my own. var app = ...

"Utilizing long-polling techniques for cross-subdomain AJAX requests

Currently, I am developing a notifications script that continuously checks a database for any updates and displays them in a custom JavaScript popup. I have successfully implemented the jQuery AJAX loading and processing script as well as the PHP long pol ...

Execute a JavaScript function when a form is submitted

Seeking to reacquaint myself with Javascript, encountering difficulties with this fundamental issue. https://jsfiddle.net/gfitzpatrick2/aw27toyv/3/ var name = document.getElementById("name"); function validate() { alert("Your name is " +name); } < ...

Can an event be initiated on dynamically loaded iframe content within a document using the jQuery "on" method?

Is there a way to include mousemove and keypress events for iframes? The code provided seems to work well with existing iframes on the page, but it doesn't seem to work for dynamically created frames in the document using JavaScript. $('iframe&a ...

Finding an element that lacks both a class and an id, and is not consistently present - what's the trick?

Currently, I am faced with a predicament in my code where a <li> element only appears under specific conditions, making it difficult to apply positioning. This element lacks an id and class attribute, which prevents me from targeting it accurately us ...

Changing images dynamically using Javascript when hovering over an element

Looking to create a dynamic image switch effect on hover, where multiple images cycle through when hovered over. Each time the mouse hovers over the image, it should switch to the next in a sequence of 5 images. <img id="switch" src="img1.jpg"> $(& ...