Linux installs NodeJs and works with Nginx to implement a reverse proxy
- 2020-05-13 04:19:48
- OfStack
This paper introduces Linux installation of NodeJs and realization of reverse proxy with Nginx, as follows:
What is NodeJs?
Node.js is an JavaScript running environment (runtime). It actually encapsulates the Google V8 engine. V8 engine performs Javascript very quickly and with excellent performance.
Node.js optimizes some special use cases to provide an alternative API that makes V8 work better in a non-browser environment.
Local installation (OS X)
Version selection
V 4.4.4, long term support, mature and reliable V 6.2.0 stable version, latest featuresI still prefer to use the latest version here
Download installation package
https://nodejs.org/dist/v6.2.0/node-v6.2.0.pkg
Double-click the install install package
Step 1, step 1, and the installation is complete.
Simple to perform
node -v
v6.2.0
Local operation (OS X)
Create the demo file
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Write to the file example.js
Executable files
node example.js
The command line prints Server running at http://127.0.0.1:3000/
At the same time in the browser input http: / / 127.0.0.1:3000 /, page output Hello World
Close the terminal, the page is no longer available.
Express framework
We use the Express framework to build the website project demo.
npm install express
node_modules1
Create the demo.js file
var express = require('express');
app = express();
app.use(express.static(__dirname + '/public'));
app.listen(8081)
Create the public folder in the peer folder and put the static file 1.jpg inside
In the browser input http: / / 127.0.0.1:8081/1. jpg
See Response Headers X - Powered - By: Express
Server installation (CentOS 7)
Install node
curl --silent --location https://rpm.nodesource.com/setup | bash -
yum -y install nodejs
yum install npm
About the version of Node
The version number I installed through node in the last step is v0.10.42. At first, I thought it was wrong. After looking up the information, I found that four versions of node have been maintained so far
v0.10.42 (LTS) v0.12.10 (LTS) 4.4.5 LTS 6.2Oh, what a mess.
Write an demo instance
This part of the process is as follows.
Install forever and run it
npm install forever -g
forever start app.js
Configuration Nginx
cd /usr/local/nginx/conf/vhost/
vi demonode.coderfix.cn.conf
server {
listen 80;
server_name demonode.coderfix.cn;
location / {
proxy_pass http://127.0.0.1:8899;
}
}
Nginx resolves the domain name and forwards it to port 8899 ~ of the local nodejs
Configure domain name resolution and access
http://demonode.coderfix.cn/
This completes the deployment of nodejs and nginx.
Possible problems
The Nodejs service overruns, resulting in an error
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE
at errnoException (net.js:884:11)
at Server._listen2 (net.js:1022:14)
at listen (net.js:1044:10)
at Server.listen (net.js:1110:5)
at Object.<anonymous> (folderName/app.js:33:24)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)1
Turn off the process you started and start again.
ps aux | grep node
kill -9 ****