← 返回首页

Nginx 基础安装与目录结构说明

Nginx 是一款高性能的 HTTP 和反向代理服务器,常用于静态资源服务、反向代理、负载均衡和网关入口。它以事件驱动架构著称,在高并发场景下资源占用较低,是 Web 服务部署中非常常见的基础组件。

一、常见安装方式

在不同 Linux 发行版中,可以使用系统包管理器快速安装 Nginx。

Ubuntu / Debian

sudo apt update
sudo apt install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

CentOS / Rocky Linux / AlmaLinux

sudo dnf install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

安装完成后,可以通过以下命令查看服务状态:

systemctl status nginx
nginx -v
nginx -t

二、核心目录结构

使用系统包安装后,Nginx 的主要文件通常分布在以下位置:

三、主配置文件结构

Nginx 配置通常由多个上下文组成,例如 eventshttpserverlocation。一个简化示例如下:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen 80;
        server_name example.com;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }
    }
}

四、配置修改流程

每次修改配置后,建议按照“检查配置、平滑重载、观察日志”的流程处理:

sudo nginx -t
sudo systemctl reload nginx
sudo tail -f /var/log/nginx/error.log
生产环境中不要直接重启服务,优先使用 reload 平滑加载配置,减少对在线请求的影响。

五、小结

掌握安装方法、目录结构和配置检查流程,是后续学习反向代理、负载均衡和 HTTPS 配置的基础。建议在测试环境中多做配置实验,并养成变更前备份配置文件的习惯。