一、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 serverLoaded: 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