Forever+nginx deployment method example of Node site

Forever+nginx deployment method example of Node site

I recently bought the cheapest Tencent cloud server, which is mainly used to deploy my personal blog and learn some liunx knowledge, so as to fully understand and master web technology. It is strongly recommended that front-end personnel have their own server. Before writing this article, My cloud server has already installed Mysql, node, nginx, etc. Let's take the deployment of a node website as an example to explain several common scenarios

Threads run persistently

Generally speaking, we start a server through node index.js on the window cmd. As long as it is not closed, we can always access and call the interface. However, on Linux, if you do not operate for a long time or you want to perform other operations, your node service will be disconnected and users will not be able to access your website. what to do? We can install the forever module to solve this problem.

npm install forever -g // Globally install the forever module

Just change the original startup method node index.js to forever start index.js. Here are some common commands

forever list // List all currently running services forever start -w index.js // Automatically restart on file change forever stopall // Stop all services forever stop app.js // Stop one of the node Apps 
forever stop [id] // forever list finds the corresponding id, then

Of course there are many more commands, you can refer to the relevant modules. Generally speaking, the simplest way to use it is:

forever start index.js

In this way, even if we switch to other Linux paths or exit, the node service still exists, that is, others can still access your website.

Configure nginx

nginx is a reverse proxy server developed by Russians and is now used by many companies around the world. For the introduction and installation of nginx, you can read the information yourself or learn quickly from the novice tutorial. Here my Linux has already installed nginx.

Next, I will use nginx to proxy http://localhost:8089 that was just started by forever start index.js, that is, to access the website http://localhost:8089 through my domain name. View the nginx configuration file path

find / -name nginx.conf 

Switch to it and modify the configuration inside

 server {
  listen 80; # Just configure the listening port to 80 server_name hellocode.xyz; # Enter the domain name and it will jump to http://localhost:8089
  include /etc/nginx/default.d/*.conf;
  location / {
   proxy_pass http://118.89.33.75:8089; # Your node website application}
  error_page 404 /404.html;
    location = /40x.html {
  }
  error_page 500 502 503 504 /50x.html;
    location = /50x.html {
  }
}

Exit to check whether the configuration is correct

nginx -t 

Reload nginx

nginx -s reload

Open the browser and enter the URL to access the website!

You may encounter cross-domain problems during the access process, so you need to set up the node side to support cross-domain. For the express framework, customize a middleware in index.js

var allowCors = function(req, res, next) {
 res.header('Access-Control-Allow-Origin', req.headers.origin);
 res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
 res.header('Access-Control-Allow-Headers', 'Content-Type');
 res.header('Access-Control-Allow-Credentials','true');
 next();
};
app.use(allowCors); //Use cross-domain middleware

Regarding cross-domain issues, the following blog will explain in detail!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Tutorial on configuring GZip compression when using Nginx as a reverse proxy for Node.js sites
  • Abandon Nginx and use nodejs as a reverse proxy server
  • Nginx+SSL+Node.js operating environment configuration tutorial
  • How to use Nginx as a reverse proxy for Node.js
  • Detailed steps to deploy https on Alibaba Cloud using nginx + node
  • Summary of how to set Nginx as a front-end server for Node.js

<<:  Detailed explanation of performance optimization ideas for React functional components

>>:  Pitfalls encountered when installing the decompressed version of MySQL 5.7.20 (recommended)

Recommend

Use CSS to achieve circular wave effect

I often see some circular wave graphics on mobile...

Three common uses of openlayers6 map overlay (popup window marker text)

Table of contents 1. Write in front 2. Overlay to...

MySQL 8.0 can now handle JSON

Table of contents 1. Brief Overview 2. JSON basic...

Mysql auto-increment primary key id is not processed in this way

Mysql auto-increment primary key id does not incr...

Docker uses dockerfile to start node.js application

Writing a Dockerfile Taking the directory automat...

Use of Linux relative and absolute paths

01. Overview Absolute paths and relative paths ar...

Detailed process of using vmware to test PXE batch installation server

Table of contents 1. Preparation 1. Prepare the e...

How to deploy HTTPS for free on Tencent Cloud

Recently, when I was writing a WeChat applet, the...

How to use echarts to visualize components in Vue

echarts component official website address: https...

Difference between HTML ReadOnly and Enabled

The TextBox with the ReadOnly attribute will be di...

Introduction to Royal Blue Color Matching for Web Design

Classical color combinations convey power and auth...

How to remotely connect to MySQL database with Navicat Premium

The party that creates a new connection is equiva...

jQuery implements the function of adding and deleting employee information

This article shares the specific code of jQuery t...

Summary of MySQL 8.0 Online DDL Quick Column Addition

Table of contents Problem Description Historical ...