How to enable HTTP/2 or HTTP/3 support in Nginx?
Jul 02, 2025 am 12:36 AM要啟用 Nginx 的 HTTP/2 或 HTTP/3 支持,需滿足前提并正確配置;HTTP/2 需 Nginx 1.9.5+、OpenSSL 1.0.2+ 及 HTTPS 環(huán)境;配置時添加 --with-http_v2_module 模塊,修改監(jiān)聽語句為 listen 443 ssl http2; 并重載服務(wù);HTTP/3 基于 QUIC,需使用第三方模塊如 nginx-quic,編譯時引入 BoringSSL 或 OpenSSL QUIC 分支,并配置 UDP 監(jiān)聽端口;部署時常見問題包括 ALPN 未啟用、證書不兼容、防火墻限制及編譯錯誤,建議優(yōu)先采用成熟的 HTTP/2,HTTP/3 適合技術(shù)團隊實驗性部署。
啟用 HTTP/2 或 HTTP/3 支持,能顯著提升網(wǎng)站性能和用戶體驗。但要在 Nginx 上實現(xiàn)它們,并不是簡單勾選一個選項就能完成的,需要從編譯、配置到證書等多個環(huán)節(jié)做好準(zhǔn)備。
啟用 HTTP/2 的前提條件
要讓 Nginx 支持 HTTP/2,首先得確認(rèn)幾個關(guān)鍵點:
- Nginx 版本:確保你的 Nginx 是 1.9.5 或以上版本。
- OpenSSL 支持 ALPN:至少 OpenSSL 1.0.2,推薦使用 LibreSSL 或更新版本。
- 使用 HTTPS:HTTP/2 必須通過加密連接(HTTPS)來運行。
- 支持的客戶端瀏覽器:主流瀏覽器都支持,但一些舊系統(tǒng)可能不兼容。
滿足這些后,就可以開始配置了。
如何配置 Nginx 支持 HTTP/2
在確認(rèn)好前提之后,接下來是具體操作步驟:
- 編譯 Nginx 時加入
--with-http_v2_module
模塊。 - 使用支持 ALPN 的 OpenSSL 庫進行編譯。
- 修改站點配置文件,把監(jiān)聽語句改成類似這樣:
listen 443 ssl http2;
- 確保 SSL 配置部分已正確設(shè)置,包括證書路徑和加密套件等。
- 重載或重啟 Nginx 服務(wù)使配置生效。
如果之前已經(jīng)用 HTTPS 運行了站點,那么只需要改監(jiān)聽那一行,加上 http2
就可以了。記得測試下是否真的啟用了 HTTP/2,可以用 Chrome 開發(fā)者工具或者在線檢測工具查一下。
如何啟用 HTTP/3(基于 QUIC)
HTTP/3 要比 HTTP/2 更進一步,它基于 UDP 協(xié)議的 QUIC 實現(xiàn),延遲更低,更適合移動端和高丟包率網(wǎng)絡(luò)。
不過 Nginx 官方目前還沒原生支持 HTTP/3,需要借助第三方模塊,比如 nginx-quic,而且整個過程更復(fù)雜:
- 需要使用支持 QUIC 的庫(如 BoringSSL 或 OpenSSL QUIC 分支)
- Nginx 要從源碼編譯,開啟相關(guān)模塊
- 需要單獨監(jiān)聽一個 UDP 端口用于 QUIC
- 配置中添加類似:
listen udp443 quic reuseport; http3 on;
- 啟用了 HTTP/2 但瀏覽器顯示還是 HTTP/1.1:檢查 SSL 是否禁用了 ALPN,或者證書類型是否為 ECDSA。
- HTTP/3 握手失?。嚎赡苁欠阑饓蛑虚g設(shè)備不支持 UDP 或 QUIC 協(xié)議。
- 編譯時報錯找不到模塊:確認(rèn) configure 參數(shù)是否包含了對應(yīng)的模塊和依賴庫。
- 性能沒明顯提升:可能是 CDN 或服務(wù)器本身瓶頸限制了協(xié)議帶來的優(yōu)化效果。
由于還在演進階段,部署 HTTP/3 相對麻煩,適合有特定需求的技術(shù)團隊嘗試。
常見問題與注意事項
實際部署過程中,可能會遇到以下情況:
這些問題大多可以通過日志排查和逐步調(diào)試解決。
基本上就這些,HTTP/2 已經(jīng)比較成熟,建議上線使用;HTTP/3 還在發(fā)展中,適合有技術(shù)能力的團隊做實驗性部署。
The above is the detailed content of How to enable HTTP/2 or HTTP/3 support in Nginx?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX and Apache each have their own advantages and disadvantages, and the choice should be based on specific needs. 1.NGINX is suitable for high concurrency scenarios because of its asynchronous non-blocking architecture. 2. Apache is suitable for low-concurrency scenarios that require complex configurations, because of its modular design.

PHP code can be executed in many ways: 1. Use the command line to directly enter the "php file name" to execute the script; 2. Put the file into the document root directory and access it through the browser through the web server; 3. Run it in the IDE and use the built-in debugging tool; 4. Use the online PHP sandbox or code execution platform for testing.

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

Linux system restricts user resources through the ulimit command to prevent excessive use of resources. 1.ulimit is a built-in shell command that can limit the number of file descriptors (-n), memory size (-v), thread count (-u), etc., which are divided into soft limit (current effective value) and hard limit (maximum upper limit). 2. Use the ulimit command directly for temporary modification, such as ulimit-n2048, but it is only valid for the current session. 3. For permanent effect, you need to modify /etc/security/limits.conf and PAM configuration files, and add sessionrequiredpam_limits.so. 4. The systemd service needs to set Lim in the unit file

When configuring Nginx on Debian system, the following are some practical tips: The basic structure of the configuration file global settings: Define behavioral parameters that affect the entire Nginx service, such as the number of worker threads and the permissions of running users. Event handling part: Deciding how Nginx deals with network connections is a key configuration for improving performance. HTTP service part: contains a large number of settings related to HTTP service, and can embed multiple servers and location blocks. Core configuration options worker_connections: Define the maximum number of connections that each worker thread can handle, usually set to 1024. multi_accept: Activate the multi-connection reception mode and enhance the ability of concurrent processing. s

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.
