Nginx + NodeJs not working together - node.js

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!

Related

Deploy nuxt application on a nginx VPS server running ubuntu

I am trying to deploy a nuxt blog to a virtual private server that runs nginx.
The blog is supposed to be accessible when I browse to https://exampledomain.com/articles
I have managed to run npm run dev on the server successifully... The app is running on localhost:3000 on the server....
I need to set a reverse proxy on the server to redirect all requests from https://exampledomain.com/articles/ to localhost:3000
I have tried this twice and its failing.... When I browse https://exampledomain.com:3000 the app is loading forever.... when I go to https://exampledomain.com/articles it says "Page not working", or "Internal server error"
Kindly assist
This could be happening due to incorrect configuration.
Try
sudo nano /etc/nginx/sites-available/your-domain.com
Remember to change the your-domain.com to your desire domain
server {
listen 80;
listen [::]:80;
index index.html;
server_name your-domain.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;
}
}

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 redirecting to port 80

I am having issues redirecting my port 80 to 4000 where my nodejs application is running on my Digital Ocean Ubuntu Droplet. Here is my /etc/nginx/sites-available/default file:
server {
listen 80;
server_name my_site.com;
location / {
proxy_pass http://MY_IP_ADDRESS:4000;
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;
}
}
When ever I head over to my droplet's IP on port 4000 the application loads successfully but not when I try on port 80.
You are doing a proxy_pass of port :8080 but you said your application is running on port 4000.
You need to tell nginx the correct port to proxy, (4000 instead of 8080)
Then you need to restart the nginx service sudo service nginx restart

How to configure nginx to redirect to same app from multiple ports?

I have a node.js app and I am running nginx as a reverse proxy server. I want to access the same app through multiple ways.
Let the ip address of machine be 12.16.12.113.
Then I want the same app to be accessible through:
12.16.12.113:3000
test.example.com
I have 2 Configuration files for nginx in sites-available directory:
file1:
server {
listen 80;
server_name test.example.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;
}
}
file2:
server {
listen 3000;
server_name default_server;
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;
}
}
The problem is that if I link both the files in sites-enabled directory, then only the app is accessible only through 12.16.12.113:3000. If I remove file2, then it is accessible through test.example.com.
But I want both the methods to work.
Edit 1:
Based on suggestion from this answer, I did the following test:
It appears that nginx is not listening to port 80. Why is this happening?
The are few ways to achieve that.
You can make your Node app listen on 12.16.12.113:3000 instead of on localhost:3000 and then you will need nginx only for port 80.
You can make your Node app listen on localhost:3000 (but only on localhost) and then proxy 12.16.12.113:3000 and port 80 by nginx.
You can make your Node app listen on localhost:4000 or 0.0.0.0:4000 on any interface and use nginx to proxy requests to port 80 and 3000.
But you will not be able to make them both listen on the same port and the same interface without problems.
This is the only advice that could be given considering the fact that you didn't include any code example of how your Node app is binding to the ports, which would be the only relevant information here.

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