Nginx保姆级部署教程(2025最新版)
一、环境准备
-
操作系统
- 支持Linux(CentOS、Ubuntu等)、Windows(需通过源码编译)。
- 本文以 CentOS 7 和 Ubuntu 22.04 为例。
-
依赖工具安装
# CentOS yum install -y wget vim gcc make openssl openssl-devel pcre pcre-devel zlib zlib-devel # Ubuntu sudo apt update sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
二、Nginx安装
1. 源码编译安装(推荐)
# 下载Nginx稳定版(以1.26.2为例)
wget http://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxvf nginx-1.26.2.tar.gz
cd nginx-1.26.2
# 配置编译参数(支持SSL、HTTP2等模块)
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-pcre
# 编译并安装
make && make install
2. 包管理器安装(Ubuntu)
sudo apt install nginx
# 验证安装
nginx -v
三、Nginx基础配置
1. 启动与停止
# 启动
/usr/local/nginx/sbin/nginx # 源码安装路径
systemctl start nginx # Ubuntu包管理器安装
# 停止
/usr/local/nginx/sbin/nginx -s stop
systemctl stop nginx
# 重载配置(修改配置文件后执行)
nginx -s reload
2. 防火墙配置
# CentOS(firewalld)
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
# Ubuntu(ufw)
sudo ufw allow 'Nginx Full'
sudo ufw reload
3. 默认配置文件详解
配置文件路径:/usr/local/nginx/conf/nginx.conf
(源码安装)或 /etc/nginx/nginx.conf
(包管理器安装)
核心配置块:
http {
server {
listen 80; # 监听端口
server_name example.com; # 域名
root /var/www/html; # 网站根目录
index index.html; # 默认首页
location / {
try_files $uri $uri/ /index.html; # 处理前端路由
}
# 反向代理配置
location /api/ {
proxy_pass http://backend-server:8080; # 后端服务地址
proxy_set_header Host $host;
}
}
}
四、静态资源部署
1. 配置静态资源目录
location /static/ {
root /var/www/static; # 路径拼接:root路径 + location路径
# 或使用 alias(路径替换)
# alias /var/www/static/;
}
- root与alias区别:
root
:路径拼接(如访问/static/image.jpg
,实际路径为/var/www/static/static/image.jpg
)。alias
:路径替换(如访问/static/image.jpg
,实际路径为/var/www/static/image.jpg
)。
2. 错误页面配置
error_page 404 /404.html;
location = /404.html {
root /var/www/error_pages;
internal; # 仅允许内部访问
}
五、反向代理与负载均衡
1. 反向代理示例
location /api/ {
proxy_pass http://127.0.0.1:8000; # 后端服务地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
2. 负载均衡配置
upstream backend_servers {
server 192.168.1.101:8080 weight=3; # 权重3
server 192.168.1.102:8080; # 默认权重1
ip_hash; # 基于IP的会话保持
}
server {
location / {
proxy_pass http://backend_servers;
}
}
六、SSL证书配置(HTTPS)
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.crt;
ssl_certificate_key /etc/ssl/private/example.com.key;
# 安全增强配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
}
证书获取方式:
- 使用Let’s Encrypt免费证书(推荐):
sudo certbot --nginx -d example.com
- 自签名证书(测试环境):
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /path/to/key -out /path/to/cert
七、常见问题解决
-
启动报错:
libpcre.so.1 not found
ln -s /usr/local/lib/libpcre.so.1 /lib64/ # CentOS
-
配置文件语法错误
nginx -t # 检查配置语法
-
端口被占用
lsof -i :80 # 查看占用进程 kill -9 <PID>
八、高级功能扩展
- 日志分割:使用
logrotate
定期切割日志文件。 - Gzip压缩:在
nginx.conf
中启用gzip on;
。 - 防盗链配置:通过
valid_referers
限制资源引用来源。
总结
以上为Nginx从安装到高级配置的全流程教程,覆盖静态资源托管、反向代理、负载均衡及HTTPS加密等核心场景。若需更详细配置(如高可用集群、缓存优化),可参考:Nginx官方文档 或 CSDN相关专题。