Setting up node js apps on nginx - node.js

i have two node js applications but am having trouble setting up on nginx only one is running properly.
The first app is running okay on a domain example.com
but the second app am having trouble it is the admin panel so i want to be visiting example.com/admin to access it how can i configure this on nginx
I added the following server block where should i change
server{
listen 80;
server_name example.com www.example.com;
location /admin {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:4001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# location /overview {
# proxy_pass http://127.0.0.1:4001$request_uri;
# proxy_redirect off;
# }
}
}
i added the location directive location /admin to point to where i want to access the admin panel

Related

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.

nginx as a reverse proxy for API cannot redirect full url, params, body headers

I'am putting my node js API (that I manage with pm2) behind a reverse proxy with nginx, here no problem.
But since, if I send any request it become a GET on '/'.
How can I tell to nginx to forward the full url, the request types (PUT, OPTIONS, DELETE, ...), the requests params and the request body ?
here is my simple nginx config.
server {
listen 80;
listen [::]:80;
server_name api-prod.mysite.com www.api-prod.mysite.com localhost;
location / {
proxy_pass http://127.0.0.1:3111;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Your nginx configuration seems good except, you should use proxy_http_version and proxy_cache_bypass because sometimes(depends on version) Nginx and nodejs using different HTTP version.
Following configuration working fine for me
server {
listen 80;
server_name arifjaunpur.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;
}
}

hosting nodejs applications using nginx in ec2 AWS

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

Multiple meteor applications using the same domain in digitalocean using nginx?

I'm trying to host 2 applications in the same droplet in digitalocean by using nginx
But so far I've only been able to get the root application running (the one without example.com/secondapp)
I want to be able to not use a subdomain and just use example.com/secondmeteorapp to be able to access it.
My sites-enabled/default looks like this:
server {
listen 80;
#listen 443 ssl;
server_name example.com/;
#ssl_certificate /etc/nginx/ssl/ssl-bundle-myApp-domain-com.crt;
#ssl_certificate_key /etc/nginx/ssl/myApp_domain_com.key;
location /dragonfire {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto http;
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
}
location /images {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location /fonts {
alias /home/dragonfire-build/bundle/programs/web.browser/assets/images;
access_log off;
expires max;
}
location "^/[a-z0-9]{40}\.(css|js)$" {
root /home/dragonfire-build/bundle/programs/web.browser;
access_log off;
expires max;
}
}
however, when I access http://serverIpAddress/dragonfire it can't find the css or javascript giving me this error:
GET http://myipaddress/1f3848edee9e199050b9b1965b9e697aa714b9f3.css?meteor_css_resource=true
GET http://myipaddress/6e48198c6b584ff79c86e0c624a65b4853faaf50.js?meteor_js_resource=true 404 (Not Found)
I can access the app if I go directly through the IP address and port but not via the nginx way
QUESTION
How can I access a second app using the same domain but with a /mysecondapp (in this case /dragonfire at the end?

Resources