ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

Java 项目实战: 外卖平台优化-Nginx配置文件结构与块层级

Java 项目实战: 外卖平台优化-Nginx配置文件结构与块层级 接入 Nginx: nginx.conf 三层结构与块级作用域纲要「Nginx配置文件整体结构」把conf/nginx.conf从逻辑上拆开三大块全局块、events块、http块各自负责什么http块的两层嵌套http全局块 →server块 →location块对照真实配置逐行讲以nginx-1.16.1自带的默认nginx.conf为例固定指令 vs 可配置值哪些是关键字不能动哪些可以改多server、多location虚拟主机与路径匹配的组织方式语法规则分号、花括号、注释、相对路径基准http块server块server 全局块listen / server_namelocation 块root / indexhttp 全局块include / sendfile / keepalive_timeoutserver 块events块worker_connections 1024;全局块#user nobody;worker_processes 1;#error_log / #pid一、为什么要先理解结构Nginx的能力全部通过conf/nginx.conf表达。这个文件默认有 117 行其中大量是注释但初次打开依然会觉得无从下手。理解结构后你会知道改配置就是往正确的块里放正确的指令。后面的三件事——部署静态资源、反向代理、负载均衡——本质上分别是要做的事改哪里部署静态资源server块里的location配rootindex反向代理server块里新增location配proxy_pass负载均衡http块里新增upstream再在location的proxy_pass里引用二、默认配置文件全貌下面是nginx-1.16.1安装后的默认conf/nginx.conf已去掉注释这是它真正的骨架#user nobody; worker_processes 1; #error_log logs/error.log; #pid logs/nginx.pid; events { worker_connections 1024; } http { include 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; #access_log logs/access.log main; sendfile on; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } } }只有 40 行左右。去掉注释是读nginx.conf的第一步——注释占了原文件的三分之二且绝大多数是PHP/HTTPS的示例模板与当前场景无关。三、全局块范围从文件开头到events {之前全部属于全局块。#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;职责配置与Nginx运行本身相关的全局参数与具体请求处理无关。指令作用说明user运行worker进程的用户默认被注释即nobodyworker_processesworker进程数默认1建议设为auto或CPU核数error_log错误日志路径与级别级别可选debug/info/notice/warn/error/critpidpid文件路径默认logs/nginx.pid关于被注释的指令注意这些行前面都有#。被注释不代表不重要而是表示使用默认值。比如#pid logs/nginx.pid;被注释但启动时依然会生成logs/nginx.pid——因为pid指令的默认值就是这个。把它打开并改成别的值才会变。error_log是值得显式打开的error_log logs/error.log; error_log logs/error.log notice;两行都写是常见写法不同级别记到不同文件。调试阶段可以开info甚至debugdebug需要编译时加--with-debug。一个易错点worker_processes与后面events块里的worker_connections共同决定理论最大连接数最大连接数 ≈ worker_processes × worker_connections默认1 × 1024 1024。这是同时保持的连接数不是QPS。反向代理场景下一个客户端连接会占用一个到后端的连接所以作为反向代理时实际承载的客户端连接数要除以 2。四、events块范围events { worker_connections 1024; }职责配置与网络连接相关的参数。指令作用worker_connections每个worker进程能同时处理的最大连接数use指定事件驱动模型如use epoll;Linux下推荐multi_accept是否一次接受多个新连接accept_mutex是否用互斥锁均衡各worker的新连接分配默认配置里只有worker_connections。Linux下Nginx会自动选择最高效的模型epoll通常不需要手写use epoll;。worker_connections调大时的配套操作Linux单进程默认最多打开 1024 个文件描述符ulimit -n连接数超过它会报too many open files。需要同时调整系统限制worker_rlimit_nofile 65535; # 放在全局块 events { worker_connections 65535; }# /etc/security/limits.conf* soft nofile65535* hard nofile65535五、http块范围与职责http { ... }及其内部全部内容。这是配置量最大、操作最频繁的块代理、缓存、日志、虚拟主机server都在这里配。http块在逻辑上再分为两部分http全局块http {之后到第一个server {之前server块可以有一个或多个http全局块http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; #gzip on; server { ... } }这里的指令对所有server生效server内可以覆盖指令作用为什么重要include mime.types引入MIME类型映射决定Content-Type删了会导致CSS/JS失效default_type未匹配MIME时的默认值默认application/octet-stream下载sendfile开启零拷贝文件传输静态资源性能关键直接内核态发送不经用户态tcp_nopush配合sendfile攒够一个包再发提高网络利用率keepalive_timeout长连接超时秒数默认 65 秒减少TCP握手开销gzip响应压缩默认关闭开启可显著减小文本类响应体积access_log/log_format访问日志默认被注释但生效使用默认路径与格式sendfile on是Nginx静态资源性能强于Tomcat的核心原因之一。传统读文件发送要经历磁盘 → 内核缓冲区 → 用户缓冲区 → 内核socket缓冲区 → 网卡两次CPU拷贝、两次上下文切换。sendfile让数据直接在内核态从文件描述符传到socket省掉用户态中转。server块server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location /50x.html { root html; } }server块即虚拟主机Virtual Host——一台机器上的一个站点。一个http块可以配多个server这是Nginx托管多站点的基础server { listen 80; server_name localhost; location / { root html; index index.html; } } server { listen 81; server_name localhost; location / { root html81; index index.html; } }http块从逻辑上分为server全局块listen、server_name等location块可有一个或多个指令作用说明listen监听端口可写80、81、443 ssl、127.0.0.1:80server_name服务器名/域名学习阶段写localhost生产写真实域名server_name的作用多个server监听同一个端口时Nginx依据请求头里的Host与server_name匹配来决定交给哪个server处理。这就是一台服务器用80端口托管多个域名的原理。location块location / { root html; index index.html index.htm; }location用于匹配请求的URI路径。指令作用说明root静态资源根目录html是相对路径基准是安装目录/usr/local/nginxindex默认首页可配多个空格分隔依次查找都找不到返回403/404proxy_pass反向代理目标地址下一篇的核心alias路径别名与root的拼接规则不同见后文一个server可以配多个location这是路径分发的基础location / { root html; # 静态资源走本地 } location /api/ { proxy_pass http://192.168.138.101:8080; # /api 前缀走后端 }root与alias的区别这是配置静态资源时最容易踩的坑。# root最终路径 root 完整 URI location /static/ { root /usr/local/nginx/html; } # 请求 /static/logo.png → /usr/local/nginx/html/static/logo.png # alias最终路径 alias (URI 去掉 location 前缀后的部分) location /static/ { alias /usr/local/nginx/html/; } # 请求 /static/logo.png → /usr/local/nginx/html/logo.pngroot会拼上完整的location路径alias会替换掉location匹配的部分。用错会导致 404且报错信息不会告诉你是路径拼错了。六、固定指令与可配置值nginx.conf里有两种内容固定指令关键字不能改server、listen、server_name、location、root、index、include、error_page、proxy_pass、upstream……可配置值按需修改端口号、域名、URI匹配规则、目录名、首页文件名、代理地址……server { # server 是固定指令 listen 80; # ← 80 可改 server_name localhost; # ← localhost 可改 location / { # ← / 可改匹配规则 root html; # ← html 可改目录名 index index.html; # ← index.html 可改文件名 } }改配置时只动值不动指令名。把server_name写成servername、listen写成listen_port都会报unknown directive。七、语法规则规则说明违反的后果每条指令以分号;结尾worker_processes 1;directive is not terminated by ;块用花括号{}包裹http { ... }unexpected }或is not terminated by }注释用##gzip on;—指令与值之间用空格分隔至少一个空格invalid number of arguments相对路径基准是安装目录root html;→/usr/local/nginx/html—缩进不影响语义只为可读性—漏写分号是最高频的报错。课程演示中就出现过把index index.html;改成index hello.html漏了分号nginx -s reload报nginx: [emerg] unexpected } in /usr/local/nginx/conf/nginx.conf:46报错行号是 46花括号所在行但真正的问题在上一行漏了分号——报错位置往往滞后一行这是排查时的经验点。用vim打开时可以用:set nu显示行号快速定位vim/usr/local/nginx/conf/nginx.conf :set nu八、作用域与继承指令在不同块中的生效范围不同子块可以覆盖父块http 块keepalive_timeout 65gzip onserver 块 Alisten 80server 块 Blisten 81keepalive_timeout 120 ← 覆盖location /root htmllocation /api/proxy_pass ...location /root html81规则http块的配置对该http内所有server生效server块的配置对该server内所有location生效子块中重复定义同一指令子块优先有些指令只能出现在特定块中放错位置会报xxx directive is not allowed here常见指令的允许位置指令可出现的位置worker_processes全局块worker_connectionsevents块include/sendfile/gzip/upstreamhttp块listen/server_nameserver块root/index/proxy_pass/aliaslocation块也可在server/http中作为默认值九、最小可用配置模板理解了结构后一个能跑起来的最小配置只需要这些worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } } }后面所有的功能扩展都是在这个骨架上加upstream、加location、加指令。API 速览指令所属块作用user全局指定worker进程运行的用户worker_processes全局worker进程数建议autoworker_rlimit_nofile全局worker进程可打开的最大文件描述符数error_log全局 /http/server/location错误日志路径与级别pid全局master进程ID文件路径worker_connectionsevents单个worker最大连接数useevents事件驱动模型epoll/select/pollinclude任意引入其他配置文件default_typehttp/server/location默认MIME类型sendfilehttp/server/location零拷贝文件传输tcp_nopushhttp/server/location攒包发送配合sendfilekeepalive_timeouthttp/server/location长连接超时时间gziphttp/server/location响应压缩listenserver监听端口server_nameserver虚拟主机域名error_pagehttp/server/location错误页映射locationserver/locationURI路径匹配块roothttp/server/location静态资源根目录拼接完整URIaliaslocation路径别名替换location前缀indexhttp/server/location默认首页可多个空格分隔官方文档Nginx配置文件结构Beginners Guidehttps://nginx.org/en/docs/beginners_guide.htmlNginx核心模块全局块与events指令https://nginx.org/en/docs/ngx_core_module.htmlNginxhttp核心模块https://nginx.org/en/docs/http/ngx_http_core_module.htmlNginx指令字母索引https://nginx.org/en/docs/dirindex.htmlNginxlocation匹配规则https://nginx.org/en/docs/http/ngx_http_core_module.html#location总结整个nginx.conf就三个块全局块、events块、http块。前两个通常不动日常 90% 的工作都在http块里。http块内部是两层嵌套http全局块对所有站点生效的公共配置→ 若干server块虚拟主机靠listenserver_name区分→ 每个server内若干location块靠URI前缀区分。这个多server 多location的组织方式正是Nginx能同时托管多个站点、同时处理静态资源与反向代理的结构基础。读配置的第一件事是删注释。默认文件 117 行里三分之二是注释和PHP/HTTPS模板删完只剩 40 行骨架结构立刻清晰。每个指令只能出现在特定块中放错会报directive is not allowed here子块可以覆盖父块的同名指令。改配置前先想清楚这条指令该放哪一层——比如gzip放http是对所有站点生效放某个location就只对匹配的路径生效。漏分号是最高频报错且报错行号通常滞后一行。看到unexpected }时往上翻一行找分号。root与alias的拼接规则不同root是根目录 完整URIalias是别名 去掉location前缀后的剩余部分。配静态资源 404 时优先怀疑这里。下一篇用这个结构做第一件实事把外卖平台的静态页面部署到Nginx并逐条解释listen、server_name、location、root、index的实际作用。
返回列表