参考资料

  1. Nginx编译安装详细说明以及案例
  2. nginx是否需要开启HTTP/2支持以提高安全性?
  3. Nginx作为反向代理的功能
  4. Nginx如何支持动态内容?
  5. SCGI(Simple Common Gateway Interface,简单通用网关接口)详细说明以及案例
  6. WSGI(Web Server Gateway Interface,Web 服务网关接口)详细说明以及案例
  7. nginx 查看配置文件
  8. Nginx如何限制连接数以防止DDoS?

Nginx配置与管理详解

基本配置结构

# 主配置文件通常位于 /etc/nginx/nginx.conf

user nginx;  # 运行Nginx的用户
worker_processes auto;  # 工作进程数,auto表示自动设置为CPU核心数

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

http {
    include /etc/nginx/mime.types;  # 包含MIME类型定义
    default_type application/octet-stream;  # 默认MIME类型
    
    # 日志格式定义
    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;  # 访问日志
    error_log /var/log/nginx/error.log warn;   # 错误日志级别
    
    sendfile on;  # 启用高效文件传输模式
    tcp_nopush on;  # 仅在sendfile开启时有效
    
    keepalive_timeout 65;  # 保持连接超时时间
    
    gzip on;  # 开启Gzip压缩
    
    # 包含其他配置文件
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

虚拟主机配置示例

server {
    listen 80;  # 监听端口
    server_name example.com www.example.com;  # 域名
    
    root /var/www/example.com;  # 网站根目录
    index index.html index.htm;  # 默认索引文件
    
    location / {
        try_files $uri $uri/ =404;  # 尝试查找文件或目录,否则返回404
    }
    
    # 静态文件缓存设置
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        access_log off;
    }
    
    # 错误页面配置
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

HTTPS配置示例

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    
    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    
    # SSL优化配置
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    
    # 其他配置...
}

反向代理配置示例

server {
    listen 80;
    server_name app.example.com;
    
    location / {
        proxy_pass http://localhost:3000;  # 转发到本地3000端口
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

负载均衡配置示例

upstream backend {
    server backend1.example.com weight=5;
    server backend2.example.com;
    server backend3.example.com backup;  # 备用服务器
    
    # 负载均衡方法
    # least_conn;  # 最少连接
    # ip_hash;     # IP哈希
}

server {
    listen 80;
    server_name load.example.com;
    
    location / {
        proxy_pass http://backend;
        # 其他代理设置...
    }
}

常用管理命令

  1. 测试配置文件语法:

    nginx -t
  2. 重新加载配置(不中断服务):

    nginx -s reload
  3. 停止服务:

    nginx -s stop
  4. 优雅停止(完成当前请求):

    nginx -s quit
  5. 查看版本信息:

    nginx -v
  6. 查看编译参数:

    nginx -V