ÔõÑùʹÓÃNginxʵÏÖHTTP/2ÐÒéÖ§³Ö
ÔõÑùʹÓÃnginxʵÏÖhttp/2ÐÒéÖ§³Ö
¸ÅÊö£º
HTTP/2ÊÇÒ»ÖÖÐÂÒ»´úµÄHTTPÐÒ飬Ïà±È֮ǰµÄHTTP/1.xÐÒ飬ËüÔÚÐÔÄܺÍЧÂÊÉÏÓÐÁ˺ܴóµÄÌáÉý¡£ÎªÁËÖ§³ÖHTTP/2ÐÒ飬ÎÒÃÇ¿ÉÒÔʹÓÃNginx×÷ΪHTTPЧÀÍÆ÷£¬²¢ÉèÖÃÏìÓ¦µÄÉèÖá£
×°ÖúÍÉèÖÃNginx£º
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃNginx¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÔÚUbuntuϵͳÖоÙÐÐ×°Öãº
sudo apt-get update sudo apt-get install nginx
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÎÒÃÇÐèÒªÉèÖÃNginxÒÔÖ§³ÖHTTP/2ÐÒé¡£·¿ªNginxµÄÉèÖÃÎļþ£¬¿ÉÒÔÔÚÒÔÏÂλÖÃÕÒµ½£º
sudo nano /etc/nginx/nginx.conf
µÇ¼ºó¸´ÖÆ
ÕÒµ½server¿éÖеÄlistenÖ¸Á²¢Ìí¼Óhttp2×÷Ϊ²ÎÊý£¬ÈçÏÂËùʾ£º
server { listen 443 ssl http2; ... }
µÇ¼ºó¸´ÖÆ
×¢ÖØ£¬ÕâÀï¼ÙÉèÄúµÄÍøվʹÓÃÁËSSLÖ¤ÊéÇÒʹÓÃĬÈϵÄ443¶Ë¿Ú¡£ÈôÊÇÄúµÄÍøվûÓÐÆôÓÃSSL»òʹÓòî±ðµÄ¶Ë¿Ú£¬ÇëÏìÓ¦µØµ÷½âÉèÖá£
ÉúÑIJ¢¹Ø±ÕÎļþºó£¬ÖØÐÂÆô¶¯NginxÒÔʹ¸ü¸ÄÉúЧ£º
sudo systemctl restart nginx
µÇ¼ºó¸´ÖÆ
ÉèÖÃSSLÖ¤Ê飺
ΪÁËʹÓÃHTTP/2ÐÒ飬ÎÒÃÇÐèÒªÉèÖÃSSLÖ¤Êé¡£ÔÚ´ËʾÀýÖУ¬ÎÒÃǽ«Ê¹ÓÃ×ÔÊðÃûÖ¤Êé¡£ÔÚÉú²úÇéÐÎÖУ¬Ó¦Ê¹ÓÃÊÜÐÅÍеÄÖ¤Êé½ÒÏþ»ú¹¹ÊðÃûµÄÖ¤Êé¡£
Ê×ÏÈ£¬½¨ÉèÒ»¸öÓÃÓÚ´æ´¢SSLÖ¤ÊéµÄĿ¼£º
sudo mkdir /etc/nginx/ssl
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÌìÉú˽ԿºÍÖ¤ÊéÇëÇó£º
sudo openssl req -new -newkey rsa:2048 -nodes -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.csr
µÇ¼ºó¸´ÖÆ
ƾ֤ÌáÐѾÙÐÐÏà¹ØÐÅÏ¢µÄÌîд£¬ÀýÈç¹ú¼ÒÂë¡¢×éÖ¯Ãû³ÆµÈ¡£
½ÓÏÂÀ´£¬×ÔÊðÃûÖ¤Ê飺
sudo openssl x509 -req -days 365 -in /etc/nginx/ssl/nginx.csr -signkey /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt
µÇ¼ºó¸´ÖÆ
ÉèÖÃNginxÒÔʹÓÃSSLÖ¤Êé¡£·¿ªNginxµÄÉèÖÃÎļþ£¬²¢ÕÒµ½ÒÔÏÂÐУº
server { ... # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; ... }
µÇ¼ºó¸´ÖÆ
×÷·Ï×¢ÊÍlistenÖ¸ÁͬʱÌí¼ÓSSLÖ¤ÊéµÄ·¾¶ºÍ˽ԿµÄ·¾¶£º
server { ... # SSL configuration # listen 443 ssl http2; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; ... }
µÇ¼ºó¸´ÖÆ
ÉúÑIJ¢¹Ø±ÕÎļþºó£¬ÖØÐÂÆô¶¯NginxÒÔʹ¸ü¸ÄÉúЧ¡£
²âÊÔHTTP/2ÐÒéÖ§³Ö£º
ΪÁËÑéÖ¤ÊÇ·ñÀÖ³ÉÆôÓÃÁËHTTP/2ÐÒ飬Çë·¿ªä¯ÀÀÆ÷²¢»á¼ûÄúµÄÍøÕ¾¡£ÔÚä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ßÖУ¬Éó²éNetworkÑ¡Ï£¬¿ÉÒÔ¿´µ½ÇëÇóµÄÐÒéΪHTTP/2¡£
´úÂëʾÀý£º
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£¬ÆäÖаüÀ¨ÁËHTTP/2ÐÒéµÄÖ§³Ö£º
server { listen 443 ssl http2; server_name example.com; ssl_certificate /etc/nginx/ssl/nginx.crt; ssl_certificate_key /etc/nginx/ssl/nginx.key; location / { root /var/www/html; index index.html; } }
µÇ¼ºó¸´ÖÆ
½áÂÛ£º
ͨ¹ýʹÓÃNginxÉèÖÃÖ§³ÖHTTP/2ÐÒ飬ÎÒÃÇÄܹ»ÌáÉýÍøÕ¾µÄÐÔÄܺÍЧÂÊ¡£Í¨¹ý×ñÕÕ±¾ÎÄÖеİ취£¬Äú¿ÉÒÔÔÚÄúµÄÍøÕ¾ÉÏÇáËÉʵÏÖHTTP/2ÐÒéµÄÖ§³Ö¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃnginxʵÏÖhttp/2ÐÒéÖ§³ÖµÄÏÈÈݺÍʾÀý´úÂ룬ϣÍû¶ÔÄúÓÐËù×ÊÖú£¡
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginxʵÏÖHTTP/2ÐÒéÖ§³ÖµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡