In modern web development, with the continuous development of front-end technology, Node.js is often used to build web applications. For those who develop web applications using Node.js, "Nginx" is a very familiar and common word, as it is a very powerful and popular web server that can be used with Node.js. In this article, we will take a deep dive into how to deploy Node.js applications using Nginx so that you can have more control over where your web applications are served from.
What is Nginx
Nginx is a popular web server that is suitable for various websites and is also present in many high-load websites. It is characterized by fast speed, low resource usage, and can handle a large number of connection requests. Nginx configuration is very flexible and can be modified as needed. Nginx can serve as a reverse proxy server and can handle static files, redirection, load balancing, etc.
What is Node.js
Node.js is a platform built on the Chrome V8 JavaScript engine for building fast, scalable and efficient web applications. Node.js uses event-driven and non-blocking I/O models, so it can handle a large number of connection requests and cope with high-load scenarios. Node.js is great for writing real-time web applications and API servers. It is an open source software that has become a widely used technology in the web development community.
How to deploy Node.js application on Nginx
In order to deploy Node.js application on Nginx, we need to perform the following steps:
Before deploying a Node.js application, you need to install Node.js and Nginx on the server.
Install Node.js on Ubuntu:
~$ curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
~$ sudo apt- get install -y nodejs
Install Nginx on Ubuntu:
~$ sudo apt update
~$ sudo apt install nginx
Create a Node.js application as an example:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(3000, ' 127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');
Save as hello.js
Start the application
node hello.js
Nginx By default, the /etc/nginx/sites-available/default file is used to set the configuration of each virtual host. We can edit this file and configure it to add a reverse proxy server to forward connection requests from the Nginx server to the Node.js application.
Edit the default file
sudo nano /etc/nginx/sites-available/default
Modify the file to the following content:
server {
listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1: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; }
}
In this example, we configure the server to listen on port 80 and forward requests to port 3000 of the Node.js application.
Restart Nginx for the configuration to take effect.
sudo service nginx restart
Now, we should be able to access the application directly.
http://example.com/
We have completed the Nginx deployment of the Node.js program. Now you have learned how to use Node.js as a reverse proxy server and use Nginx Deploy Node.js applications in production. By learning more about Nginx and Node.js, we can better master web development and server management skills.
The above is the detailed content of How to deploy nginx for nodejs service. For more information, please follow other related articles on the PHP Chinese website!