1. 基础 HTTP 服务配置
server {
listen 80; # 监听 80 端口(HTTP)
server_name example.com; # 域名或 IP
root /var/www/html; # 静态文件根目录
index index.html index.htm; # 默认索引文件
location / {
try_files $uri $uri/ =404; # 优先匹配文件,再匹配目录,否则返回 404
}
}
2. 反向代理配置
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:3000; # 将请求转发到本地的 3000 端口(如 Node.js 应用)
proxy_set_header Host $host; # 传递原始请求头
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3. 负载均衡
http {
upstream backend { # 定义后端服务器组
server 10.0.0.1:80 weight=3; # 权重 3(更高优先级)
server 10.0.0.2:80;
server 10.0.0.3:80 backup; # 备用服务器(当其他服务器宕机时启用)
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend; # 请求转发到后端组
# 可选负载均衡算法:轮询(默认)、weight、ip_hash(会话保持)、least_conn(最少连接)等
}
}
}
4. SSL 配置(HTTPS)
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt; # SSL 证书路径
ssl_certificate_key /etc/nginx/ssl/example.com.key; # 私钥路径
# 推荐 SSL 协议和加密套件
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
location / {
root /var/www/html;
index index.html;
}
}
# 强制 HTTP 跳转到 HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
5. 静态文件缓存优化
server {
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 7d; # 客户端缓存 7 天
add_header Cache-Control "public";
access_log off; # 可选:关闭此类请求的日志
}
}
6. 重定向与重写规则
# 301 永久重定向
server {
listen 80;
server_name old-domain.com;
return 301 https://new-domain.com$request_uri;
}
# URL 重写(如隐藏 .php 扩展)
server {
location / {
rewrite ^/user/(\d+) /user.php?id=$1 last; # 将 /user/123 重写为 /user.php?id=123
}
}
7. 访问控制
# 限制 IP 访问
location /admin {
allow 192.168.1.0/24; # 允许特定 IP 段
deny all; # 拒绝其他所有 IP
}
# 基础认证(用户名/密码)
location /private {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd; # 使用 htpasswd 生成的文件
}
8. 日志配置
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; # 访问日志路径及格式
error_log /var/log/nginx/error.log warn; # 错误日志级别(warn、error、crit 等)
}
9. Gzip 压缩
http {
gzip on; # 开启 Gzip
gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml;
gzip_min_length 1024; # 最小压缩文件大小
gzip_comp_level 6; # 压缩级别(1-9)
gzip_vary on; # 根据请求头 Vary: Accept-Encoding 启用
}
10. 跨域配置(CORS)
location /api {
add_header 'Access-Control-Allow-Origin' '*'; # 允许所有域名(生产环境建议指定域名)
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
}
常见问题处理
- 413 Request Entity Too Large
在 http 或 server 块中添加:
client_max_body_size 100M; # 允许上传最大文件大小
2.502 Bad Gateway
检查后端服务是否运行,或调整代理超时时间:
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
配置检查与重载
# 检查配置语法
nginx -t
# 重新加载配置(不中断服务)
nginx -s reload
以上配置覆盖了大部分常见需求,实际使用时需根据场景调整参数。更复杂的配置可参考 Nginx 官方文档。