NGINX 全面教程:使用 Node.js 和 Docker 的演示项目

引言

在现代 Web 开发中,高性能和可扩展性是衡量一个应用程序成功与否的重要指标。NGINX 作为一个高性能的 Web 服务器和反向代理服务器,被广泛应用于各种项目中。本篇博客将详细介绍 NGINX 的背景、核心功能,并通过一个实际的 Hands-On 演示,展示如何使用 Node.js 和 Docker 构建一个可扩展的应用程序。

目录

  1. NGINX 的诞生背景
  2. Hands-On 演示概述
  3. 构建简单的 Node.js 应用程序
  4. 使用 Docker 容器化应用程序
  5. 编写 NGINX 配置,实现负载均衡
  6. 生成 SSL 证书,配置 HTTPS
  7. 总结

1. NGINX 的诞生背景

在了解如何使用 NGINX 之前,先来了解一下它的诞生背景以及为什么它被创建。NGINX 诞生于对高性能、高并发性的需求。传统的 Web 服务器在处理大量并发连接时性能会下降,而 NGINX 采用了异步、事件驱动的架构,能够高效地处理数以万计的并发连接。

2. Hands-On 演示概述

在本次 Hands-On 演示中,我们将:

  • 构建一个简单的 Node.js 应用程序,它仅提供一个 index.html 文件。
  • 启动三个实例的该应用程序,每个实例都运行在 Docker 容器中。
  • 编写所有代码和配置,确保对每个步骤和每个配置项都有深入理解,而不是简单地复制粘贴代码。
  • 配置 NGINX 作为反向代理,实现对这三个应用程序实例的负载均衡。

3. 构建简单的 Node.js 应用程序

首先,我们需要创建一个简单的 Node.js 应用程序,它能够提供静态的 index.html 文件。

3.1 创建 index.html

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>示例网站</title>
</head>
<body>
<h1>欢迎来到示例网站</h1>
<p>这是一个使用 Node.js、NGINX 和 Docker 构建的示例应用程序。</p>
</body>
</html>

3.2 创建 server.js

1
2
3
4
5
6
7
8
9
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;

app.use(express.static(__dirname));

app.listen(port, () => {
console.log(`服务器正在监听端口 ${port}`);
});

3.3 创建 package.json

1
2
3
4
5
6
7
8
9
10
11
12
{
"name": "nginx-demo",
"version": "1.0.0",
"description": "NGINX 与 Node.js 演示项目",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.17.1"
}
}

4. 使用 Docker 容器化应用程序

为了能够轻松地管理和部署多个应用程序实例,我们将使用 Docker 对其进行容器化。

4.1 创建 Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
FROM node:14

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

4.2 创建 docker-compose.yml

为了同时运行多个应用程序实例,我们将使用 Docker Compose。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
version: '3'
services:
app1:
build: .
ports:
- "3001:3000"
environment:
- INSTANCE_NAME=app1
app2:
build: .
ports:
- "3002:3000"
environment:
- INSTANCE_NAME=app2
app3:
build: .
ports:
- "3003:3000"
environment:
- INSTANCE_NAME=app3

4.3 启动应用程序实例

在终端中运行以下命令,构建并启动所有容器:

1
docker-compose up --build

现在,我们有三个应用程序实例分别运行在端口 300130023003 上。

5. 编写 NGINX 配置,实现负载均衡

接下来,我们将配置 NGINX 作为反向代理服务器,为后端的 Node.js 应用程序实例提供负载均衡。

5.1 安装 NGINX

在本地计算机上安装 NGINX。以 Ubuntu 为例:

1
2
sudo apt update
sudo apt install nginx

5.2 编写 NGINX 配置文件

创建或修改 NGINX 的配置文件,一般位于 /etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 全局配置
worker_processes auto;

events {
worker_connections 1024;
}

http {
# 定义后端服务器组
upstream backend {
server localhost:3001;
server localhost:3002;
server localhost:3003;
}

server {
listen 80;
server_name localhost;

location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}

5.3 解释配置

  • worker_processes:设置 NGINX 工作进程数量,auto 表示自动根据 CPU 核心数配置。
  • worker_connections:每个进程允许的最大并发连接数。
  • upstream backend:定义后端服务器组,包含三个 Node.js 应用程序实例。
  • proxy_pass:将请求转发给后端服务器组。
  • proxy_set_header:设置请求头,传递客户端真实 IP 等信息。

5.4 测试和重启 NGINX

测试 NGINX 配置是否正确:

1
sudo nginx -t

重新加载 NGINX 配置:

1
sudo systemctl reload nginx

现在,访问 http://localhost,NGINX 将会在三个应用程序实例之间进行负载均衡。

6. 生成 SSL 证书,配置 HTTPS

为了提高安全性,我们将为 NGINX 配置 HTTPS,加密客户端和服务器之间的通信。

6.1 生成自签名 SSL 证书

在终端中运行以下命令生成自签名证书:

1
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout private.key -out certificate.crt

按照提示输入证书信息。

6.2 配置 NGINX 的 HTTPS

修改 NGINX 配置文件,添加 HTTPS 支持:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
http {
upstream backend {
server localhost:3001;
server localhost:3002;
server localhost:3003;
}

server {
listen 80;
server_name localhost;
return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
server_name localhost;

ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
}

/path/to/certificate.crt/path/to/private.key 替换为实际证书路径。

6.3 重启 NGINX

1
2
sudo nginx -t
sudo systemctl reload nginx

现在,访问 https://localhost,使用 HTTPS 连接到我们的应用程序。

7. 总结

通过本次实践,我们从零开始构建了一个使用 NGINX、Node.js 和 Docker 的演示项目。我们了解到:

  • NGINX 的高性能优势,以及为什么要将其作为反向代理服务器。
  • 如何使用 Docker 容器化应用程序,并同时运行多个实例以实现高可用性。
  • 如何编写 NGINX 配置文件,实现对后端应用程序的负载均衡和代理。
  • 如何生成 SSL 证书并配置 HTTPS,以确保客户端和服务器之间的通信安全。

掌握这些知识,有助于我们在实际项目中构建高性能、高可用性的 Web 服务架构。

参考资料


https://withesse.co/post/test/
Author
zt
Posted on
May 27, 2025
Licensed under