一、Nginx 核心功能全景图
二、核心功能详解
静态内容服务优化矩阵
配置指令 | 功能描述 | 最佳实践 | 性能影响 |
---|---|---|---|
root |
网站根目录 | 使用SSD存储 | 高 |
expires |
浏览器缓存 | 静态资源设置长期缓存 | 极高 |
gzip |
压缩传输 | 对文本类型启用压缩 | 高 |
sendfile |
零拷贝传输 | 启用sendfile on |
极高 |
tcp_nopush |
TCP优化 | 与sendfile搭配使用 | 中 |
open_file_cache |
文件描述符缓存 | open_file_cache max=1000 |
高 |
负载均衡策略对比
策略类型 | 配置指令 | 工作原理 | 适用场景 | 缺点 |
---|---|---|---|---|
轮询 | round-robin |
顺序分配请求 | 默认均衡策略 | 不考虑服务器负载 |
权重轮询 | weight=3 |
按权重比例分配 | 服务器性能不均 | 静态配置 |
IP哈希 | ip_hash |
相同IP固定后端 | 会话保持需求 | 扩展性差 |
最少连接 | least_conn |
选择连接最少 | 长连接服务 | 计算开销大 |
响应时间 | fair (第三方) |
最快响应优先 | 性能敏感服务 | 需额外模块 |
Nginx 功能概述
Nginx 是高性能的 Web 服务器和反向代理服务器,主要功能包括:
- 静态内容服务:高效处理 HTML、CSS、JS 等静态文件
- 反向代理:将客户端请求转发到后端应用服务器
- 负载均衡:在多台服务器间分配请求流量
- SSL/TLS 终止:处理 HTTPS 加密连接
- 访问控制:基于 IP、用户认证等机制限制访问
功能作用详解
1. Nginx 核心功能
静态内容服务
server {
root /var/www/html; # 网站根目录
index index.html; # 默认首页
}
- 支持高效静态文件处理
- 支持 gzip 压缩减少传输量
- 支持浏览器缓存控制
反向代理配置
location /app {
proxy_pass http://backend_server;
proxy_set_header Host $host;
}
- 隐藏后端服务器信息
- 支持负载均衡到多个后端
- 支持 WebSocket 代理
2. 访问控制
基础认证
auth_basic "Restricted";
auth_basic_user_file /path/to/htpasswd;
- 保护敏感目录
- 支持多用户管理
- 密码加密存储(bcrypt)
IP 访问控制
location /admin {
allow 192.168.1.0/24;
deny all;
}
- 按网络范围限制访问
- 支持动态 IP 黑白名单
- 可与认证结合使用
3. HTTPS 安全配置
证书管理
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
- 支持 RSA/ECC 证书
- 支持 OCSP Stapling
- 支持证书链配置
安全增强
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
- 禁用不安全协议(SSLv3, TLSv1.0)
- 启用前向保密(Perfect Forward Secrecy)
- 启用 HSTS 强制 HTTPS
4. 性能优化
缓存配置
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;
location / {
proxy_cache my_cache;
proxy_cache_valid 200 1h;
}
- 减少后端服务器负载
- 加速静态内容访问
- 支持缓存清理接口
连接优化
keepalive_timeout 65;
keepalive_requests 100;
gzip on;
gzip_types text/plain application/json;
- 减少 TCP 连接开销
- 压缩传输内容
- 支持 HTTP/2 协议
5. 日志与监控
分析需求 | awk命令 | 功能描述 | 输出示例 |
---|---|---|---|
状态码统计 | awk '{print $9}' |
sort | uniq -c |
流量IP排名 | awk '{print $1}' |
sort | uniq -c |
请求URL统计 | `awk ‘{print $7}’ | sort | uniq -c |
响应时间分析 | awk 'NF > 1 {print 7, $NF}' |
sort -k2nr | 慢请求分析 |
流量时间分布 | awk '{print $4}' |
cut -d: -f1 | uniq -c |
访问日志
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent';
access_log /var/log/nginx/access.log main;
- 自定义日志格式
- 支持 JSON 格式日志
- 实时流量监控
命令总结表格
演示命令 | 功能描述 | 关键参数 |
---|---|---|
yum install nginx |
安装 Nginx | 无 |
systemctl start nginx |
启动 Nginx 服务 | 无 |
nginx -t |
测试配置文件语法 | 无 |
nginx -s reload |
重载配置文件 | 无 |
htpasswd -c /path/to/file user |
创建密码文件 | -c 创建新文件 |
htpasswd /path/to/file user |
添加用户 | 无 |
openssl req -x509 -newkey rsa:2048 |
生成自签名证书 | -x509 X.509证书格式 |
curl -u user:password URL |
带认证访问 | -u 用户名密码 |
curl -k https://URL |
忽略证书验证 | -k 不验证证书 |
一、Nginx 安装与基本配置
1. 安装 Nginx
# 添加EPEL仓库
[root@localhost ~]# yum install epel-release -y
# 安装Nginx
[root@localhost ~]# yum install nginx -y
# 启动并设置开机自启
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service
2. 验证安装
# 检查服务状态
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2025-07-21 15:30:05 CST; 5s ago
# 测试访问
[root@localhost ~]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.1
3. 基本配置
# 创建测试站点目录
[root@localhost ~]# mkdir -p /var/www/site1
[root@localhost ~]# echo "Site 1 Home" > /var/www/site1/index.html
# 创建站点配置文件
[root@localhost ~]# vi /etc/nginx/conf.d/site1.conf
server {
listen 80;
server_name site1.localhost;
root /var/www/site1;
index index.html;
}
# 重载配置
[root@localhost ~]# nginx -t && systemctl reload nginx
nginx: configuration file /etc/nginx/nginx.conf test is successful
二、用户认证配置
1. 创建密码文件
# 安装密码工具
[root@localhost ~]# yum install httpd-tools -y
# 创建认证用户
[root@localhost ~]# htpasswd -c /etc/nginx/auth_users admin
New password:
Re-type new password:
Adding password for user admin
# 添加第二个用户
[root@localhost ~]# htpasswd /etc/nginx/auth_users user
2. 配置认证
# 编辑站点配置
[root@localhost ~]# vi /etc/nginx/conf.d/site1.conf
server {
listen 80;
server_name site1.localhost;
root /var/www/site1;
index index.html;
location /private {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/auth_users;
}
}
# 重载配置
[root@localhost ~]# nginx -s reload
3. 测试访问
# 尝试访问受保护区域
[root@localhost ~]# curl http://site1.localhost/private
HTTP/1.1 401 Unauthorized
Server: nginx/1.20.1
WWW-Authenticate: Basic realm="Restricted Area"
# 使用认证访问
[root@localhost ~]# curl -u admin:password http://site1.localhost/private
Site 1 Private Area
三、HTTPS 配置
1. 生成自签名证书
# 创建证书目录
[root@localhost ~]# mkdir /etc/nginx/ssl
[root@localhost ~]# cd /etc/nginx/ssl
# 生成私钥和证书
[root@localhost ssl]# openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout site1.key -out site1.crt \
-subj "/C=CN/ST=Beijing/L=Beijing/O=Localhost/CN=site1.localhost"
2. 配置 HTTPS 站点
# 编辑配置文件
[root@localhost ~]# vi /etc/nginx/conf.d/site1.conf
server {
listen 443 ssl;
server_name site1.localhost;
root /var/www/site1;
index index.html;
ssl_certificate /etc/nginx/ssl/site1.crt;
ssl_certificate_key /etc/nginx/ssl/site1.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
}
# HTTP重定向到HTTPS
server {
listen 80;
server_name site1.localhost;
return 301 https://$host$request_uri;
}
# 重载配置
[root@localhost ~]# nginx -s reload
3. 测试 HTTPS
# 跳过证书验证测试
[root@localhost ~]# curl -k https://site1.localhost
Site 1 Home
# 查看证书详情
[root@localhost ~]# openssl s_client -connect site1.localhost:443 -showcerts