Nginx 配置生成器

可视化配置 Nginx 虚拟主机,生成包含 HTTPS/反向代理/负载均衡的完整配置文件

Nginx 配置生成器
基本信息
HTTPS 配置
性能与安全
自定义 location 规则 添加规则
路径必填,留空的规则将被跳过;不添加则使用默认 location /
生成的 Nginx 配置
场景示例 — 点击「填入」加载常用配置模板
静态网站 + HTTPS
纯静态文件托管,启用 HTTPS + HTTP/2 + Gzip
Node.js 反向代理
代理到本地 3000 端口的 Node.js 应用
PHP-FPM(WordPress)
PHP 应用配置,含 WordPress 伪静态规则
关于 Nginx 配置生成器
  • 支持静态文件、反向代理、PHP-FPM 三种站点类型,自动生成对应的 Nginx server 块配置
  • HTTPS 配置遵循 Mozilla SSL 配置最佳实践,包含 TLS 1.2/1.3、安全加密套件、HSTS 等
  • 生成的配置文件通常放在 /etc/nginx/sites-available/ 目录,并软链接到 sites-enabled/
操作说明
  • 选择站点类型,填写域名和相关参数,点击「生成配置」
  • 点击「下载」保存为 example.com.conf 文件,上传到服务器后执行 nginx -t && nginx -s reload
  • 支持 Ctrl+Enter 快捷键触发生成
注意事项
  • 生成的配置为模板,部署前请根据实际环境调整证书路径、端口、目录等参数
  • 修改配置后务必执行 nginx -t 检查语法,再执行 nginx -s reload 重载
  • 所有处理均在浏览器本地完成,不会上传任何数据
Nginx 配置知识详解
Nginx 配置文件结构
# /etc/nginx/nginx.conf(主配置) events { worker_connections 1024; # 每个 worker 进程最大连接数 } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 引入站点配置(推荐方式) include /etc/nginx/sites-enabled/*.conf; server { # 虚拟主机配置 listen 80; server_name example.com www.example.com; location / { # 路径匹配规则 } } }
location 匹配规则详解
修饰符匹配方式优先级示例
=精确匹配最高(1)location = /favicon.ico
^~前缀匹配(不检查正则)高(2)location ^~ /static/
~正则匹配(区分大小写)中(3)location ~ \.php$
~*正则匹配(不区分大小写)中(3)location ~* \.(jpg|png)$
无修饰符前缀匹配低(4)location /api/
# 静态资源缓存 location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff2)$ { expires 30d; add_header Cache-Control "public, immutable"; access_log off; } # 反向代理 API location /api/ { 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; proxy_set_header X-Forwarded-Proto $scheme; } # 禁止访问隐藏文件(.git/.env 等) location ~ /\. { deny all; return 404; } # WebSocket 代理 location /ws/ { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_read_timeout 3600s; }
HTTPS 最佳实践配置
server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # 协议和加密套件(Mozilla Intermediate) ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; # 会话缓存(提升性能) ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d; ssl_session_tickets off; # OCSP Stapling(加速证书验证) ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 8.8.4.4 valid=300s; # 安全响应头 add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always; add_header X-Frame-Options SAMEORIGIN always; add_header X-Content-Type-Options nosniff always; add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; }
性能优化配置
worker_processes auto; # 自动匹配 CPU 核心数 worker_rlimit_nofile 65535; # 最大文件描述符数 events { worker_connections 4096; use epoll; # Linux 高性能 I/O 模型 multi_accept on; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; client_max_body_size 50m; gzip on; gzip_vary on; gzip_comp_level 6; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css text/xml application/json application/javascript application/xml+rss application/atom+xml image/svg+xml; server_tokens off; # 隐藏 Nginx 版本号 }
负载均衡配置
upstream backend { # 轮询(默认) server 127.0.0.1:8001; server 127.0.0.1:8002; server 127.0.0.1:8003; # 权重轮询 # server 127.0.0.1:8001 weight=3; # IP Hash(同一 IP 固定到同一后端) # ip_hash; # 最少连接 # least_conn; keepalive 32; } server { location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_next_upstream error timeout http_500 http_502 http_503; } }
访问控制与限流
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s; limit_conn_zone $binary_remote_addr zone=conn:10m; server { # IP 白名单/黑名单 location /admin/ { allow 192.168.1.0/24; allow 10.0.0.1; deny all; } # 限制 API 请求速率 location /api/ { limit_req zone=api burst=20 nodelay; limit_conn conn 10; proxy_pass http://backend; } # Basic 认证 location /private/ { auth_basic "受限区域"; auth_basic_user_file /etc/nginx/.htpasswd; } }
常用 Nginx 命令速查
命令说明
nginx -t检查配置语法(部署前必做)
nginx -s reload热重载配置(不中断服务)
nginx -s stop快速停止(强制)
nginx -s quit优雅停止(等待请求完成)
nginx -V查看版本和编译参数
systemctl reload nginx通过 systemd 热重载
tail -f /var/log/nginx/error.log实时查看错误日志
ln -s /etc/nginx/sites-available/x.conf /etc/nginx/sites-enabled/启用站点