Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of a method for Forever+nginx to deploy a Node site

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Recently, I have acquired the cheapest Tencent Cloud server, which is mainly used to deploy my personal blog and learn some liunx knowledge, so as to master web technology in all aspects. It is strongly recommended that front-end staff have a server of their own. Before writing this article, my CVM has already installed Mysql, node, nginx and so on. Here are some common scenarios for deploying a node website

Thread running persistently

Generally speaking, we start a server on window cmd through node index.js, and we can access and invoke the interface all the time as long as we don't shut it down. But on linux, if you don't operate for a long time or you have to perform other operations, your node service will be disconnected and users will not be able to access your site. What shall I do? We can install forever module to solve the problem.

Npm install forever-g / / globally install the forever module

From the original startup mode node index.js to forever start index.js, here are a few common commands

Forever list / / list all currently running services forever start-w index.js / / automatically restart forever stopall / / stop all services forever stop app.js / / stop one of the node App forever stop [id] / / forever list find the corresponding id, and then

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

Forever start index.js

In this way, even if we switch to another linux path or exit, the node service still exists, that is, others can still visit your site.

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 consult the materials yourself or go to the rookie tutorial to learn it quickly. Here my linux has been installed with nginx.

Now I'm going to use nginx to represent the http://localhost:8089 just started by forever start index.js, that is, to access http://localhost:8089 through my domain name. View nginx profile path

Find /-name nginx.conf

Switch to the inside and modify the configuration.

Server {listen 80; # listen port 80 can be configured directly server_name hellocode.xyz; # input domain name 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.htl; location = / 40x.html {} error_page 500502503504 / 50x.html Location = / 50x.html {}}

Exit check to see if the configuration is correct

Nginx-t

Reload nginx

Nginx-s reload

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

Cross-domain problems may be encountered in the process of access, so the node side needs to be set to support cross-domain. In the case of the express framework, a custom middleware is defined 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

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

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report