In this guide, you will learn how to install and configure NGINX on an Ubuntu 18.04 Linode in order to create a reverse proxy to a running Node.js server. Node.js is an open-source JavaScript runtime environment that is often used to create and serve web applications, and NGINX is commonly used to serve Node.js applications. After the installation and configuration of NGINX is complete, you will create a test JavaScript file in order to check that your Node.js server is running properly. Thus, with the help of this guide, you will be able to serve dynamic and responsive content from your Node.js server with the help of NGINX.
Unlock the Secrets of Success: Get Ready to Take Your First Step with Before You Begin!
- Securely Purchase a Domain Name and Easily Link It to Your Site with Linode’s DNS Manager!
- Unlock the Power of Your Linode: Get Started and Secure Your Compute Instance Now!
Unlock the Potential of Your Server with Easy Installation and Configuration of NGINX!
- Unlock Amazing Possibilities with NGINX and Screen – Create Your Own Node.js Web Server File Now!
sudo apt-get install nginx screen
- Start NGINX and enable it to start automatically on reboots.
sudo systemctl start nginx
sudo systemctl enable nginx
- Create a New NGINX Site Configuration File – Step-by-Step Guide for Your Domain or IP Address!
File: /etc/nginx/sites-available/example.com
#Names a server and declares the listening port
server {
listen 80;
server_name example.com www.example.com;
#Configures the publicly served root directory
#Configures the index file to be served
root /var/www/example.com;
index index.html index.htm;
#These lines create a bypass for certain pathnames
#www.example.com/test.js is now routed to port 3000
#instead of port 80
location ~* \.(js)$ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
}
}
- Link Your NGINX Configuration for Maximum Efficiency – Learn How to Create a Symlink from Sites-Available to Sites-Enabled!
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com
- Unlink NGINX’s Default Site Configuration File in Just a Few Simple Steps!
sudo rm /etc/nginx/sites-enabled/default
- Ensure Your Site’s Configuration File is Error-Free with This Simple Check!
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
- Restart NGINX Now – Load Your Site’s Configuration in an Instant!
sudo systemctl restart nginx
Unlock the Potential of Your Website with an Index File – Create Your Site’s Index File Now!
- Step-by-Step Guide: Create Your Website’s Root Directory and Securely Store Your Index.html File!
sudo mkdir -p /var/www/example.com
- Create a Professional Site with Just a Few Clicks: Use the Text Editor of Your Choice to Create Your Site’s Index File in the Root Directory!
File: /var/www/example.com/index.html
<!DOCTYPE html>
<html>
<body>
<p><strong>If you have not finished the <a href="https://www.linode.com/docs/guides/how-to-install-nodejs-and-nginx-on-ubuntu-18-04/">guide</a>, the button below will not work.</strong></p>
<p>The button links to test.js. The test.js request is passed through NGINX and then handled by the Node.js server.</p>
<a href="test.js">
<button type="button">Go to test.js</button>
</a>
</body>
</html>
Bring Your Web Server to Life with Node.js – Create Your Node.js Web Server Today!
- Easily Manage Multiple Node.js Versions with Node Version Manager (NVM)!
sudo wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.35.3/install.sh | bash
- Unlock the Power of NVM in Just Minutes – Load It in Your Current Terminal Session!
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm --version
0.35.3
- Unlock the Power of Node.js: Get Up and Running Quickly with Easy Installation!
nvm install 12.16.2
- Say Goodbye to Node.js Version Conflicts – Use NVM to Run Your Preferred Version!
nvm use 12.16.2
Now using node v12.16.2 (npm v6.14.4)
Learn How to Easily Create a Test JavaScript File and Take Your Coding Skills to the Next Level!
In the Install and Configure NGINX section, you configured NGINX to listen on port 80 to serve its static content, as well as a reverse proxy to your Linode’s localhost:3000 when a request for the /test.js file is made. Consequently, in this section, you will create the test.js file to be able to test your Node.js web server, which will be created in the next section.
- Put Your Tests to the Test: Create a test.js File in the Root Directory of Your Site!
File: /var/www/example.com/test.js
<!DOCTYPE html>
<html>
<body>
<h2>
Your Node.JS server is working.
</h2>
<p>
The below button is technically dynamic. You are now using Javascript on both the client-side and the server-side.</p>
<button type="button" onclick="document.getElementById('sample').innerHTML = Date()"> Display the date and time.</button>
<p id="sample"></p>
</body>
</html>
Build Your Own Web Server with Node.js – Step-by-Step Guide!
- Create a Node.js Server in Your Site’s Root Directory in Just a Few Easy Steps!
File: /var/www/example.com/server.js
//nodejs.org/api for API docs
//Node.js web server
var http = require("http"), //Import Node.js modules
url = require("url"),
path = require("path"),
fs = require("fs");
http.createServer(function(request, response) { //Create server
var name = url.parse(request.url).pathname; //Parse URL
var filename = path.join(process.cwd(), name); //Create filename
fs.readFile(filename, "binary", function(err, file) { //Read file
if(err) { //Tracking Errors
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200); //Header request response
response.write(file, "binary"); //Sends body response
response.end(); //Signals to server that
}); //header and body sent
}).listen(3000); //Listening port
console.log("Server is listening on port 3000.") //Terminal output
- Unlock the Power of Multiple Terminals with a New Screen Session!
screen
- Discover How to Quickly and Easily Locate Your test.js File in Your Root Directory!
cd /var/www/example.com
- Keep Your Node.js Web Server Running in the Background: Append & to the End of the Command!
node server.js &
- Effortlessly Exit Your Screen Session with Just Two Keystrokes!
- Unlock the Magic of the Web with a Simple Click: Access Your Site’s Index.html Page Now!
- Load the Test.js Page Dynamically with Your Node.js Web Server – Click Here Now!
- Experience the Magic of Automatically Showing the Current Date and Time with a Single Click!