这一篇,我们进入真正的核心:理解 nginx.conf 的结构与执行逻辑一、主配置文件位置
默认路径:/etc/nginx/nginx.confuser nginx;worker_processes auto;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; include /etc/nginx/conf.d/*.conf;}
二、全局配置(Global)
worker_processes = CPU核心数
三、events 块:连接模型配置
events { worker_connections 1024;}
worker_processes × worker_connections四、http 块:核心业务区域
日志
压缩
反向代理
虚拟主机
server 配置
http {log_format main '$remote_addr - $request';access_log /var/log/nginx/access.log main;include /etc/nginx/conf.d/*.conf;}
include /etc/nginx/conf.d/*.conf;
这表示:自动加载 conf.d 目录下的所有配置文件五、server 块:虚拟主机
server { listen 80; server_name example.com; root /usr/share/nginx/html; index index.html;}
六、location 块:请求匹配规则
location / { root /usr/share/nginx/html;}
location /api/ { proxy_pass http://127.0.0.1:8080;}
七、location 匹配规则简述
八、配置修改与验证流程
九、一个完整示例
/etc/nginx/conf.d/demo.conf # 这里的配置名称建议为 域名.conf
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; } location /api/ { proxy_pass http://127.0.0.1:8080; }}
十、本篇核心总结
继续跟着系列走,你会真正掌握 Nginx 的底层逻辑。