I have 2 node js app deploy to Digital Ocean (Ubuntu 16.04), I use pm2 for application management. I try to use nginx as a web server but now I want to run 2 app with different port for example:
App 1: http://my_ip:3001
App 2: http://my_ip:3002
I try to config nginx but it seem not work:
server {
listen 3001;
location / {
proxy_pass http://localhost:8080; //Node app
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;
}
}
It work only on port 80.
What configuration I need to do for this problem? Thanks
UPDATE: Just using ufw to open the ports
Related
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;
}
I have started node service in the port 3000 and i opened the port 3000 in the server, however i can't connect the web app. Anyone can help me, thank you!
Here is the screenshot of my port status.
When i use
nmap -p 3000 202.117.43.155
It shows
So the port 3000 is showed as status "filtered", where is the block?
in your node app check if you are using app.listen(PORT, IP_ADDRESS_OR_HOST )
try to use app.listen(PORT) instead`
I solved this problem by applying Nginx.
After install Nginx,
sudo nano /etc/nginx/sites-available/example.com
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;
}
}```
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
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
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)