简介
Nginx 是 Linux 服务端必备高性能 Web 服务器、反向代理、负载均衡中间件,部署项目、域名转发、静态资源托管、跨域配置基本都离不开它。
本文从零开始:Linux 安装 Nginx → 常用命令 → 目录结构 → nginx.conf 逐行详解 → 反向代理/静态站点配置实战,全程干货无废话,可直接收藏当手册、发掘金专栏、面试复习。
适合人群 :后端开发者、服务器运维、初学 Nginx、项目部署 环境:CentOS 7 / CentOS 8 / Ubuntu 通用
一、Linux 安装 Nginx
1. CentOS yum 安装
bash
# 更新源
yum update -y
# 安装nginx
yum install nginx -y
2. Ubuntu apt 安装
bash
apt update
apt install nginx -y
3. 源码安装(适合自定义版本)
适合需要指定版本、自定义模块,我只给核心流程:
bash
# 安装依赖
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
./configure
或指定目录./configure --prefix=/usr/local/nginx --with-http_ssl_module
make && make install
安装nginx之前,需要先安装以下几个库:
bash
yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl--devel
二、Nginx 常用启停命令(必记)
bash
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx
# 重载配置(不中断服务,改配置必用)
systemctl reload nginx
# 设置开机自启
systemctl enable nginx
# 查看状态
systemctl status nginx
# 检查配置是否有误
nginx -t
安装启动后,浏览器输入服务器 IP,能看到 Nginx 默认欢迎页即安装成功。
三、Nginx 默认核心目录结构
bash
/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
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
user nginx;
指定 Nginx 运行进程所属用户,默认 nginx,权限够用,不用改。
nginx
worker_processes auto;
工作进程数,auto 自动匹配 CPU 核心数,生产推荐 auto,不用手动设。
nginx
error_log /var/log/nginx/error.log;
错误日志存放路径,排查 502、启动失败必看这里。
nginx
pid /run/nginx.pid;
记录 Nginx 主进程 PID 文件路径,系统启停依赖这个文件。
2. events 事件块
nginx
events {
worker_connections 1024;
}
worker_connections 1024:单个进程最大并发连接数- 默认 1024 足够中小型项目,高并发可调到 2048、4096
3. http 全局块(最核心)
nginx
log_format main xxx;
定义日志格式:客户端IP、请求时间、请求地址、状态码、请求大小、来源、浏览器UA 等。
nginx
access_log /var/log/nginx/access.log main;
指定访问日志路径,使用上面定义的 main 格式。
nginx
sendfile on;
开启高效文件传输,静态资源提速,必须开启。
nginx
tcp_nopush on;
tcp_nodelay on;
优化 TCP 网络传输,提升响应速度,默认保持开启即可。
nginx
keepalive_timeout 65;
HTTP 长连接超时时间 65 秒,空闲连接 65 秒后断开。
nginx
include mime.types;
default_type application/octet-stream;
- 引入文件 MIME 类型,让 Nginx 识别 html/css/js/png/jpg 等
- 无法识别的文件默认当作二进制流下载
nginx
include /etc/nginx/conf.d/*.conf;
重点:引入 conf.d 下所有 .conf 配置,后续所有站点、反向代理都放这里,不改动主配置。
4. server 虚拟主机块
nginx
listen 80;
监听 80 端口,HTTP 默认端口。
nginx
server_name _;
匹配所有未指定域名的请求,默认兜底站点。
nginx
root /usr/share/nginx/html;
静态资源根目录。
nginx
location / {
index index.html index.htm;
}
访问根路径默认返回 index.html / index.htm。
五、实战配置1:静态网站站点(conf.d/site.conf)
nginx
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
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
# 1. 检查配置语法是否有错
nginx -t
# 2. 重载配置(不中断业务)
systemctl reload nginx
八、总结
- Linux 推荐 yum/apt 一键安装,简单省心;
- 牢记 Nginx 启停、重载、开机自启常用命令;
- 理解 nginx.conf 四层结构:全局块 → events → http → server → location;
- 生产规范:主配置不动,站点全部拆到
conf.d目录; - 改配置一定要先
nginx -t校验,再reload,避免服务挂掉。