nginx refused to connect node app on ec2 instance - node.js

i have simple nodejs app running on ec2 instance with nginx configs
when tried to access the app from browser it give me "ec2-18-223-0-201.us-east-2.compute.amazonaws.com refused to connect."
when trying to curl it from VM
using curl http://localhost:3000 it works correctly, however when trying curl http://127.0.0.1:3000 it give me this output
Found. Redirecting to https://127.0.0.1:3000/
here's my nginx configs
upstream test{
server 127.0.0.1:3000;
}
server {
listen 80;
server_name ec2-18-223-0-201.us-east-2.compute.amazonaws.com www.ec2-18-223-0-201.us-east-2.compute.amazonaws.com;
location / {
client_max_body_size 20M;
client_body_buffer_size 128k;
proxy_pass http://test;
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;
}
}

One thing that should be clear before the actual problem. Is there in redirect policy in node app that returns below output?
curl http://127.0.0.1:3000 it gives me this output
Found. Redirecting to https://127.0.0.1:3000/ because redirection is
expected from Nginx, not from node app.
But I am sure the problem is with Nginx not with Node app as it is able to respond on a local port 3000.
refused to connect to connect mean that the server not running at all or the port may disable from the firewall.
Two possible reasons:
The Port 80 is not allowed in Security Group of the instance so allow 80 in the security group of AWS instance.
The Nginx is not running. Check the log under tail -f /var/log/nginx/error.log and the reason might be the log name of the DNS in the sever section.
So therefor two Suggestion for Nginx config
update your Nginx config to support long DNS name
vim /etc/nginx/nginx.conf and add value under http section in the config
http {
server_names_hash_bucket_size 512;
....
}
2. Remove redundent name from the config, its not be the reason but you should remove server_name ec2-18-223-0-201.us-east-2.compute.amazonaws.com www.ec2-18-223-0-201.us-east-2.compute.amazonaws.com;

Related

How Do I Setup NGINX reverse proxy for a Node.js app with Linode NodeBalancer so that I don't get 502 Bad Gateway

I have set up a Linode NodeBalancer to handle HTTPS traffic for my website, https://adamhelm.com, which listens on port 443. I have also set up an nginx server to route traffic to my Node.js application on port 3000 with the following nginx configuration:
listen 443;
server_name www.adamhelm.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;
}
When I try to access my website at https://adamhelm.com, I get a "502 Bad Gateway" error on my web browser. The error logs from NGINX show that there is a "Permission denied" error when trying to connect to the upstream server at http://45.79.140.133:3000. The connection to the upstream server is being made from the NodeBalancer with a private connection using the IP address 192.168.255.142.
I am able to access the app through the host's IP address directly at http://45.79.140.133:3000 without any issues.
I am looking for help to correctly configure this setup.
The Node.js app communication to NGINX requires opening outbound ports. Anyone wishing to leverage NGINX as a reverse proxies to Node.js with Linode NodeBalancers should be aware of this.
The solution for CentOS7 is:
Solution Reference URL
yum install policycoreutils-python
semanage port --add --type http_port_t --proto tcp 8001

404 not found, nginx looking for the default path 'etc/nginx/html/index.html' on NextJS application

I'm currently in need of some help with this 404 issue I've been stuck on for days now. My system is running CentOS7 (CPanel, VPS) and it uses engintron for the nginx reverse proxy and pm2 to keep my next.js application running.
default.conf
server {
#listen 80 default_server;
listen 80;
server_name my.domain.name domain.name;
location / {
# reverse proxy for next server
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;
}
location /_next/static/ {
alias path/to/nextjs/static/folder;
expires 365d;
access_log off;
}
# deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy
# Set the port for HTTP proxying
set $PROXY_TO_PORT 8080;
include common_http.conf;
}
All the other files I haven't touched, so it's safe to rule them out as they're the default ones that come with engintron.
Connecting to my.domain.name, gives me this error in the console.
"/etc/nginx/html/index.html" is not found (2: No such file or directory), client: ::ffff:my.ip.address, server: my.domain.name, request: "GET / HTTP/2.0", host: "my.domain.name"
My best guess on why this problem occurs is because my domain/server can't receive the files or doesn't have permission through the proxy at all as getting the data at 127.0.0.1:3000 via curl http://127.0.0.1/3000 returns my site's code in full.
Any ideas on what could be causing this? Have an amazing rest of your day :)
Engintron can make proxying to Node apps easy. Have a look at the docs (engintron.com/docs) as well as the "custom rules" files in WHM > Plugins > Engintron.
As long as you load your Node site in your public_html folder for the given account (Apache cannot render it either way, so it's not an issue) and _next/static/ is inside that public_html folder (for symmetrical proxying), then the Node.js proxy rule you set in Engintron's custom rules (again see the docs) will be sufficient for your needs.
I would also recommend you revert any changes you did in Engintron's Nginx config files.

Simply nginx reverse proxy from localhost:80 to localhost:8080 not working

im learning reverse proxy w/ nginx for the first time, and the following isnt working for me
im trying to reroute requests from http://localhost to an api server i have running at http://localhost:8080
server {
listen 80;
location / {
proxy_pass http://localhost:8080;
}
}
when i hit http://localhost, I simply get shown the welcome to nginx splash screen.
if i hit http://localhost:8080, i see my api
I have a node express service running at :8080, which i can hit manually, but shouldn't http://localhost be proxied there too?
When I setup a nginx domain that forwards requests to a node server, it looks like this, for the server_name, you can use localhost as a parameter for accessing it via localhost. You can also pass default_server to make this the default server config.
Note: Only one active config can contain default_server otherwise Nginx will throw errors.
Note: When using default_server, Nginx will catch localhost in that server config. Otherwise you need to specify localhost in the list of server_name's (separated by a space).
server {
# Setup the domain name(s)
server_name example.com;
listen 80 default_server;
# If you would like to gzip your stuff
gzip on;
gzip_min_length 1;
gzip_types *;
# Setup the proxy
# This will forward all requests to the server
# and then it will relay the servers response back to the client
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
Found out that adding this to my nginx.conf fixes the issue:
listen [::]:80;
For some reason listen 80; doesn't catch my http://localhost requests.

IIS and Node Side by Side on same Windows sever

I have a application with UI on Angular and Web API on Nodejs deployed on following Web servers( on the same Windows server say "server1").
Have configured multiple IPs on the server as listed below
UI -- IIS , listening on 10.250.18.51 with port 80 on
server1
API -- NodeJs , listening on 10.250.18.52 with port 80 on
server1. Ultimately will be using Nginx to forward the request to nodejs
Problem Statement
Not able to run both Nodejs and IIS on port 80 even though both listening to different IPs
IIS error : The port is already used by another process
NodeJs Error : EACCES, Permission denied
Am I missing something ?
Regards
Although they are running on different IP, They are running on the same machine so on the very same machine you can not run two processes on the same node.
As you are using nginx the best solution would be making a reverse proxy for node process.
Use different domains for both processes and use Nginx to forward them like the following:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.xxx.me xxx.me;
# your IIS config for nginx
}
# slack
server {
listen 80;
server_name xxx.xxx.me;
location / {
proxy_pass http://localhost:7777;
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;
}
}
Above snippet assumes that your node process is running on 7777

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;
}
}

Resources