参考资料

  1. 如何启用Nginx上游连接复用?
  2. Nginx配置文件详细说明以及案例
  3. Nginx基本介绍
  4. Nginxtry_files 文件判断指令详细说明以及案例
  5. 如何配置Nginx用户认证?
  6. nginx反向代理配置详解
  7. nginx 配置方式
  8. Nginx在Web开发中的应用

nginx 配置方式

  1. 主配置文件结构

全局块
events {
    事件模块配置
}
http {
    HTTP模块配置
    server {
        虚拟主机配置
        location / {
            路由规则
        }
    }
}
  1. 常用配置项

  • 全局块:worker_processes, error_log

  • events:worker_connections

  • http:include mime.types, sendfile on, keepalive_timeout

  • server:listen, server_name, access_log, error_log

  • location:root, alias, proxy_pass, index

  1. 示例配置

server {
    listen 80;
    server_name example.com;
    root /var/www/html;
    
    location /static/ {
        alias /data/static/;
        expires 30d;
    }
    
    location /api/ {
        proxy_pass http://localhost:3000/;
        proxy_set_header Host $host;
    }
}
  1. 检查与重载
    nginx -t 测试配置
    nginx -s reload 热重载配置