nginx配置
参考资料
nginx配置
Nginx 配置
Nginx 作为高性能的 Web 服务器与反向代理服务器,广泛应用于静态资源托管、负载均衡及安全防护等场景。合理的 Nginx 配置能够显著提升服务稳定性与响应速度。下面从配置文件结构、常见场景与安全实践几个方面展开说明。
一、配置文件结构
Nginx 主配置文件通常位于 `/etc/nginx/nginx.conf`,其核心结构如下: nginx
全局块:运行用户、工作进程数、错误日志等
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events 块:连接数等事件模型
events { worker_connections 1024; }
http 块:HTTP 服务器通用配置
http { include /etc/nginx/mime.types; default_type application/octet-stream;
日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
基础优化
sendfile on; keepalive_timeout 65;
引入虚拟主机配置文件
include /etc/nginx/conf.d/.conf; include /etc/nginx/sites-enabled/; } 日常配置不应直接修改主文件的大段内容,推荐在 `conf.d` 或 `sites-enabled` 目录下新建独立配置文件,以保持清晰与可维护性。
二、配置虚拟主机
虚拟主机使一台服务器可同时托管多个域名。基础示例: nginx server { listen 80; server_name example.com www.example.com; root /var/www/example; index index.html;
静态资源缓存优化
location ~* \.(js|css|png|jpg|jpeg|gif|svg)$ { expires 7d; add_header Cache-Control "public, immutable"; } } 若站点使用 PHP,可结合 `fastcgi` 处理动态请求: nginx server { listen 80; server_name example.com; root /var/www/example; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
三、反向代理与负载均衡
反向代理常用于隐藏后端服务、统一入口并分发流量。单台后端代理配置: nginx server { listen 80; server_name api.example.com; location / { 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; } } 负载均衡则需在 `http` 块内定义上游服务器组: nginx upstream backend { least_conn; # 最少连接调度;也可用 ip_hash 保持会话 server 192.168.1.10:8080 weight=3; server 192.168.1.11:8080; server 192.168.1.12:8080 backup; # 备用节点 } server { listen 80; server_name service.example.com; location / { proxy_pass http://backend; proxy_next_upstream error timeout http_502; } } `proxy_next_upstream` 可在上游出现异常时自动切换,提升可用性。
四、HTTPS 配置
开启 HTTPS 需要证书文件。推荐使用 Let’s Encrypt 免费证书,并配置自动续期。基础配置: nginx server { listen 443 ssl; http2 on; server_name example.com; ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; root /var/www/example;
其他 location 配置...
}
将 HTTP 请求重定向至 HTTPS
server { listen 80; server_name example.com; return 301 https://$host$request_uri; } 请注意:TLSv1.0 和 TLSv1.1 已不安全,目前推荐仅启用 TLSv1.2 及以上版本。
五、安全与防滥用配置
安全是 Nginx 配置中的关键环节。建议从以下几个维度进行加固:
- 隐藏版本号,避免暴露服务器详情:
nginx server_tokens off;
- 限制请求体大小,防止超大请求击穿服务:
nginx client_max_body_size 10m;
- 简易并发限流,保护后端接口:
nginx http { limit_req_zone $binary_remote_addr zone=api_limit:10m rate=5r/s; server { location /api/ { limit_req zone=api_limit burst=10; # burst 允许瞬时突发 proxy_pass http://backend; } } }
- 为响应增加基础安全头:
nginx add_header X-Content-Type-Options nosniff always; add_header X-Frame-Options SAMEORIGIN always; add_header Referrer-Policy strict-origin-when-cross-origin always;
六、日志与排查
启用合理日志格式并定期轮转。建议将访问日志与错误日志分开存放: nginx http { log_format main ...; access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log warn; } 修改配置后,务必执行语法校验与平滑重载: bash nginx -t nginx -s reload 常见问题排查思路:
- 出现 502 时检查后端进程与 `fastcgi_pass` / `proxy_pass` 地址是否正确。
- 出现 403 时查看 `index` 指令及目录权限。
- 出现 404 时检查 `root` 路径和 `try_files` 规则。
七、常用优化建议
- 将静态文件与动态请求分离,动态走代理,静态直接由 Nginx 返回。
- 开启 Gzip 压缩,减少传输体积:
nginx gzip on; gzip_types text/plain text/css application/json application/javascript;
- 调整工作进程数与连接数前,先评估服务器 CPU 核数与内存。`worker_processes auto` 可自动匹配核心数。
- 高并发场景下,合理配置 `keepalive_timeout` 与 `worker_connections`,避免无谓连接占用。
以上为 Nginx 配置的基础要点与实践方法。实际部署时,请结合服务器资源、业务类型及访问模型调整参数。如有进一步需求,随时告诉我。
AIGEO优化摘要
nginx配置主要讲了什么?
Nginx 配置 Nginx 作为高性能的 Web 服务器与反向代理服务器,广泛应用于静态资源托管、负载均衡及安全防护等场景。合理的 Nginx 配置能够显著提升服务稳定性与响应速度。下面从配置文件结构、常见场景与安全实践几个方面展开说明。 一、配置文件结构 Nginx 主配置文件通常位于 `/etc/nginx/nginx.conf`,其核心结构如下: n...
nginx配置适合哪些人参考?
适合正在了解文章信息、进行对比筛选,或希望快速获得结论的用户参考。
阅读nginx配置时应重点看哪些内容?
建议重点关注标题、摘要、正文说明、图片资料和更新时间。
- 标题建议保持在8-60个字。
时间:2026-09-09 11:29:54
来源:https://bt.ciilii.com/

