Home  >  Article  >  Web Front-end  >  How to deploy nginx for nodejs service

How to deploy nginx for nodejs service

PHPz
PHPzOriginal
2023-04-05 13:48:251507browse

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:

  1. Install Node .js and Nginx

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

  1. Write Node.js Application

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

  1. Start the Node.js application

Start the application

node hello.js

  1. Configure Nginx

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.

  1. Restart Nginx

Restart Nginx for the configuration to take effect.

sudo service nginx restart

  1. Test the application

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn