# Nginx 教程：目录结构与配置文件详解

URL: https://caijiao.org/nginx/config-structure
Source: docs/nginx/config-structure.md
Description: 本节 Nginx 教程详细介绍 nginx 目录结构与配置文件组成，帮助你掌握 nginx.conf、sites-enabled、logs 等常见路径及配置含义，快速理解 Nginx 文档。

在安装 Nginx 后，理解其目录结构与配置文件非常重要。Nginx 的灵活性与性能很大程度上来源于其模块化配置设计。

## 一、Nginx 目录结构

以下以 Linux 系统为例说明常见的 Nginx 目录结构：

```bash
/etc/nginx/
├── nginx.conf          # 主配置文件
├── conf.d/             # 默认加载的附加配置目录
├── sites-available/    # 可用站点配置
├── sites-enabled/      # 启用站点配置
├── modules/            # 模块配置目录
├── logs/               # 访问与错误日志
└── html/               # 默认静态页面目录
````

### 关键目录说明

* **nginx.conf**：主配置文件，定义全局参数与包含子配置。
* **conf.d/**：存放虚拟主机或模块配置。
* **logs/**：记录 `access.log` 与 `error.log`。
* **html/**：默认站点文件目录。

## 二、nginx.conf 配置文件结构

主配置文件通常包含以下三个主要部分：

```nginx
# 全局块
user  nginx;
worker_processes  auto;

# events 块
events {
    worker_connections  1024;
}

# http 块
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;
}
```

## 三、常见配置文件位置（按系统）

| 系统                   | 配置路径                              | 服务管理命令                        |
| -------------------- | --------------------------------- | ----------------------------- |
| CentOS / Rocky Linux | `/etc/nginx/nginx.conf`           | `systemctl restart nginx`     |
| Ubuntu / Debian      | `/etc/nginx/nginx.conf`           | `service nginx reload`        |
| macOS (brew)         | `/usr/local/etc/nginx/nginx.conf` | `brew services restart nginx` |
| Windows              | `conf/nginx.conf`                 | `nginx.exe -s reload`         |

---

## 四、快速验证配置文件

修改配置后，应使用以下命令验证：

```bash
nginx -t
```

若输出 `syntax is ok` 和 `test is successful`，则表示配置文件语法正确。
