Nginx HTTP2ÉèÖý̳̣¬ÌáÉýÍøÕ¾»á¼ûËÙÂÊ
nginx http2ÉèÖý̳̣¬ÌáÉýÍøÕ¾»á¼ûËÙÂÊ
¸ÅÊö£º
ÔÚÏÖ´ú»¥ÁªÍøÖУ¬¿ìËÙµÄÍøÕ¾¼ÓÔØËÙÂÊÊÇÎüÒýÓû§µÄÒªº¦ÒªËØÖ®Ò»¡£HTTP/2ÊÇÒ»ÖÖÐÂÒ»´úµÄÍøÂçͨѶÐÒ飬Ëüͨ¹ýÓÅ»¯Êý¾Ý´«ÊäµÄ·½·¨£¬Ê¹µÃÍøÕ¾µÄ¼ÓÔØËÙÂʸü¿ì¡¢ÐÔÄܸü¼Ñ¡£±¾½Ì³Ì½«Ö¸µ¼ÄúÔõÑùʹÓÃNginxЧÀÍÆ÷¾ÙÐÐHTTP/2µÄÉèÖ㬴ӶøÌáÉýÍøÕ¾µÄ»á¼ûËÙÂÊ¡£
°ì·¨Ò»£º×°ÖÃNginx
Ê×ÏÈ£¬ÔÚÄúµÄЧÀÍÆ÷ÉÏ×°ÖÃNginx¡£Æ¾Ö¤ÄúʹÓõIJÙ×÷ϵͳ²î±ð£¬¿ÉÒԲο¼ÏìÓ¦µÄÎĵµ¾ÙÐÐ×°Öá£
°ì·¨¶þ£ºÌìÉúSSLÖ¤Êé
ΪÁËʹÓÃHTTP/2£¬ÄúÐèҪΪÄúµÄÍøÕ¾ÌìÉúÒ»¸öSSLÖ¤Êé¡£Äú¿ÉÒÔʹÓÃLet’s EncryptµÈÃâ·ÑµÄSSLÖ¤Êé½ÒÏþ»ú¹¹£¬Ò²¿ÉÒÔ¹ºÖÃÉÌÒµSSLÖ¤Êé¡£
°ì·¨Èý£ºÉèÖÃNginx
ÕÒµ½ÄúµÄNginxÉèÖÃÎļþ£¬Í¨³£Î»ÓÚ/etc/nginx/nginx.conf»ò/etc/nginx/sites-available/default¡£ÔÚ¸ÃÎļþÖоÙÐÐÈçÏÂÉèÖãº
¿ªÆôHTTP/2Ö§³Ö
ÔÚhttp¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
http {
listen 443 ssl http2; ssl_certificate /path/to/your/ssl/certificate.crt; ssl_certificate_key /path/to/your/ssl/private.key; ...
µÇ¼ºó¸´ÖÆ
}
ÉèÖüÓÃÜÌ×¼þ
ÔÚssl_protocolsºÍssl_ciphersÖÐÌí¼ÓÒÔÏÂÉèÖãº
http {
... ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ...
µÇ¼ºó¸´ÖÆ
}
ÆôÓÃgzipѹËõ
ÔÚserver¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
server {
... gzip on; gzip_types text/plain text/css application/javascript image/svg+xml; ...
µÇ¼ºó¸´ÖÆ
}
ÉèÖûº´æ
ÔÚserver¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
server {
... location ~* .(jpg|jpeg|gif|png|css|js)$ { expires 1y; add_header Cache-Control "public"; } ...
µÇ¼ºó¸´ÖÆ
}
ÉèÖÃÖض¨ÏòHTTPµ½HTTPS
ÔÚserver¿éÖÐÌí¼ÓÒÔÏÂÉèÖãº
server {
... listen 80; server_name yourdomain.com; return 301 https://yourdomain.com$request_uri; ...
µÇ¼ºó¸´ÖÆ
}
ÖØÆôNginx
ÔÚÍê³ÉÒÔÉÏÉèÖúó£¬ÉúÑÄNginxÉèÖÃÎļþ£¬²¢ÖØÐÂÆô¶¯NginxЧÀÍ¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁ
sudo nginx -t # ¼ì²éÉèÖÃÎļþÊÇ·ñÓÐÓï·¨¹ýʧ
sudo systemctl restart nginx # ÖØÆôNginxЧÀÍ
°ì·¨ËÄ£º²âÊÔHTTP/2
ͨ¹ýä¯ÀÀÆ÷»á¼ûÄúµÄÍøÕ¾£¬²¢·¿ªä¯ÀÀÆ÷µÄ¿ª·¢Õß¹¤¾ß¡£ÔÚ”Network”Ñ¡ÏÖУ¬¼ì²éHTTP°æ±¾ÊÇ·ñΪHTTP/2¡£
´úÂëʾÀý£º
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£¬¹©²Î¿¼£º
http {
server { listen 443 ssl http2; server_name yourdomain.com; ssl_certificate /path/to/your/ssl/certificate.crt; ssl_certificate_key /path/to/your/ssl/private.key; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~* .(jpg|jpeg|gif|png|css|js)$ { expires 1y; add_header Cache-Control "public"; } }
µÇ¼ºó¸´ÖÆ
}
½áÂÛ£º
ͨ¹ýÔÚNginxÖÐÆôÓÃHTTP/2£¬²¢¾ÙÐÐһЩÓÅ»¯ÉèÖã¬Äܹ»ÏÔ×ÅÌáÉýÄúÍøÕ¾µÄ»á¼ûËÙÂÊ¡£Í¬Ê±£¬»¹¿ÉÒÔÉèÖûº´æ¡¢ÆôÓÃgzipѹËõµÈ£¬½øÒ»²½¸ÄÉÆÓû§µÄ»á¼ûÌåÑ顣ϣÍû±¾½Ì³Ì¶ÔÄúÓÐËù×ÊÖú£¬×£ÄúµÄÍøÕ¾¸ü¿ì¡¢¸üÁ÷ͨ£¡
ÒÔÉϾÍÊÇNginx HTTP2ÉèÖý̳̣¬ÌáÉýÍøÕ¾»á¼ûËÙÂʵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡