BHUVNESH KUMAR
BHUVNESH KUMAR

Reputation: 401

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.

Upvotes: 1

Views: 1329

Answers (4)

Harshit
Harshit

Reputation: 1

Removing port number after the domain/IP

Here we discuss how we can apply the same principles to domains as we do based on IP.

We should have Nginx installed on the EC2 instance, and then we need to update the port and IP on the default page. Currently, the app is running on port 3000 at IP address 21.11.42.250.

currently running like this - http://21.11.42.250:3000

Follow these commands for Nginx installation:

-- sudo apt install nginx

Visit this path to update the port and IP:

-- cd /etc/nginx/sites-enabled/

-- sudo vim default

Remove the complete data from the default page and update it with the following:

server {

server_name 21.11.42.250; // your current IP address or domain name
location / {
    proxy_pass http://127.0.0.1:3000; // local running port initialization
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

}

Restart the Nginx service:

-- sudo service nginx restart

After following these steps, the port number is not required, and you can run your code without specifying the port - 21.11.42.250.

Upvotes: 0

Amol Barshile
Amol Barshile

Reputation: 216

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.

Upvotes: 1

user3234800
user3234800

Reputation: 76

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.

Upvotes: 0

Bipul Jain
Bipul Jain

Reputation: 4643

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.

Upvotes: 0

Related Questions