亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
? What Are Keepalive Connections?
? Client-side Keepalive (Browser to Nginx)
?? Upstream Keepalive (Nginx to Backend)
Step 1: Define an upstream with keepalive pool
Step 2: Configure the proxy to reuse connections
? Real-World Impact
? Common Pitfalls
首頁 運(yùn)維 Nginx Nginx和Keepalive Connections

Nginx和Keepalive Connections

Jul 29, 2025 am 03:02 AM

啟用客戶端和上游Keepalive連接可顯著提升Nginx性能;2. 客戶端側(cè)設(shè)置keepalive_timeout和keepalive_requests以控制空閑時間和請求上限;3. 上游側(cè)需在upstream塊中配置keepalive數(shù)量并使用proxy_http_version 1.1與proxy_set_header Connection ""實(shí)現(xiàn)連接復(fù)用;4. 錯誤配置會導(dǎo)致連接無法復(fù)用或資源耗盡,正確調(diào)優(yōu)可降低延遲20–50%并減少CPU開銷。

Nginx and Keepalive Connections

When optimizing web server performance—especially with Nginx—Keepalive connections are one of the most impactful settings you can tune. They reduce latency, lower CPU overhead from TCP handshakes, and improve overall throughput for both clients and upstream servers.

Nginx and Keepalive Connections

Here’s what you need to know:


? What Are Keepalive Connections?

Keepalive keeps a TCP connection open after a request/response cycle so that multiple HTTP requests can be sent over the same connection instead of opening a new one each time. This avoids the overhead of repeated TCP and TLS handshakes.

Nginx and Keepalive Connections

In Nginx, there are two main contexts where keepalive matters:

  1. Client → Nginx (browser to server)
  2. Nginx → Upstream (like app servers or backends)

? Client-side Keepalive (Browser to Nginx)

This is handled automatically if the client supports HTTP/1.1 (which it almost always does). But you can fine-tune it in your server block:

Nginx and Keepalive Connections
server {
    keepalive_timeout  65s;     # How long to keep idle client connections open
    keepalive_requests 100;     # Max requests per connection (default 100)
}
  • keepalive_timeout: If no new request comes within this time, the connection closes.
  • keepalive_requests: Limits how many requests one connection can serve—useful to prevent memory leaks or resource exhaustion.

? Tip: For high-traffic sites, lowering keepalive_timeout to 15–30s can reduce idle connections without hurting performance much.


?? Upstream Keepalive (Nginx to Backend)

This is where many miss big performance gains. If Nginx proxies to a backend (e.g., FastAPI, Node.js, PHP-FPM), enabling keepalive there reduces latency dramatically.

Step 1: Define an upstream with keepalive pool

upstream backend {
    server 127.0.0.1:8000;
    keepalive 32;  # Number of idle keepalive connections per worker
}

Step 2: Configure the proxy to reuse connections

location / {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
}

Why these lines?

  • proxy_http_version 1.1; — Required for keepalive (HTTP/1.0 doesn’t support it).
  • proxy_set_header Connection ""; — Clears the Connection: close header from the client, allowing Nginx to reuse the upstream connection.

Without this, every proxy request opens a new TCP connection to your backend—wasteful and slow.


? Real-World Impact

  • Latency: Eliminates TCP/TLS handshake per request → faster response times.
  • Throughput: One connection can handle dozens or hundreds of requests.
  • CPU/Memory: Fewer connections = less overhead on both Nginx and upstream servers.

For example, if your backend is a Python app behind Gunicorn, enabling upstream keepalive in Nginx can reduce average response time by 20–50% under load.


? Common Pitfalls

  • Forgetting proxy_set_header Connection ""; — upstream connections won’t be reused.
  • Not setting keepalive in upstream block — defaults to no keepalive.
  • Too high a keepalive_timeout — can exhaust file descriptors on busy servers.
  • Using HTTP/1.0 in proxy — won’t support keepalive at all.

Basically, if you're using Nginx as a reverse proxy (which most people are), enabling both client and upstream keepalive is low-hanging fruit for better performance. It’s not magic—it’s just smart reuse of connections.

以上是Nginx和Keepalive Connections的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Laravel 教程
1597
29
PHP教程
1488
72
主Nginx配置文件(nginx.conf)在哪裡? 主Nginx配置文件(nginx.conf)在哪裡? Jul 05, 2025 am 12:10 AM

Nginx主配置文件通常位於/etc/nginx/nginx.conf(Ubuntu/Debian、CentOS/RHEL)、/usr/local/etc/nginx/nginx.conf(macOSHomebrew)或源碼安裝路徑下的conf目錄;可通過nginx-t查看加載的配置路徑,ps-ef|grepnginx檢查啟動參數(shù)指定的路徑,或用find/-namenginx.conf、locatenginx.conf快速查找;配置文件結(jié)構(gòu)包含全局設(shè)置、events塊和http塊,常見站點(diǎn)配置常

是什麼導(dǎo)致NGINX中的'太多打開文件”錯誤? 是什麼導(dǎo)致NGINX中的'太多打開文件”錯誤? Jul 05, 2025 am 12:14 AM

當(dāng)Nginx出現(xiàn)“Toomanyopenfiles”錯誤時,通常是因?yàn)橄到y(tǒng)或進(jìn)程達(dá)到了文件描述符限制。解決方法包括:1.提高Linux系統(tǒng)的軟硬限制,在/etc/security/limits.conf中設(shè)置nginx或運(yùn)行用戶的相關(guān)參數(shù);2.調(diào)整Nginx的worker_connections值以適應(yīng)預(yù)期流量,並確保重載配置;3.增加系統(tǒng)級文件描述符上限fs.file-max,編輯/etc/sysctl.conf並應(yīng)用更改;4.優(yōu)化日誌和資源使用,減少不必要的文件句柄佔(zhàn)用,例如使用open_l

什麼是Stub_Status模塊,如何啟用其監(jiān)視? 什麼是Stub_Status模塊,如何啟用其監(jiān)視? Jul 08, 2025 am 12:30 AM

stub_status模塊顯示Nginx的實(shí)時基礎(chǔ)狀態(tài)信息。具體包括:1.當(dāng)前活躍連接數(shù);2.總接受連接數(shù)、總處理連接數(shù)、總請求數(shù);3.正在讀取、寫入、等待的連接數(shù)。要檢查是否啟用,可通過命令nginx-V查看是否存在--with-http_stub_status_module參數(shù)。若未啟用,需重新編譯加入該模塊。啟用時需在配置文件中添加location塊並設(shè)置訪問控制,最後重載Nginx服務(wù)即可通過指定路徑訪問狀態(tài)頁面。建議結(jié)合監(jiān)控工具使用,但僅限內(nèi)部網(wǎng)絡(luò)訪問,且不能替代全面監(jiān)控方案。

錯誤'已經(jīng)在使用中的地址”或'端口80已經(jīng)在使用中”是什麼意思? 錯誤'已經(jīng)在使用中的地址”或'端口80已經(jīng)在使用中”是什麼意思? Jul 07, 2025 am 12:09 AM

"Addressalreadyinuse"錯誤意味著系統(tǒng)中另一程序或服務(wù)已佔(zhàn)用目標(biāo)端口或IP地址。常見原因包括:1.服務(wù)器重複運(yùn)行;2.其他服務(wù)佔(zhàn)用端口(如Apache佔(zhàn)用80端口導(dǎo)致Nginx無法啟動);3.崩潰或重啟後端口未釋放。可通過命令行工具排查:Linux/macOS使用sudolsof-i:80或sud??onetstat-tulpn|grep:80;Windows通過netstat-ano|findstr:80並查PID。解決方法包括:1.停止衝突進(jìn)程(如sudos

如何啟用HTTP嚴(yán)格運(yùn)輸安全(HSTS)? 如何啟用HTTP嚴(yán)格運(yùn)輸安全(HSTS)? Jul 12, 2025 am 01:00 AM

啟用HSTS的方法是在HTTPS網(wǎng)站中配置Strict-Transport-Security響應(yīng)頭,具體操作為:1.Nginx在server塊添加add_header指令;2.Apache在配置文件或.htaccess添加Header指令;3.IIS在web.config添加customHeaders;需確保站點(diǎn)已完整支持HTTPS,參數(shù)包括max-age(有效期)、includeSubDomains(子域名生效)、preload(預(yù)加載列表),提交到HSTSPreload列表前提包括根域名和子

什麼是Nginx Plus,其主要功能是什麼? 什麼是Nginx Plus,其主要功能是什麼? Jul 07, 2025 am 12:37 AM

NginxPlus與開源Nginx的主要區(qū)別在於其為企業(yè)級應(yīng)用提供的增強(qiáng)功能和官方支持。 1.它提供實(shí)時監(jiān)控儀錶盤,可追蹤連接數(shù)、請求率及服務(wù)器健康狀態(tài);2.支持更高級的負(fù)載均衡方法,如最少連接數(shù)分配、基於哈希的一致性算法及加權(quán)分發(fā);3.支持會話保持(粘性會話),確保用戶請求持續(xù)發(fā)送至同一後端服務(wù)器;4.允許動態(tài)更新配置,無需重啟服務(wù)即可調(diào)整上游服務(wù)器組;5.提供高級緩存和內(nèi)容分發(fā)功能,降低後端壓力並提升響應(yīng)速度;6.可通過API實(shí)現(xiàn)自動化配置更新,適應(yīng)Kubernetes或自動擴(kuò)展環(huán)境;7.包含

如何使用split_clients模塊執(zhí)行A/B測試? 如何使用split_clients模塊執(zhí)行A/B測試? Jul 08, 2025 am 12:22 AM

A/B測試可通過Nginx的split_clients模塊實(shí)現(xiàn),該方法基於用戶屬性哈希將流量按比例分配至不同組。具體步驟如下:1.在http塊中使用split_clients指令定義分組及比例,如50%A和50%B;2.使用$cookie_jsessionid、$remote_addr或$arg_uid等變量作為哈希鍵,確保同一用戶持續(xù)分配至同一組;3.在server或location塊中通過if條件判斷使用對應(yīng)後端;4.通過自定義日誌格式記錄分組信息以便分析效果;5.結(jié)合監(jiān)控工具跟蹤各組性能與

See all articles