简介
Nginx 是 Linux 服务端必备高性能 Web 服务器、反向代理、负载均衡中间件,部署项目、域名转发、静态资源托管、跨域配置基本都离不开它。
本文从零开始:Linux 安装 Nginx → 常用命令 → 目录结构 → nginx.conf 逐行详解 → 反向代理/静态站点配置实战,全程干货无废话,可直接收藏当手册、发掘金专栏、面试复习。
适合人群 :后端开发者、服务器运维、初学 Nginx、项目部署 环境:CentOS 7 / CentOS 8 / Ubuntu 通用
一、Linux 安装 Nginx
1. CentOS yum 安装
bash
体验AI代码助手
代码解读
复制代码
# 更新源 yum update -y # 安装nginx yum install nginx -y
2. Ubuntu apt 安装
bash
体验AI代码助手
代码解读
复制代码
apt update apt install nginx -y
3. 源码安装(自定义版本、生产常用)
源码安装可自定义 Nginx 版本、自定义编译模块,是生产环境主流安装方式。
3.1 安装依赖、下载编译安装
bash
体验AI代码助手
代码解读
复制代码
# 安装依赖 yum install gcc pcre pcre-devel zlib zlib-devel -y # 下载解压编译安装(自行换版本号) wget http://nginx.org/download/nginx-1.24.0.tar.gz tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0 # --prefix=/usr/local/nginx 指定安装目录 # --with-http_ssl_module参数是启用 Nginx 对 HTTPS 协议的支持,即加载 `ngx_http_ssl_module` 模块 ./configure --prefix=/usr/local/nginx --with-http_ssl_module make && make install
3.2 手动配置 systemd 系统服务,实现开机自启、服务托管、平滑重启等功能
3.2.1 创建 nginx.service 服务文件
bash
体验AI代码助手
代码解读
复制代码
vim /usr/lib/systemd/system/nginx.service
3.2.2 写入完整服务配置(直接复制即用)
ini
体验AI代码助手
代码解读
复制代码
[Unit] Description=Nginx Web Server After=network.target [Service] Type=forking # 源码安装nginx默认路径,无需修改 ExecStart=/usr/local/nginx/sbin/nginx ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecReload=/usr/local/nginx/sbin/nginx -s reload PrivateTmp=true [Install] WantedBy=multi-user.target
3.2.3 生效服务并配置自启
bash
体验AI代码助手
代码解读
复制代码
# 重载系统服务配置 systemctl daemon-reload # 启动nginx systemctl start nginx # 设置开机自启 systemctl enable nginx # 查看运行状态 systemctl status nginx
配置完成后,源码安装的 Nginx 可完全兼容所有 systemctl 命令,和yum安装使用方式完全一致。
3.2.4 验证是否安装成功
体验AI代码助手
代码解读
复制代码
nginx -v
二、Nginx 常用启停命令(必记)
bash
体验AI代码助手
代码解读
复制代码
# 启动 systemctl start nginx # 停止 systemctl stop nginx # 重启 systemctl restart nginx # 重载配置(不中断服务,改配置必用) systemctl reload nginx # 设置开机自启 systemctl enable nginx # 查看状态 systemctl status nginx # 检查配置是否有误 nginx -t
安装启动后,浏览器输入服务器 IP,能看到 Nginx 默认欢迎页即安装成功。
三、Nginx 默认核心目录结构
bash
体验AI代码助手
代码解读
复制代码
/etc/nginx/ # 主配置目录 ├── nginx.conf # 主配置文件(核心) ├── conf.d/ # 子配置目录(推荐站点放这里) ├── sites-available/ # 站点可用配置 ├── sites-enabled/ # 启用站点软链接 /var/log/nginx/ # 日志目录 access.log / error.log /usr/share/nginx/html/ # 默认静态页面根目录
生产建议:主 nginx.conf 只保留全局配置,每个站点单独拆到 conf.d/*.conf,方便管理。
四、nginx.conf 完整原版 + 逐行超详细详解
完整默认 nginx.conf 模板
nginx
体验AI代码助手
代码解读
复制代码
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # 引入其他模块配置 include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { 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; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name _; root /usr/share/nginx/html; location / { index index.html index.htm; } } }
逐行详解
1. 全局块(最外层)
nginx
体验AI代码助手
代码解读
复制代码
user nginx;
指定 Nginx 运行进程所属用户,默认 nginx,权限够用,不用改。
nginx
体验AI代码助手
代码解读
复制代码
worker_processes auto;
工作进程数,auto 自动匹配 CPU 核心数,生产推荐 auto,不用手动设。
nginx
体验AI代码助手
代码解读
复制代码
error_log /var/log/nginx/error.log;
错误日志存放路径,排查 502、启动失败必看这里。
nginx
体验AI代码助手
代码解读
复制代码
pid /run/nginx.pid;
记录 Nginx 主进程 PID 文件路径,系统启停依赖这个文件。
2. events 事件块
nginx
体验AI代码助手
代码解读
复制代码
events { worker_connections 1024; }
worker_connections 1024:单个进程最大并发连接数- 默认 1024 足够中小型项目,高并发可调到 2048、4096
3. http 全局块(最核心)
nginx
体验AI代码助手
代码解读
复制代码
log_format main xxx;
定义日志格式:客户端IP、请求时间、请求地址、状态码、请求大小、来源、浏览器UA 等。
nginx
体验AI代码助手
代码解读
复制代码
access_log /var/log/nginx/access.log main;
指定访问日志路径,使用上面定义的 main 格式。
nginx
体验AI代码助手
代码解读
复制代码
sendfile on;
开启高效文件传输,静态资源提速,必须开启。
nginx
体验AI代码助手
代码解读
复制代码
tcp_nopush on; tcp_nodelay on;
优化 TCP 网络传输,提升响应速度,默认保持开启即可。
nginx
体验AI代码助手
代码解读
复制代码
keepalive_timeout 65;
HTTP 长连接超时时间 65 秒,空闲连接 65 秒后断开。
nginx
体验AI代码助手
代码解读
复制代码
include mime.types; default_type application/octet-stream;
- 引入文件 MIME 类型,让 Nginx 识别 html/css/js/png/jpg 等
- 无法识别的文件默认当作二进制流下载
nginx
体验AI代码助手
代码解读
复制代码
include /etc/nginx/conf.d/*.conf;
重点:引入 conf.d 下所有 .conf 配置,后续所有站点、反向代理都放这里,不改动主配置。
4. server 虚拟主机块
nginx
体验AI代码助手
代码解读
复制代码
listen 80;
监听 80 端口,HTTP 默认端口。
nginx
体验AI代码助手
代码解读
复制代码
server_name _;
匹配所有未指定域名的请求,默认兜底站点。
nginx
体验AI代码助手
代码解读
复制代码
root /usr/share/nginx/html;
静态资源根目录。
nginx
体验AI代码助手
代码解读
复制代码
location / { index index.html index.htm; }
访问根路径默认返回 index.html / index.htm。
五、实战配置1:静态网站站点(conf.d/site.conf)
nginx
体验AI代码助手
代码解读
复制代码
server { listen 80; server_name blog.xxx.com; root /home/www/blog; index index.html; location / { try_files $uri $uri/ /index.html; } # 缓存静态资源 location ~* \.(jpg|png|gif|js|css)$ { expires 7d; } }
六、实战配置2:反向代理(后端接口转发)
nginx
体验AI代码助手
代码解读
复制代码
server { listen 80; server_name api.xxx.com; location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
七、改配置后标准流程
bash
体验AI代码助手
代码解读
复制代码
# 1. 检查配置语法是否有错 nginx -t # 2. 重载配置(不中断业务) systemctl reload nginx
八、总结
- Linux 推荐 yum/apt 一键安装,简单省心;
- 牢记 Nginx 启停、重载、开机自启常用命令;
- 理解 nginx.conf 四层结构:全局块 → events → http → server → location;
- 生产规范:主配置不动,站点全部拆到
conf.d目录; - 改配置一定要先
nginx -t校验,再reload,避免服务挂掉。
作者:dxdz
链接:https://juejin.cn/post/7635442226148671528
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。