visit
NGINX Open Source’s features and performance have made it a staple of high‑performance sites – it’s the #1 web server at the 100,000 busiest websites in the world
Consider the following node js application
const http = require('http');
const hostname = 'localhost';
const port = 5000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Sysmon App is Up and Running!\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at //${hostname}:${port}/`);
});
Now Install Nginx Reverse Proxy in Linux
Create a file called /etc/apt/sources.list.d/nginx.list and add the following lines to it.
deb //nginx.org/packages/ubuntu/ bionic nginx
deb-src //nginx.org/packages/ubuntu/ bionic nginx
$ wget --quiet //nginx.org/keys/nginx_signing.key && sudo apt-key add nginx_signing.key
$ sudo apt update
$ sudo apt install nginx
# wget --quiet //nginx.org/keys/nginx_signing.key && rpm --import nginx_signing.key
# yum install nginx
After successfully installing Nginx, start it, enable it to auto-start at system boot and check if it is up and running.
<strong>---------- On Debian/Ubuntu ----------</strong>
$ sudo systemctl status nginx
$ sudo systemctl enable nginx
$ sudo systemctl status nginx
If you are running a system firewall, you need to open port 80 (HTTP), 443 (HTTPS) and 5000 (Node app), which the web server listens on for client connection requests.
<strong>---------- On Debian/Ubuntu ----------</strong>
$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw allow 5000/tcp
$ sudo ufw reload
Configure Nginx as Reverse Proxy For Nodejs Application
Now create a server block configuration file for your Node app under /etc/nginx/conf.d/ as shown.
$ sudo vim /etc/nginx/conf.d/sysmon.conf
Copy and paste the following configuration (change localhost with your server IP and yourdomainname with your domain name).
server {
listen 80;
server_name sysmon.yourdomainname;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass //localhost:5000;
}
}
}
$ sudo systemctl restart nginx
OR
# systemctl restart nginx
Access Nodejs Application via Web Browser
Now you should be able to access your Node app without providing the
port it is listening on, in the URL: this is a much convenient way for
users to access it.
//sysmon.yourdomainname
For your test domain name to work, you need to setup local DNS using the /etc/hosts file, open it and add the line below in it (remember to change localhost with your server IP and yourdomainname with your domain name as before).
localhost sysmon.yourdomainname
Congratulations! You had successfully configured Nginx as a
reverse proxy for your Nodejs application.