Nginx 配置速查
反向代理、SSL、静态资源、重载等常用配置
🔍
基础命令
nginx -t测试配置文件语法
nginx -s reload平滑重载配置
systemctl restart nginx重启 Nginx 服务
tail -f /var/log/nginx/error.log实时查看错误日志
Server 配置
server { listen 80; root /var/www/html; }监听 80 端口并设置根目录
index index.html index.htm;设置默认首页文件
try_files $uri $uri/ /index.html;SPA 前端路由 fallback
反向代理
proxy_pass http://127.0.0.1:3000;转发请求到后端服务
proxy_set_header Host $host;传递 Host 请求头
proxy_set_header X-Real-IP $remote_addr;传递客户端真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;传递代理链 IP 信息
HTTPS / SSL
listen 443 ssl;监听 443 并启用 SSL
ssl_certificate /path/fullchain.pem;配置 SSL 证书路径
ssl_certificate_key /path/privkey.pem;配置 SSL 私钥路径
return 301 https://$host$request_uri;HTTP 强制跳转 HTTPS
静态资源优化
location /static/ { alias /data/static/; }映射静态文件目录
expires 30d;设置浏览器缓存时间
gzip on; gzip_types text/plain application/json;开启 Gzip 压缩