How to connect my domain to my node app with nginx - node.js

I bought ubuntu server in digitalocean.
I am connecting my server IP through ssh on my terminal and i create a small node app and after starting the app, I can able to see my app running on my http://myipaddress:nodeport
How i can i connect my domain to this?
I bought free domain from freenom for testing purpose.
By following some tutorials i make a /etc/nginx/sites-available folder into my server and create a file called default and write code like this
server {
listen 80;
server_name sameer.tk; // i bought this domain from freenom
location / {
proxy_pass http://my_private_ip:3004 //with nodeport
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;
}
}
But if i hit sameer.tk after the setup its not working, i even restart my nginx.

You no need any nginx for node server.
You may run node server as 80 port and open 80 port in network security group.
After that, you should point domain to your instance public ip address in your domain management panel.
Ref:
https://www.digitalocean.com/community/tutorials/how-to-point-to-digitalocean-nameservers-from-common-domain-registrars

Related

How to setup a node server (with nginx sat on top) to work with mongodb

I have a node express application which communicates with mongodb and serves back the response in JSON format after doing some processing. The application works as expected when run on a local machine.
This is how my connect code looks
await MongoClient.connect(uri, async function (err, client) {
...
}
However, I have deployed the application to an aws ec2 instance following this tutorial where I added nginx as a layer on top of my node application. Now I get a 504 Gateway Time-out on any routes that try to connect to mongodb.
The server block in my nginx configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}
I understand that mongodb does not use HTTP, which is what nginx uses for communication. So I have tried to follow this tutorial but have had no luck.
Can anybody point me in the right direction?
Turns out I had completely forgotten to whitelist my server's IP address when I deployed the app to an ec2 instance. Hence why everything worked as expected locally (my local IP address was whitelisted).
This had nothing to do with NGINX. My mistake.

How to host nodejs services into nginx server in VirtualMachine?

I wrote some nodejs services in my ubuntu local.Now I want to deploy my nodejs services into nginx server in my seperate VM.I set up the nginx in my virtual machine.How can I pull my nodejs services to nginx server and how to connect these api's through postman. I getting confusion at nginx config file.
You should setup a reverse proxy with nginx to redirect the traffic to you node application. Install node on your VM, copy your application and install all the dependencies using npm install. Afterwards, you should start the node application using node index.js where index.js is the entry point of your application. You could also use a process manager such as pm2 to start the application. Then, you have to setup the reverse proxy with nginx which is redirecting the traffic to the port of your application. (In your sample code 3000). The application should now be available on the IP of your VM. Below you find a minimal example configuration for nginx.
server {
listen 80;
server_name domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}

Cant access the site through the domain name hosted on vps

I have a nodejs website hosted on vps hosting listening on port 8000. I did reverse proxy using nginx to listen all request on port 8000 to port 80 by changing the /etc/nginx/sites-available/default to following:
server {
listen 80;
server_name www.greenlemonevents.com;
location / {
proxy_pass http://67.209.122.170:8000;
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;
}
}
where 67.209.122.170 is my remote server ipadress. When i access my site by the ipaddress it is working fine but when i access it by the domain name apache2 pages is shown. http://www.greenlemonevents.com/ this is showing the apache2 default page and this http://www.greenlemonevents.com:8000/ is working fine. I do not understand why i have to add port when it is working fine here: http://67.209.122.170/ .
I have purged apache2 from the vps server and also removed /var/www/html folder but still it is showing the default ubuntu page.
This is my first time hosting on a vps hosting so pardon if i seem a bit confused... :)

Nginx + NodeJs not working together

I am having troubles setting up NodeJs to work together with Nginx.
I have added forward of the subdomain to an IP address of my server. (when I ping subdomain it gives me the server IP address).
This is my config in /etc/nginx/sites-enabled/default
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name chat.mydomainname.com;
location / {
proxy_pass http://localhost:3000;
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;
}
}
My NodeJs server is running on port 3000 and I can access it via browser successfully on url http://MYIPADDRESS:3000
I noticed when I restart nginx server (service nginx restart) that I don't get any output that I usually should get.
I have tried to check what is on port 80 and does something blocks nginx from running. I killed those processes (which were all nginx) and I started it again, but no luck.
I am using Ubuntu 16.04 on Digital Ocean. (I have added subdomain on DigitalOcean also)
Can someone see some obvious mistake here?
Thanks!

Nginx proxy_pass doesn't work on port 80

I am using a DigitalOcean VPS hosting a meteor app. I don't have a domain name yet, so just use the plain IP address. When I set below config and use myipaddress:3000 and myipaddress:8080, both of them worked well; but if I change the 8080 to 80, only myipaddress:3000 works. Using only myipaddress or myipaddress:80 will show "Welcome to nginx on Debian!" message. (I use Ubuntu 14.04 on the VPS).
server {
listen 8080;
server_name default;
location / {
proxy_pass http://localhost:3000;
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;
}
}
Can not figure out why can't use port 80.
---- Solved this problem --------
I commented out the "listen 80 default_server" in the /etc/nginx/sites-enabled/default" file, then my config at "/etc/nginx/conf.d/mysite.conf" works on port 80.
You probably still have the default.conf still in the directory that nginx is using to serve up the sites. either that or check in nginx.conf. Somewhere there is a server setup already using 80 that is being served first.

Resources