Share E-Book
Scan to open this page

Scan with your phone to open this page

AuthorDerek DeJonghe

NGINX 是当今使用最广泛的 Web 服务器之一,部分原因在于它可以用作 HTTP 和其他网络协议的负载均衡器和反向代理服务器。本修订版完全指南通过一些简单易懂的例子解析了应用交付中真实存在的问题。实用的实操指南可帮助您设置开源或商业产品,并利用它们解决各种用例中的问题。 对于了解现代 Web 架构(例如 n 层或微服务设计以及 TCP和 HTTP 等常见 Web 协议)的专业人士来说,本书的这些实操指南为安全和软件负载均衡以及 NGINX 应用交付平台的监控和维护提供了经过验证的解决方案。您还将了解到免费的 NGINX 开源版以及许可授权版 NGINX Plus 的高级功能。 您将获取以下方面的实操指南: • HTTP、TCP 和 UDP 高性能负载均衡 • 通过加密流量、安全链接、HTTP 身份验证子请求等保护访问 • 将 NGINX 部署到 Google、亚马逊云科技(AWS)和Azure 云服务 • 将 NGINX Plus 配置为 SAML 环境中的服务提供商 • HTTP/3(QUIC)、OpenTelemetry 和 njs 模块

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
# NGINX完全指南:实现高性能负载均衡的进阶实操指南 ## 【One-Line Pitch】 A practical, recipe-driven guide to mastering NGINX as a load balancer, reverse proxy, and application delivery platform—covering everything from basic HTTP/TCP/UDP balancing to advanced security, automation, and NGINX Plus features. Ideal for DevOps engineers, site reliability engineers, and architects working with modern web architectures who need proven solutions they can implement immediately. ## 【Book Arc】 - **Opening (~0%–10%)**: Introduces NGINX's role in modern web infrastructure, covering installation, key files and directories (/etc/nginx/, nginx.conf, conf.d/), essential commands, and the foundational distinction between the http (Layer 7) and stream (Layer 4) contexts. Establishes the mental model for everything that follows. - **Early (~10%–20%)**: Dives deep into high-performance load balancing—HTTP, TCP, and UDP upstream pools, weighted round-robin, least connections, generic hash, and IP hash algorithms. Covers session persistence (sticky routing) and health checks, including NGINX Plus's active health checks and slow-start features. - **Early–Middle (~20%–37%)**: Explores traffic management (A/B testing with split_clients, GeoIP-based routing, connection/rate/bandwidth limiting) and scalable content caching (cache zones, hash keys, locking, stale cache, purging, slicing). Introduces programmability and automation: NGINX Plus API, key-value stores, the njs module for JavaScript extensions, and configuration management tools (Ansible, Chef, Consul templates). - **Middle (~37%–50%)**: Focuses on authentication and security—HTTP basic auth, auth subrequests, JWT validation (NGINX Plus), OpenID Connect integration, CORS handling, client-side and upstream TLS encryption, secure links with expiring URLs, and HSTS enforcement. - **Late (~50%–100%)**: Covers advanced security controls (country-based access restrictions), deployment to major cloud platforms (AWS, Google Cloud, Azure), NGINX Plus SAML service provider configuration, and modern features like HTTP/3 (QUIC), OpenTelemetry integration, and the njs module. Concludes with debugging, logging, request tracing, and performance tuning. ## 【Key Takeaways】 - **The http vs. stream context distinction is foundational** (Early): http operates at Layer 7 with full HTTP protocol awareness, while stream operates at Layer 4 for TCP/UDP routing. Choosing the right context determines what load-balancing and inspection capabilities you have. - **Multiple load-balancing algorithms exist for different traffic patterns** (Early): Round-robin (default), weighted round-robin, least connections, generic hash, and random algorithms each suit different scenarios. Generic hash (e.g., using $remote_addr) works in both http and stream contexts, while IP hash is http-only. - **Session persistence requires intelligent routing** (Early): For stateful applications, NGINX provides cookie-based and route-based sticky routing (NGINX Plus) to ensure subsequent requests reach the same backend server—critical for user experience in interactive applications. - **Traffic management features are composable** (Early): split_clients enables A/B testing, GeoIP modules enable location-based routing, and limit_conn/limit_rate/limit_rate_after enable connection, rate, and bandwidth control. These can be mixed to create sophisticated traffic policies. - **Cache keys determine cache effectiveness** (Early): The default proxy_cache_key ($scheme$proxy_host$request_uri) works for static content, but dynamic content requires custom keys incorporating user-specific variables like cookies—choosing the right key requires deep application knowledge. - **NGINX Plus API enables dynamic infrastructure automation** (Middle): The REST API allows servers to self-register and deregister from upstream pools without manual intervention, enabling automatic scaling. The key-value store extends this to dynamic traffic management decisions like blocklists. - **JWT validation and OIDC integration shift auth to the edge** (Middle): NGINX Plus can validate JWT signatures before requests reach application servers, and act as an OIDC relying party—reducing backend workload and centralizing security. Key management (rotation, secure storage) is critical. - **Security headers and upstream encryption are non-negotiable** (Middle): HSTS prevents downgrade attacks, proxy_ssl_verify enables upstream certificate validation (off by default!), and secure_link provides expiring, user-specific protected URLs with proper HTTP 403/410 semantics. ## 【Reading Tips】 - **Skim the first chapter** if you're already familiar with NGINX basics—the file/directory layout and commands are standard, but the http vs. stream distinction is worth re-reading before Chapter 2. - **Deep-read Chapters 2–3** for load balancing and traffic management—these are the core value of the book. Pay special attention to the algorithm selection guidance and how to combine rate/connection/bandwidth limits. - **Chapter 5 (Programmability and Automation)** contains OCR-heavy code samples; focus on understanding the concepts (API-driven scaling, key-value stores, njs) rather than copying code verbatim. The njs JWT example is particularly useful but requires careful reading. - **Chapter 7 (Security)** is where production-readiness lives—the upstream SSL verification defaults (off!) and secure_link patterns are easy to miss but critical for real deployments. The CORS and HSTS sections are practical and immediately applicable. - **The NGINX Plus sections** (sticky routing, active health checks, API, JWT, OIDC, SAML) are clearly marked—if you're using open-source NGINX only, you can skim these but should still understand what features exist for future planning. ## 【Coverage Limits】 This guide covers the book's first ~50% in depth (load balancing, traffic management, caching, automation, authentication, and core security). The later chapters on cloud deployment, SAML, HTTP/3, OpenTelemetry, logging, and performance tuning are only briefly noted—the excerpts provide limited detail on these sections. ##
Page 11
...................................................................... 167 15.0 简介 167 15.1 使用压测工具实现测试自动化 167 15.2 控制浏览器缓存 168 15.3 保持客户端长连接 169 15.4 保持上游长连接...
View in text
Excerpt 2
t-Type 值定义为 'text/html',同时定义了响应正文中的字符串 "Welcome to nginx!"。HTTP match 代码块具有三个指令:status、header 和 body。这三个指令均 带有比较标记。 TCP/UDP 服务的 stream 健康检查非常相似: stream { s t...
View in text
Excerpt 3
务未提供类型(比如 AWS Cognito),因为此类服 务会更改分割数或完全删除它,以便 jwt 函数仅接收令牌值。 在 NGINX 核心配置中加载 njs 模块。在 http 代码块中导入并使用 JavaScript: 52 | 第 5 章:可编程性和自动化 该解决方案演示了 NGINX 中可用的 Lua 和...
View in text
Excerpt 4
add_header 'Content-Type' 'text/plain; charset=UTF-8'; add_header 'Content-Length' 0; return 204; } } } 这个示例中的内容很多,但通过使用 map 将 GET 和 POST 方法进行分组,简化了示例 中的内容。O...
View in text
Excerpt 5
并提供静态内容。这展示了 NGINX 如何在相同的 HTTP/2 端点和相应的路由下支 持 gRPC 和非 gRPC。 对 gRPC 调用的负载均衡也类似于非 gRPC HTTP 流量: upstream grpcservers { upstream grpcservers { server backend1.l...
View in text
Excerpt 6
t/sources.list.d/nginx.list \ \ # 用于存储源代码和.deb 文件的新目录 && tempDir="$(mktemp -d)" \ && chmod 777 "$tempDir" \ #(777 确保 APT 的“ _apt”用 户也可以访问它) \ # 用于构建依赖项的已安装软件...
View in text
Excerpt 7
g_format trace '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$http_x_forwarded_...
View in text
Excerpt 8
同,欧亚猞猁捕食较大的有蹄类动物,例如鹿、麋,甚至是驯养的绵 羊。成年猞猁每天消耗两到五磅肉,可长达一周食用单一食物。 欧亚猞猁在二十世纪中叶濒临灭绝,后来经过坚持不懈的保护,降级为无危物种。 O'Reilly 封面上的许多动物都濒临灭绝,但对世界意义重大。 封面插画由 Karen Montgomery 参考 S...
View in text
Tags
AI categories
BackendCloud NativeCybersecurity
Publisher: O'Reilly Media
Publish Year: 2024
Language: Chinese
Pages: 200
File Format: PDF
File Size: 7.1 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…