hosting nodejs applications using nginx in ec2 AWS - node.js

/etc/nginx/nginx.conf file
in http section, given the below informaiton.
include /etc/nginx/conf.d/*.conf;
/etc/nginx/conf.d/myapp.conf file content
server {
listen 80;
listen [::]:80;
server_name _;
index index.html index.htm;
location / {
proxy_pass 'http://XXXXX:3005/';
}
location /admin/ {
proxy_pass 'http://XXXX:3000/';
}
}
http://XXXX is redirecting to the main page.
http://xxxxx/admin/ is not redirecting. Instead it is throwing below errors in chrome console.
error image
The same site is working fine http://xxxx:3000 it is working fine. No issues.
Whatelse am I missing?
Note: OS is Amazon Linux & I have tested the same configuration with sample help world node js program & it is working fine.

Make sure that your routing is correct and that you have restarted nginx after configuration.
Try the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3005;
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 /admin {
proxy_pass http://APP_PRIVATE_IP_ADDRESS: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;
}
}

Related

How can I route to multiple NodeJS applications using Nginx?

I have 2 NodeJS applications running locally on port 3000 and 3001. I'm trying to route to them individually by using my_ip/tilbudsfinneren for one of them, and my_ip/hms for the other one.
My sites-available file looks like this:
server {
listen 80;
listen [::]:80;
root /var/www/my_ip/html;
index index.html index.htm index.nginx-debian.html;
server_name my_ip www.my_ip;
location /tilbudsfinneren {
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;
}
location /hms {
proxy_pass http://localhost:3001;
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;
}
}
Whenever I try to reach my_ip/hms or my_ip/hms/ping it does not reach the NodeJS application, I get get "Cannot GET /hms". Is there an issue with my nginx configuration, or is there maybe something else wrong?
If you want to serve both apps as reverse proxy you must use rewrite methods in both locations.
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
location /tilbudsfinneren/ {
rewrite ^/tilbudsfinneren/(.*)$ /$1 break;
proxy_pass http://localhost:3000;
#other config...
}
location /hms/ {
rewrite ^/hms/(.*)$ /$1 break;
proxy_pass http://localhost:3001;
}
}

Node.js is not working in Reactjs app - nginx

Locally everythings is working correct, now im trying to get the node working but i can't
It's my nginx config:
server {
listen 80;
server_name 54.38.184.210;
location /api/ {
proxy_pass http://54.38.184.210:8080/;
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 / {
root /root/site/public;
try_files $uri /index.html;
}
}
http://54.38.184.210/
i run - npm run build, then i put all dist files into public, the static content is working, i can see whole website, but the backend is not working, im starting my server using "node index.js"
Maybe im doing something wrong, please help me!
Please try this,
server {
listen 443 ssl;
server_name 54.38.184.210;
ssl_certificate {your path to .crt file}/api.crt;
ssl_certificate_key {your path to .rsa file}/api.rsa;
location / {
proxy_pass 54.38.184.210:8080;
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;
}
}
"service nginx restart" to restart your backend nginx

Nginx reverse proxy to NodeJS App causing 502 bad gateway error

I am setting up nginx so that I can access my API built using express through a url like - example.com/api
Here is my nginx config
upstream appfrontend {
server localhost:9008 fail_timeout=0;
}
upstream api {
server localhost:3001;
}
server {
listen 80;
listen [::]:80;
server_name hospoline.com www.hospoline.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
listen [::]:443;
server_name example.com; # replace this with your domain
root /var/www/html/example-certbot-webroot;
# The public and private parts of the certificate are linked here
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location /.well-known {
root /var/www/html/example;
}
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://appfrontend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 900s;
}
location /api {
proxy_pass http://api;
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 visit my site example.com the front end loads perfectly.
When I visit example.com/api/fetch_doctors, I get a 502 bad gateway error.
My API is working fine in localhost. When I send a request to localhost:3001 on my local computer, I get the list of doctors.
Both my front end server and backend server are run using forever.
I am really lost and breaking my head about this for one full day. I have no idea where I'm going wrong. Any help in the right direction would be great! Thank you all!

HTTPS with Nginx and Nodejs

So I used certbot to configure Nginx for https. That worked well (I used the automatic configuration).
Now how can I configure my Node.js back end to be able to make GET/POST requests from the front end that is being hosted with Nginx?
EDIT:
location /api {
proxy_pass http://localhost:3000; #server port
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 / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
If you have correctly configured your Nginx with SSl and your node application. The requests you send should work with https://URL . Please check your nginx.conf and verify the following things.
You can add ssl cert in Nginx with
ssl_certificate /etc/nginx/ssl/server.crt #cert location
ssl_certificate_key /etc/nginx/ssl/server.key #key location
in your server block in Nginx.And your nodejs App should be configured like this in the server block
location / {
proxy_pass http://localhost:3000; #server port
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;
}
This will be sufficient for your server to work with SSL.

How to Create a NodeJS http Proxy

I tried proxying my nodejs application running at http://localhost:7000 to domainname.com/api using nginx config but domainname.com/api works perfectly however domainname.com/api/info is not
Here is my nginx config
server {
listen 80;
server_name domainname.com
location /api/ {
proxy_pass http://localhost:7000;
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;
} }
Figured out the issue. trailing slash is the one required as shown
proxy_pass http://localhost:7000/;

Resources