multiple nodejs apps for multiple domains on the same machine/ip. switching ports - node.js

We have multiple domains each have embedded nodejs app we need to make every nodejs app running url like : http://domain_name:port_number
but we have a strange problem that every port we set for any nodejs app could be accessed from one domain not the domain we set the port in its nodejs app except 8080
How to make all ports accessible from all domains running on the same machine/ip or open port number for a domain
we use apache as the web server not using express or nginx

The problem solved there was a dns settings on cloudeflare prevents completing requests

Related

How do I deploy a Next.js app to an addon domain on cPanel, which listens to port 3000 without affecting my other domains?

I have a website running on my main domain added in cPanel listening to the main "80" port and can be opened without adding the port to the URL. for example: "http://mywebsite.com".
Now I want to add another website on a new addon domain. But this website is a Next.js app running on port 3000.
I used SSH to successfully build and start the next.js app.
The next.js app opens perfectly on the newly added addon domain with the port 3000. For example "http://new-website.com:3000"
- If I change the port from 3000 to 80 when starting the next.js app, I can open it without adding the port, but the problem is, that my other domain will now also open the next.js app.
My server Information:
Virtual instance running on google cloud.
CentOS 7
Apache Web server (I can turn off Apache and run NGINX if needed)
cPanel installed
I can of course just create another virtual instance and add my new website there but I don't want to pay double when my current instance is capable of running multiple websites on different domains.
Everything is working perfectly with my both websites and the only problem is I cannot have my both websites work currently without having to add port number to my next.js app.
How do I open my new next.js app without adding the port in a way that my other static website won't be affected and show it's content like before?
Tried:
I used NGINX reverse proxy, but the same thing happens.
Changing next.js port from 3000 to 80
Exporting the next.js app and uploading it as static using "npx next export" but website not working as expected.
Turns out it is possible to do this with Nginx reverse proxy and setting different ports and assigning them to different domains!
Got the answer from this Stackoverflow question:
Port numbers not hiding in nginx reverse proxy (next js server)
The link to the answer
In my case I changed server name from subdomain to my other website and removed the location since my other website is static and does not need port.
server {
listen 80;
listen [::]:80;
server_name my-website-2.com;
root "Website_directory";
}

Best practice for Deploy Node Express application and php application on AWS ec2 instance

I have an AWS ec2 instance along with one domain. I want to deploy my express node application and PHP application on the same server without adding a port with the domain.
For PHP deployment I have configured xampp server with a default port of 80.
And my node application runs on port 3000.
Currently, I am accessing the node app using - mydomain.com:3000
and for the PHP - mydomain.com/website
I wanted to use an express node app without entering the port number. example - mydomain.com
Is there any way to configure it with my given scenario?
Perhaps what you want is to run two different applications on one physical server.
How about approaching the concept of load balancing on one server?
Typically, you can implement this using nginx's reverse proxy and upstream.
For example, if the node server is running on port 3000 and the php server is running on port 4000
In nginx node.mydomain.com is port 3000
php.mydomain.com can send on port 4000.

How to deploy different MERN apps to digital ocean on a single droplet?

I've always used heroku to deploy my MERN apps. For the mongo db I use MongoDB Atlas, but in my job they want to migrate all the projects to DigitalOcean. I have several questions regarding this:
Can I have mongoDB + nodejs backend + react app on a single
droplet?
Can I deploy two or more apps in a single droplet? (The
apps have different domains)
Is there a video tutorial about this
(I've read lots of documentations and got many errors while trying
to do it. My eyes hurt 🙃)
For example if I have in Heroku two apps for the same website, one app for the nodejs backend and another one for the react frontend... can I do the same on DigitalOcean?
Thanks in advance!
Yeah, you can deploy multiple services in a single server, they just need to be listening on different ports.
For example, let's consider that a MongoDB server is running on port 27017, a Node.js http server is running on port 5000, and a React app is running on port 8000.
Say, your server's IP is 13.13.13.13.
Then you can access your MongoDB server, Node.js http server, and React app using 13.13.13.13:27017, 13.13.13.13:5000, and 13.13.13.13:8000, respectively, from anywhere in the Internet where your IP isn't blocked.
Now, in your server, you set up iptables to forward all incoming connections from port 8000 to 80. Now, you can access your React app by visiting 13.13.13.13, no need to use the port anymore.
Now, let's say, you add DNS records for example.com and api.example.com to point to your IP. And since you can't have A records or CNAME records pointing to a port, both of your domains will direct you to your React app. You'll have to explicitly specify the port number along with your domain if you want to access your Node.js backend, like http://example.com:5000, or http://api.example.com:5000.
If you don't want to access your backend using the port number, you can make use of Nginx as a reverse proxy. You can set up Nginx to route all the traffic to api.example.com to your backend server listening on localhost:5000.

Do I need a different server to run node.js

sorry if this is a wrong question on this forum but I am simply just stuck and need some advice. I have a shared hosting service and a cloud based hosting server with node.js installed. I want to host my website as normal but I also want to add real time chat and location tracking using node.js I am confused with what I am reading in several places because node.js is itself a server but not designed to host websites? So I have to run 2 different servers? One for the website and one to run node.js? When I setup the cloud one with a node.js script running I can no longer access the webpages.
Whats the best way for me achieve this as I am just going round in circles. Also is there a way I can set up a server on my PC and run and test both of these together before hand so I see what is needed and get it working as it will stop me ordering servers I dont need.
Many thanks for any help or advice.
Node can serve webpages using a framework like Express, but can cause conflicts if run on the same port as another webserver program (Apache, etc). One solution could be to serve your webpages through your webserver on port 80 (or 443 for HTTPS) and run your node server on a different port in order to send information back and forth.
There are a number of ways you can achieve this but here is one popular approach.
You can use NGINX as your front facing web server and proxy the requests to your backend Node service.
In NGINX, for example, you will configure your upstream service as follows:
upstream lucyservice {
server 127.0.0.1:8000;
keepalive 64;
}
The 8000 you see above is just an example, you may be running your Node service on a different port.
Further in your config (in the server config section) you will proxy the requests to your service as follows:
location / {
proxy_pass http://lucyservice;
}
You're Node service can be running in a process manager like forever / pm2 etc. You can have multiple Node services running in a cluster depending on how many processors your machine has etc.
So to recap - your front facing web server will be handling all traffic on port 80 (HTTP) and or 443 (HTTPS) and this will proxy the requests to your Node service running on whatever port(s) you define. All of this can happen on one single server or multiple if you need / desire.

How to remove port from url for node application using nginx

My react and angular application (UI have two parts) are running using
node/express application on port 3000. On server.js (node-express entry point) I dynamically
handle which UI to render (react or angular at a time) on a browser using
express-static feature.
Earlier my application is running on - https://mywebsite.com:3000/ but
as per requirement it should be changed to - https://mywebsite.com which we
handled using "nginx proxy" with DevOps person but now encountered
another issue actually now UI is accessible by using both URL that is
https://mywebsite.com:3000/ and https://mywebsite.com. I want it should be
accessible by using https://mywebsite.com/ only without port.
My server's API's (https://mywebsite.com:3000/api/v1 ) is accessible from three places: -
1) iOS app
2) Admin app (running differently)
3) and UI ( React.js + Angular.js) (https://mywebsite.com)
Note- Is there any way to handle this either through the deployment process or setting at node/express server level. We found one solution that is to create a separate node server for the UI part but as per cost-cutting, we ignored this approach that is creating another server for UI.
Suppose my domain name is https://api.aegisapi.com:3000, first, you can check in your inbound, means HTTPS is present or not if not present then add HTTPS for 443,
then you can run https://api.aegisapi.com. It works.
When you run your node server like this
it listens on all interfaces i.e. 0.0.0.0 which is accessible from outside.
You should your listen to
from
app.listen(3000)
to
app.listen(3000, '127.0.0.1');
Also, you should also block this port with IPTables as well.
if using AWS EC2 instance then open inbound port 3000 and open public facing port e.g. 80 or 443 to get request from users, then as you are using nginx as reverse proxy use that to forward request to port 3000.

Resources