ÆÊÎöNginxµÄ·´ÏòÊðÀíЧÀÍÆ÷µÄSSL¼ÓÃܺÍÖ¤ÊéÖÎÀíʵÏÖϸ½Ú
ÆÊÎönginxµÄ·´ÏòÊðÀíЧÀÍÆ÷µÄssl¼ÓÃܺÍÖ¤ÊéÖÎÀíʵÏÖϸ½Ú
´ÓÍøÂçÇå¾²µÄ½Ç¶ÈÀ´¿´£¬ÎªWebЧÀÍÆ÷ÉèÖÃSSL¼ÓÃܺÍÖ¤ÊéÖÎÀíÊÇÖÁ¹ØÖ÷ÒªµÄ¡£±¾ÎĽ«ÆÊÎöNginx·´ÏòÊðÀíЧÀÍÆ÷ÔÚSSL¼ÓÃܺÍÖ¤ÊéÖÎÀí·½ÃæµÄʵÏÖϸ½Ú¡£ÎÒÃǽ«Ì½ÌÖÈç×÷ÉõNginxÉèÖÃSSLÖ¤Ê飬ÒÔ¼°ÔõÑùʵÏÖHTTPSÐÒéµÄÇ徲ͨѶ¡£
Ò»¡¢NginxµÄSSLÉèÖÃ
Ê×ÏÈ£¬È·±£ÒѾÔÚNginxЧÀÍÆ÷ÉÏ׼ȷװÖÃÁËOpenSSL¿â¡£È»ºó£¬ÔÚNginxµÄÉèÖÃÎļþÖÐÕÒµ½ÐèÒª¾ÙÐÐSSLÉèÖõÄserver¿é£¬²¢ÔÚÆäÖÐÌí¼ÓÒÔÏ´úÂ룺
server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/ssl/certificate.crt; # SSLÖ¤Êé·¾¶ ssl_certificate_key /path/to/ssl/private.key; # SSLÖ¤Êé˽Կ·¾¶ ssl_protocols TLSv1.2 TLSv1.3; # Ö§³ÖµÄSSLÐÒé°æ±¾ ssl_ciphers HIGH:!aNULL:!MD5; # Ö§³ÖµÄ¼ÓÃÜËã·¨ ssl_prefer_server_ciphers on; # ÓÅÏÈʹÓÃЧÀÍÆ÷¶ËµÄ¼ÓÃÜËã·¨ location / { proxy_set_header Host $host; proxy_pass http://backend_server; } }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬¡°ssl_certificate¡±²ÎÊýÖ¸¶¨SSLÖ¤ÊéµÄ·¾¶£¬¡°ssl_certificate_key¡±²ÎÊýÖ¸¶¨SSLÖ¤ÊéµÄ˽Կ·¾¶¡£Í¬Ê±£¬ÎÒÃÇ¿ÉÒÔʹÓá°ssl_protocols¡±ºÍ¡°ssl_ciphers¡±²ÎÊýÀ´Ö¸¶¨ÔÊÐíµÄSSLÐÒé°æ±¾ºÍ¼ÓÃÜËã·¨¡£
¶þ¡¢SSLÖ¤ÊéÖÎÀí
¾ÓÉÉÏÊöÉèÖúó£¬ÎÒÃÇ»¹ÐèÒªÏàʶÔõÑùÖÎÀíSSLÖ¤Ê飬°üÀ¨ÌìÉú×ÔÊðÃûÖ¤Êé¡¢¹ºÖÃÉÌÒµÖ¤ÊéÒÔ¼°¸üÐÂÖ¤Êé¡£
ÌìÉú×ÔÊðÃûÖ¤Êé
×ÔÊðÃûÖ¤Ê飬¼´Ã»Óб»È¨ÍþÖ¤Êé»ú¹¹ÐÅÍеÄÖ¤Ê飬ÊÊÓÃÓÚ²âÊÔÇéÐκÍÄÚ²¿Ê¹Óá£ÎÒÃÇ¿ÉÒÔʹÓÃOpenSSLÏÂÁîÌìÉú×ÔÊðÃûÖ¤Ê飺
openssl req -newkey rsa:2048 -nodes -keyout private.key -x509 -days 365 -out certificate.crt
µÇ¼ºó¸´ÖÆ
¹ºÖÃÉÌÒµÖ¤Êé
ÉÌÒµÖ¤ÊéÓÉÊÜÐÅÍеĵÚÈý·½Ö¤Êé»ú¹¹½ÒÏþ£¬ÓÐÓÃÆÚͨ³£Îª1-2Äê¡£¹ºÖÃÉÌÒµÖ¤ÊéÐèÒªÌṩÏà¹ØµÄÉí·ÝÑéÖ¤ÖÊÁÏ£¬²¢Æ¾Ö¤Ö¤Êé»ú¹¹µÄÒªÇó¾ÙÐÐÉêÇë¡£
ÔÚ»ñÈ¡ÉÌÒµÖ¤Êéºó£¬½«Ö¤ÊéºÍ˽ԿÎļþÉÏ´«µ½NginxЧÀÍÆ÷£¬²¢ÔÚÉèÖÃÎļþÖÐÖ¸¶¨Æä·¾¶¡£
¸üÐÂÖ¤Êé
Ö¤ÊéÔÚµ½ÆÚ֮ǰÐèҪʵʱ¸üУ¬ÒÔÈ·±£Çå¾²ÐÔ¡£Í¨³££¬Ö¤Êé»ú¹¹»áÌṩ¸üÐÂÖ¤ÊéµÄÁ÷³ÌºÍ°ì·¨¡£ÎÒÃÇÐèҪƾ֤¸ÃÁ÷³Ì£¬»ñȡеÄÖ¤ÊéºÍ˽ԿÎļþ£¬²¢Ìæ»»ÏÖÓеÄÖ¤ÊéÎļþ¡£
Èý¡¢SSL»Ø»°»º´æ
SSLͨѶÔÚ½¨ÉèÅþÁ¬Ê±ÐèÒª¾ÙÐмÓÃÜÏ¢ÕùÃܲÙ×÷£¬Õâ»áÏûºÄЧÀÍÆ÷µÄÅÌËã×ÊÔ´¡£ÎªÁËÌá¸ßÐÔÄÜ£¬NginxÌṩÁËSSL»á»°»º´æ»úÖÆ¡£
ÔÚNginxÉèÖÃÎļþµÄ¡°http¡±¿éÖÐÌí¼ÓÒÔÏ´úÂ룬¿ªÆôSSL»á»°»º´æ£º
http { ssl_session_cache shared:SSL:50m; ssl_session_timeout 5m; }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖУ¬¡°ssl_session_cache¡±²ÎÊýÖ¸¶¨SSL»á»°»º´æµÄÀàÐͺ;Þϸ£¬¡°ssl_session_timeout¡±²ÎÊýÖ¸¶¨»á»°µÄ³¬Ê±Ê±¼ä¡£
ËÄ¡¢HTTPSÖض¨Ïò
ΪÁËÇ¿ÖÆʹÓÃHTTPSÐÒé¾ÙÐлá¼û£¬ÎÒÃÇ¿ÉÒÔÔÚNginxµÄÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룬ʵÏÖHTTPÇëÇóµÄ×Ô¶¯Öض¨Ïòµ½HTTPS£º
server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
µÇ¼ºó¸´ÖÆ
Îå¡¢×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËNginx·´ÏòÊðÀíЧÀÍÆ÷ÔÚSSL¼ÓÃܺÍÖ¤ÊéÖÎÀí·½ÃæµÄʵÏÖϸ½Ú¡£ÉèÖÃSSLÖ¤ÊéºÍ¼ÓÃÜËã·¨¡¢ÖÎÀíSSLÖ¤ÊéºÍ˽ԿÎļþ¡¢¿ªÆôSSL»á»°»º´æÒÔ¼°ÊµÏÖHTTPµ½HTTPSµÄÖض¨Ïò£¬ÊÇÈ·±£Ð§ÀÍÆ÷Çå¾²ÐÔµÄÖ÷Òª°ì·¨¡£
×¢£ºÒÔÉÏÖ»ÊǶÔNginx·´ÏòÊðÀíЧÀÍÆ÷µÄSSL¼ÓÃܺÍÖ¤ÊéÖÎÀíʵÏÖϸ½ÚµÄ¼òÒªÏÈÈÝ£¬ÏÖʵµÄÉèÖúÍÖÎÀí¿ÉÄÜÒò²î±ðµÄЧÀÍÆ÷ºÍÐèÇó¶øÓÐËù²î±ð¡£ÔÚʵ¼ùÖУ¬Çë²Î¿¼¹Ù·½ÎĵµºÍÏà¹Ø×ÊÁÏ£¬²¢Æ¾Ö¤ÏêϸÇéÐξÙÐÐÉèÖúÍÖÎÀí¡£
ÒÔÉϾÍÊÇÆÊÎöNginxµÄ·´ÏòÊðÀíЧÀÍÆ÷µÄSSL¼ÓÃܺÍÖ¤ÊéÖÎÀíʵÏÖϸ½ÚµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡