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

Mysql master/slave database synchronization configuration and common errors

As the number of visits increases, for some time-...

User experience analysis of facebook dating website design

<br />Related article: Analysis of Facebook&...

Detailed steps to change the default password when installing MySQL in Ubuntu

Step 1: Enter the directory: cd /etc/mysql, view ...

MySQL 5.7 Common Data Types

——Notes from "MySQL in Simple Terms (Second ...

A brief introduction to web2.0 products and functions

<br />What is web2.0? Web2.0 includes those ...

Detailed explanation of MySQL information_schema database

1. Overview The information_schema database is th...

How to add, delete and modify columns in MySQL database

This article uses an example to describe how to a...

Implementation of Node connection to MySQL query transaction processing

Table of contents Enter the topic mysql add, dele...

Teach you how to use MySQL8 recursive method

I have previously written an article about recurs...

Object.entries usage you don't know in JavaScript

Table of contents Preface 1. Use for...of to iter...

JavaScript prototype and prototype chain details

Table of contents 1. prototype (explicit prototyp...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...