Skip to content

Nginx目录结构

了解Nginx的目录结构有助于更好地管理和配置Nginx。

标准目录结构

Nginx安装后的标准目录结构如下:

/usr/local/nginx/              # Nginx安装根目录
├── conf/                     # 配置文件目录
│   ├── nginx.conf           # 主配置文件
│   ├── mime.types           # MIME类型定义
│   └── fastcgi_params       # FastCGI参数
├── html/                     # 默认网站根目录
│   ├── 50x.html             # 错误页面
│   └── index.html           # 默认首页
├── logs/                     # 日志文件目录
│   ├── access.log           # 访问日志
│   └── error.log            # 错误日志
├── sbin/                     # 可执行文件目录
│   └── nginx                # Nginx可执行文件
└── temp/                     # 临时文件目录
    ├── client_body_temp     # 客户端请求体临时目录
    ├── proxy_temp           # 代理临时目录
    ├── fastcgi_temp         # FastCGI临时目录
    ├── uwsgi_temp           # uWSGI临时目录
    └── scgi_temp            # SCGI临时目录

包管理器安装的目录结构

使用包管理器安装的Nginx,目录结构可能略有不同:

Ubuntu/Debian

/etc/nginx/                  # 配置文件目录
├── nginx.conf              # 主配置文件
├── conf.d/                 # 额外配置目录
├── sites-available/        # 可用站点配置
├── sites-enabled/          # 启用的站点配置
└── snippets/               # 配置片段

/var/log/nginx/             # 日志文件目录
├── access.log             # 访问日志
└── error.log              # 错误日志

/var/www/html/             # 默认网站根目录

/usr/sbin/nginx            # Nginx可执行文件

CentOS/RHEL

/etc/nginx/                  # 配置文件目录
├── nginx.conf              # 主配置文件
├── conf.d/                 # 额外配置目录
└── default.d/              # 默认配置

/var/log/nginx/             # 日志文件目录
├── access.log             # 访问日志
└── error.log              # 错误日志

/usr/share/nginx/html/      # 默认网站根目录

/usr/sbin/nginx            # Nginx可执行文件

重要目录详解

配置文件目录 (conf/ 或 /etc/nginx/)

主要配置文件:

  • nginx.conf:主配置文件,包含全局配置
  • mime.types:定义文件扩展名与MIME类型的映射
  • fastcgi_params:FastCGI参数配置
  • scgi_params:SCGI参数配置
  • uwsgi_params:uWSGI参数配置

额外配置目录:

  • conf.d/:存放额外的配置文件
  • sites-available/:存放所有站点配置(Debian/Ubuntu)
  • sites-enabled/:存放启用的站点配置(Debian/Ubuntu)

网站根目录 (html/ 或 /var/www/html/)

默认网站文件:

  • index.html:默认首页
  • 50x.html:服务器错误页面
  • 404.html:未找到页面(需要自己创建)

自定义网站: 通常在网站根目录下为每个站点创建独立目录。

日志文件目录 (logs/ 或 /var/log/nginx/)

主要日志文件:

  • access.log:记录所有访问请求
  • error.log:记录错误信息

日志轮转: 可以使用logrotate工具进行日志轮转。

可执行文件目录 (sbin/ 或 /usr/sbin/)

主要文件:

  • nginx:Nginx主程序

临时文件目录 (temp/ 或 /var/lib/nginx/)

主要目录:

  • client_body_temp:客户端请求体临时文件
  • proxy_temp:代理临时文件
  • fastcgi_temp:FastCGI临时文件
  • uwsgi_temp:uWSGI临时文件
  • scgi_temp:SCGI临时文件

自定义目录结构

推荐的生产环境目录结构

/data/nginx/
├── conf/                    # 配置文件
│   ├── nginx.conf         # 主配置
│   ├── conf.d/            # 额外配置
│   ├── sites-available/   # 可用站点
│   └── sites-enabled/     # 启用站点
├── www/                     # 网站根目录
│   ├── site1/             # 站点1
│   ├── site2/             # 站点2
│   └── static/            # 静态资源
├── logs/                    # 日志文件
│   ├── access/            # 访问日志
│   └── error/             # 错误日志
├── ssl/                     # SSL证书
│   ├── site1.crt
│   └── site1.key
├── cache/                   # 缓存目录
│   ├── proxy_cache/
│   └── fastcgi_cache/
└── temp/                    # 临时文件
    ├── client_body_temp/
    ├── proxy_temp/
    └── fastcgi_temp/

配置文件路径引用

在配置文件中引用路径时,可以使用相对路径或绝对路径。

相对路径

相对于Nginx安装根目录:

nginx
root html;  # 相对于安装根目录

绝对路径

使用完整路径:

nginx
root /data/nginx/www/site1;

权限设置

目录权限

bash
# 配置文件目录
chmod 755 /etc/nginx

# 网站根目录
chmod 755 /var/www/html

# 日志目录
chmod 755 /var/log/nginx

文件权限

bash
# 配置文件
chmod 644 /etc/nginx/nginx.conf

# 日志文件
chmod 644 /var/log/nginx/*.log

运行用户

确保Nginx运行用户对相关目录有读写权限:

nginx
user nginx;  # 或 www-data

常用路径命令

查找Nginx安装路径

bash
which nginx
whereis nginx

查找配置文件

bash
nginx -t

查看默认配置

bash
cat /etc/nginx/nginx.conf

查找网站根目录

bash
grep -r "root" /etc/nginx/

总结

Nginx目录结构的关键点:

  • 配置文件:通常在 /etc/nginx/conf/
  • 网站根目录:通常在 /var/www/html/html/
  • 日志文件:通常在 /var/log/nginx/logs/
  • 可执行文件:通常在 /usr/sbin/nginxsbin/nginx
  • 临时文件:通常在 /var/lib/nginx/temp/

了解Nginx的目录结构,有助于更好地管理和维护Nginx服务器。