一、Nginx简介
Nginx(发音为“engine x”)是一款高性能的开源Web服务器、反向代理服务器和邮件代理服务器,适用于高并发场景。其核心优势包括:
高效处理静态资源:适合静态文件服务器。
反向代理与负载均衡:支持动态分发流量到后端服务器。
低资源消耗:基于事件驱动模型,适合高并发场景。
丰富的模块支持:如SSL终止、缓存、访问控制等。
二、安装Nginx
1. 环境准备
Linux系统
Ubuntu/Debian:
bash
深色版本
sudo apt update sudo apt install nginx
CentOS/RHEL:
bash
深色版本
sudo yum install epel-release sudo yum install nginx
Windows系统
下载Nginx:访问Nginx官网,下载Windows版本(如
nginx-1.25.3.zip
)。解压文件至非系统盘目录(如
C:\nginx
)。启动Nginx:
bash
深色版本
cd C:\nginx start nginx.exe # 双击nginx.exe或在命令行执行
验证安装
访问服务器IP或域名的http://<IP>:80
,若显示“Welcome to nginx!”则安装成功。
三、基础配置
1. 配置文件结构
Nginx的主配置文件为nginx.conf
,通常位于:
Linux:
/etc/nginx/nginx.conf
Windows:
nginx/conf/nginx.conf
主要配置块:
http
:全局配置,包含所有虚拟主机(server
块)。server
:定义虚拟主机,监听特定IP和端口。location
:定义URL路径的处理规则。
2. 配置示例:静态文件服务器
nginx
深色版本
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name example.com; # 替换为你的域名或IP
# 静态文件根目录
root /var/www/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# 静态资源缓存配置
location ~* \.(jpg|jpeg|png|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}
}
}
配置说明:
root
:指定静态文件目录。index
:设置默认索引文件。try_files
:优先尝试直接返回文件,否则返回404。expires
:设置静态资源缓存时间。
3. 反向代理配置
假设后端服务运行在http://localhost:3000
:
nginx
深色版本
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
关键参数:
proxy_pass
:后端服务地址。proxy_set_header
:将客户端真实IP传递给后端。
4. 配置验证与重启
检查配置语法:
bash
深色版本
nginx -t # Linux nginx.exe -t # Windows
重新加载配置:
bash
深色版本
sudo systemctl reload nginx # Linux nginx.exe -s reload # Windows
四、高级配置与优化
1. 负载均衡
nginx
深色版本
http {
upstream backend {
server 192.168.1.10:8080 weight=3; # 权重3
server 192.168.1.11:8080; # 权重1
server 192.168.1.12:8080 backup; # 备用服务器
}
server {
listen 80;
server_name loadbalancer.example.com;
location / {
proxy_pass http://backend;
}
}
}
负载均衡策略:
weight
:权重值越高,分配的流量越多。backup
:仅在主服务器不可用时启用。
2. SSL/TLS配置(HTTPS)
nginx
深色版本
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/cert.pem; # SSL证书
ssl_certificate_key /path/to/privkey.pem;
location / {
proxy_pass https://backend;
}
}
获取SSL证书:
使用Let's Encrypt免费证书:
bash
深色版本
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d example.com
3. 性能优化
调整工作进程和连接数:
nginx
深色版本
worker_processes auto; # 自动根据CPU核心数分配
events {
worker_connections 4096; # 每个进程最大连接数
}
启用Gzip压缩:
nginx
深色版本
http {
gzip on;
gzip_types text/plain text/css application/json application/javascript;
}
五、安全加固
1. 绑定特定IP
nginx
深色版本
server {
listen 192.168.1.100:80; # 仅监听特定IP
# 其他配置...
}
2. 限制访问IP
nginx
深色版本
location /admin {
deny 192.168.1.100; # 禁止特定IP
allow 192.168.1.0/24; # 允许子网
deny all; # 其他IP拒绝
}
3. 限制请求大小
nginx
深色版本
http {
client_max_body_size 10M; # 限制上传文件大小
client_body_timeout 10; # 设置请求体超时时间
}
六、常见问题与解决方案
1. 启动失败
问题:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解决:检查端口占用:
bash
深色版本
sudo netstat -tuln | grep :80 sudo kill -9 <PID>
2. 配置语法错误
问题:
nginx: [emerg] unexpected "}" in...
解决:使用
nginx -t
检查配置语法,修正错误后重新加载。
3. 端口被防火墙拦截
解决:开放端口(如80、443):
bash
深色版本
sudo ufw allow 80/tcp sudo ufw allow 443/tcp
七、总结
Nginx的安装与配置涉及多个关键步骤,从基础的静态服务器到高级的负载均衡和安全策略,需根据实际需求灵活调整。通过合理配置,可显著提升服务器性能与安全性。建议参考Nginx官方文档获取最新信息。
参考资料:
Nginx官方文档:https://nginx.org/en/docs/
Let's Encrypt证书指南:https://certbot.eff.org/