Nginx 入门指南

Nginx 入门指南

ℹ️高性能 HTTP 和反向代理服务器学习指南

🎯 学习目标

  • 理解 Nginx 的核心概念和特点
  • 掌握 Nginx 的基本安装和配置
  • 了解 Nginx 的应用场景和优势

📋 Nginx 简介

🔍 什么是 Nginx?

Nginx(发音同 “engine X”)是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个 BSD-like 协议下发行。

🏗️ 核心特点

特性 说明
高性能 可以扛住 5W 左右的并发连接
低内存占用 相比 Apache 占用更少系统资源
高稳定性 支持热部署,配置变更无需重启
反向代理 强大的负载均衡和反向代理功能
静态资源服务 高效处理静态文件(图片、CSS、JS)

📊 应用场景



graph TD
    A[Nginx] --> B[Web服务器]
    A --> C[反向代理]
    A --> D[负载均衡]
    A --> E[静态资源服务]
    A --> F[API网关]

    B --> G[个人网站]
    B --> H[企业官网]

    C --> I[后端服务代理]
    C --> J[微服务架构]

    D --> K[高并发系统]
    D --> L[高可用架构]


🚀 快速开始

安装 Nginx

Ubuntu/Debian

1
2
sudo apt update
sudo apt install nginx

CentOS/RHEL

1
2
sudo yum install epel-release
sudo yum install nginx

Docker

1
docker run -d -p 80:80 --name my-nginx nginx

基础命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 启动 Nginx
sudo systemctl start nginx

# 停止 Nginx
sudo systemctl stop nginx

# 重启 Nginx
sudo systemctl restart nginx

# 重新加载配置(不中断服务)
sudo systemctl reload nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查配置文件语法
sudo nginx -t

# 查看 Nginx 状态
sudo systemctl status nginx

📁 配置文件结构

主要配置文件

1
2
3
4
5
6
/etc/nginx/
├── nginx.conf # 主配置文件
├── conf.d/ # 额外配置目录
├── sites-available/ # 可用站点配置
├── sites-enabled/ # 已启用站点配置
└── snippets/ # 配置片段

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
27
28
29
30
31
32
33
34
# 主进程配置
user nginx;
worker_processes auto; # 工作进程数,auto 自动匹配 CPU 核心数
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# 事件模块
events {
worker_connections 1024; # 每个工作进程的最大连接数
}

# HTTP 模块
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

# 日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

# 性能优化
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

# 包含其他配置
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

🎯 核心概念

1. 反向代理

[tip] 正向代理 vs 反向代理

  • 正向代理:客户端通过代理服务器访问互联网
  • 反向代理:客户端通过代理服务器访问后端服务
1
2
3
4
5
6
7
8
9
10
server {
listen 80;
server_name example.com;

location / {
proxy_pass http://localhost:3000; # 代理到本地 Node.js 服务
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

2. 虚拟主机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name website1.com; # 第一个网站

root /var/www/website1;
index index.html;
}

server {
listen 80;
server_name website2.com; # 第二个网站

root /var/www/website2;
index index.html;
}

3. Location 匹配规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 精确匹配
location = / {
# 只有访问 / 时才匹配
}

# 前缀匹配(优先级低于精确匹配)
location /api {
# 匹配 /api 开头的所有请求
}

# 正则匹配(区分大小写)
location ~ \.php$ {
# 匹配所有 .php 文件
}

# 正则匹配(不区分大小写)
location ~* \.(jpg|jpeg|png|gif)$ {
# 匹配所有图片文件
}

# 优先级:精确匹配 > 正则匹配 > 前缀匹配

📝 常用配置示例

静态网站托管

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 80;
server_name mysite.com www.mysite.com;
root /var/www/mysite;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}

# 图片缓存配置
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
add_header Cache-Control "public, immutable";
}
}

错误页面自定义

1
2
3
4
5
6
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root /usr/share/nginx/html;
}

🔧 配置测试

1. 检查配置语法

1
sudo nginx -t

2. 查看 Nginx 版本

1
nginx -v

3. 测试配置重载

1
sudo nginx -s reload

📚 学习路径

  1. 入门基础(当前阶段)

    • 了解 Nginx 基本概念
    • 掌握安装和基础配置
    • 熟悉配置文件结构
  2. 📖 反向代理与负载均衡

  3. 性能优化与安全配置

  4. 🎯 实战配置


🔗 相关资源

官方资源

学习资源


💡 实践建议

[success] 学习建议

  1. 先在本地环境安装和测试
  2. 从简单的静态网站开始练习
  3. 逐步学习反向代理和负载均衡
  4. 关注性能优化和安全配置
  5. 多做实验,积累实战经验

下一步:继续学习 02_Nginx反向代理与负载均衡_实战教程