Deploy node.js application to nginx - node.js

I have a node.js backend application running on port 1337 and i want to deploy it to my nginx server so i can get access to my backend from another computer and other netwroks.
i tried this in sites_availables and i enabled this configuration :
server {
listen 80;
server_name x.x.x.x(my server adrress);
location /node {
proxy_pass http://localhost:1337;
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;
}
}
then i restarted nginx
sudo service nginx restart
now i'm trying to reach x.x.x.x/node through my browser and i get 501 bad getaway.

The reverse proxy pipe from nginx to node is being broken because your path in the nginx config /node does not exist on your node application. You either need to implement that route, or do a rewrite to have nginx rewrite the path when proxying (check out this question: https://serverfault.com/questions/586586/nginx-redirect-via-proxy-rewrite-and-preserve-url)

Related

Node JS hosting using Nginx Reverse proxy gives 404 error

I want to host a Nodejs API server to my digitalocean server where a WordPress application is already running using Nginx and PHP fpm
I followed the below link to set up the WordPress application and it's working fine now.
https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-web-server-and-reverse-proxy-for-apache-on-one-ubuntu-18-04-server
I wanted to set up Nodejs application inside the same server for demo purposes and I followed the digitalocean guide for setting up node js with a different config file and subdomain.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
My Nginx config for node application looks like this
server {
server_name sub.domain.com
location / {
proxy_pass http://localhost:6969;
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 have allowed port 6969 using ufw allow 6969.
I am able to access the Nodejs application using sub.domain.com:6969 but sub.domain.com gives me a 404 error. (404 Not Found nginx/1.18.0 (Ubuntu))
I want to access Nodejs application directly without a port number. I have checked Nginx logs and there are no errors, and configures is gives success in nginx -t
Please give me some suggestions to debug and fix this issue. I don't have much knowledge in Nginx configuration. I was just following tutorials from Digitalocean to configure the WordPress and node application.
Thanks in Advance
You are missing the port
server {
server_name sub.domain.com;
listen: 80;
location / {
proxy_pass http://localhost:6969;
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;
}

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

Can I upload multiple node.js apps under one domain?

I use DreamHost as my provider, and I have a VPS plan, allowing me to upload nodejs apps. However I wish to be able to have multiple small apps under one domain, for example
www.example.com/app1
www.example.com/app2
www.example.com/app3
Is this possible with Node.js?
You cant run multiple apps on a single port.
But we can run apps on different port and redirect the url to correct app using nginx e.g
server {
listen 80;
root /var/www;
server_name www.example.com;
location /app1 {
proxy_pass http://localhost:{port}; // app1 server Comment 1
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 /app2 {
proxy_pass http://localhost:{port}; // app2 server Comment 2
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 redirect www.example.com/app1 to app1 server and www.example.com/app2 to app2 server. See comment 1 and 2.
You can't run multiple apps on single port but you can run them on different ports and can access them using those ports like
www.example.com //app1 (default http port is 80 and default https port is 443 hence you don't need to specify port for the apps using port 80)
www.example.com:3000 //app2

Configure nginx for both Vue app front end and node.js backend

I have been searching for this answer for months and have never been able to resolve this. I am using nginx as my web server and using node.js for my backend apis and vue as my main front end webpage that uses webpack. I would like to be able to go to my Vue page by going to http://ip and then get access to the api services by going to http://ip/api. I dont have a domain name set up so I use IP address for the urls.
So far I built my Vue app and is sitting in the /var/www/html/Web/dist folder and it works if you go to the http://ip url but I can not access my node.js APIs. My node.js server is running on localhost port 3000. My nginx config looks like this:
server {
listen 80;
root /var/www/html/Web/dist;
}
server {
listen 80;
location /api/ {
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;
}
}
You are duplicating server configuration. Simply keep one server properties set :
server {
listen 80;
root /var/www/html/Web/dist;
location /api/ {
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;
}
}
That means :
listens to all request for all ip's on port 80
Evaluate relative path from root /var/www/html/Web/dist;
if there's a matching '/api/' in url pass it to localhost:3000
It should work as expected now

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.

Resources