王培顺的博客&WangPeishun’s Blog

以下是一份详细的Nginx使用手册大纲,内容涵盖基础配置、高级功能及常见场景,供学习参考!


Nginx 使用手册

目录

  1. Nginx 简介
  2. 安装与卸载
  3. 基础配置与命令
  4. 核心配置详解
  5. 虚拟主机配置
  6. 反向代理与负载均衡
  7. SSL/TLS 配置
  8. 安全加固
  9. 高级配置(缓存、Gzip、WebSocket)
  10. 日志管理
  11. 常见问题排查
  12. 附录:常用模块与资源

1. Nginx 简介

  • 什么是Nginx
    轻量级、高性能的Web服务器/反向代理服务器,支持高并发、低内存消耗。
  • 核心特性

    • 处理静态资源
    • 反向代理与负载均衡
    • HTTP/HTTPS、HTTP/2支持
    • 模块化架构(如ngx_http_rewrite_module

2. 安装与卸载

2.1 安装

  • Ubuntu/Debian

    sudo apt update
    sudo apt install nginx
  • CentOS/RHEL

    sudo yum install epel-release
    sudo yum install nginx
  • macOS (Homebrew)

    brew install nginx

2.2 卸载

  • Ubuntu/Debian

    sudo apt purge nginx nginx-common
  • CentOS/RHEL

    sudo yum remove nginx

3. 基础配置与命令

3.1 配置文件结构

  • 主配置文件
    /etc/nginx/nginx.conf
  • 配置块类型

    • main:全局配置(进程数、用户等)
    • events:连接处理参数
    • http:HTTP服务配置
    • server:虚拟主机配置
    • location:URI路由规则

3.2 常用命令

# 启动
sudo systemctl start nginx

# 停止
sudo systemctl stop nginx

# 重启
sudo systemctl restart nginx

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

# 检查配置语法
sudo nginx -t

4. 核心配置详解

4.1 main 块配置

user nginx;          # 运行用户
worker_processes auto; # 工作进程数(建议设为CPU核心数)
error_log /var/log/nginx/error.log warn; # 错误日志路径

4.2 events 块配置

events {
  worker_connections 1024;  # 单个进程最大连接数
  use epoll;                 # 高性能事件模型(Linux)
}

4.3 http 块配置

http {
  include /etc/nginx/mime.types;  # 文件类型映射
  default_type application/octet-stream;
  access_log /var/log/nginx/access.log;  # 访问日志
  sendfile on;                   # 零拷贝传输
  keepalive_timeout 65;          # 长连接超时时间
  gzip on;                       # 启用Gzip压缩
  include /etc/nginx/conf.d/*.conf; # 加载子配置
}

5. 虚拟主机配置

5.1 单域名配置

server {
  listen 80;
  server_name example.com;
  root /var/www/html;      # 网站根目录
  index index.html;

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

5.2 多域名配置

# 域名1
server {
  listen 80;
  server_name site1.com;
  root /var/www/site1;
}

# 域名2
server {
  listen 80;
  server_name site2.com;
  root /var/www/site2;
}

5.3 Location 匹配规则

  • 前缀匹配

    location /images/ { ... }
  • 正则匹配

    location ~ \.(jpg|png)$ { ... }
  • 精确匹配

    location = /login { ... }

6. 反向代理与负载均衡

6.1 反向代理配置

server {
  listen 80;
  server_name proxy.example.com;

  location / {
    proxy_pass http://backend_server; # 后端服务地址
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

upstream backend_server {
  server 192.168.1.100:8080;
  server 192.168.1.101:8080;
}

6.2 负载均衡策略

  • 轮询(默认)

    upstream backend {
      server srv1.example.com;
      server srv2.example.com;
    }
  • 加权轮询

    upstream backend {
      server srv1 weight=3;
      server srv2 weight=2;
    }
  • IP哈希

    upstream backend {
      ip_hash;
      server srv1;
      server srv2;
    }

7. SSL/TLS 配置

7.1 生成证书

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /etc/nginx/ssl/example.key \
  -out /etc/nginx/ssl/example.crt

7.2 HTTPS 配置

server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /etc/nginx/ssl/example.crt;
  ssl_certificate_key /etc/nginx/ssl/example.key;

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

  # 强制跳转HTTPS
  if ($scheme = http) {
    return 301 https://$host$request_uri;
  }
}

8. 安全加固

  • 隐藏版本号

    server_tokens off;
  • 限制访问

    location /admin {
      allow 192.168.1.0/24;
      deny all;
    }
  • 防止DDoS

    limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
    location / {
      limit_req zone=one burst=20;
    }

9. 高级配置

9.1 缓存配置

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m;

server {
  location / {
    proxy_cache my_cache;
    proxy_cache_valid 200 302 10m;
  }
}

9.2 Gzip 压缩

gzip_types text/plain text/css application/json;
gzip_min_length 1000;
gzip_proxied any;

9.3 WebSocket 支持

location /ws/ {
  proxy_pass http://backend;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

10. 日志管理

  • 访问日志格式

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  • 日志分割(使用logrotate
    创建 /etc/logrotate.d/nginx 配置定期归档。

11. 常见问题排查

  • 检查配置语法

    nginx -t
  • 查看错误日志

    tail -f /var/log/nginx/error.log
  • 验证端口监听

    netstat -tulpn | grep nginx

12. 附录

  • 官方文档
    Nginx Documentation
  • 常用模块列表

    • ngx_http_ssl_module
    • ngx_http_rewrite_module
    • ngx_http_proxy_module

本手册持续更新,建议结合实践操作加深理解。遇到复杂场景时,可参考官方文档或社区资源。

标签: none

添加新评论