ÔõÑùʵÏÖNginxµÄHTTPSÉèÖÃ
ÔõÑùʵÏÖNginxµÄHTTPSÉèÖã¬ÐèÒªÏêϸ´úÂëʾÀý
Ç°ÑÔ
Ëæ×Å»¥ÁªÍøµÄÉú³¤ºÍÇå¾²ÒâʶµÄÌá¸ß£¬Ô½À´Ô½¶àµÄÍøÕ¾×îÏÈÆôÓÃHTTPSÐÒéÀ´±£»¤Óû§µÄÒþ˽ºÍÇå¾²¡£Nginx×÷Ϊһ¸ö¸ßÐÔÄܵĿªÔ´WebЧÀÍÆ÷£¬²»µ«¿ÉÒÔÉèÖÃHTTP£¬»¹¿ÉÒÔÉèÖÃHTTPS¡£±¾ÎĽ«ÏÈÈÝÔõÑùʵÏÖNginxµÄHTTPSÉèÖ㬲¢¸½ÉÏÏêϸµÄ´úÂëʾÀý£¬Àû±ã¸÷È˲ο¼ºÍʹÓá£
Ò»¡¢ÌìÉúÖ¤ÊéºÍ˽Կ
ÒªÆôÓÃHTTPSÐÒ飬Ê×ÏÈÐèÒªÌìÉúSSLÖ¤ÊéºÍ˽Կ¡£SSLÖ¤ÊéÓÃÓÚÑé֤ЧÀÍÆ÷µÄÉí·Ý£¬Ë½Ô¿ÓÃÓÚ¼ÓÃÜÏ¢ÕùÃÜ´«ÊäµÄÊý¾Ý¡£¿ÉÒÔʹÓÃÖÖÖÖÖ¤Êé»ú¹¹£¨CA£©Ç©·¢µÄÖ¤Ê飬Ҳ¿ÉÒÔʹÓÃ×ÔÊðÃûÖ¤Êé¡£
ÌìÉú×ÔÊðÃûÖ¤ÊéµÄÒªÁìÈçÏ£º
·¿ªÖնˣ¬ÔÚÏÂÁîÐÐÖÐÊäÈëÒÔÏÂÏÂÁ
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout private.key -out certificate.crt -subj "/CN=www.example.com"
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬private.keyÊÇ˽ԿÎļþÃû£¬certificate.crtÊÇÖ¤ÊéÎļþÃû£¬www.example.comÊÇЧÀÍÆ÷µÄÓòÃû£¬¿ÉÒÔƾ֤ÏÖÕæÏàÐξÙÐÐÐ޸ġ£
ÊäÈëÉÏÊöÏÂÁîºó£¬½«±»ÒªÇóÊäÈëһЩ֤ÊéÏà¹ØµÄÐÅÏ¢£¬°üÀ¨¹ú¼Ò¡¢ÖÝ¡¢¶¼»á¡¢×éÖ¯µÈ¡£Æ¾Ö¤ÌáÐÑÊäÈë¼´¿É¡£
ÊäÈëÍê±Ïºó£¬»áÔÚÄ¿½ñĿ¼ÏÂÌìÉú˽ԿÎļþprivate.keyºÍÖ¤ÊéÎļþcertificate.crt¡£
¶þ¡¢±à¼NginxÉèÖÃÎļþ
ÌìÉúÖ¤ÊéºÍ˽Կºó£¬ÐèÒª±à¼NginxµÄÉèÖÃÎļþ£¬ÆôÓÃHTTPSÐÒé¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄNginxÉèÖÃʾÀý£º
server { listen 443 ssl; server_name www.example.com; ssl_certificate /path/to/certificate.crt; # Ö¤ÊéÎļþ·¾¶ ssl_certificate_key /path/to/private.key; # ˽ԿÎļþ·¾¶ location / { # ÆäËûÉèÖÃ... } }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬listen 443 ssl;ÌåÏÖʹÓÃHTTPSÐÒ飬¼àÌý443¶Ë¿Ú£»server_name www.example.com;Ö¸¶¨Ð§ÀÍÆ÷µÄÓòÃû£»ssl_certificateºÍssl_certificate_key»®·ÖÖ¸¶¨Ö¤ÊéºÍ˽ԿµÄÎļþ·¾¶¡£
Èý¡¢ÖØÆôNginxЧÀÍ
ÉèÖÃÎļþ±à¼Íê³Éºó£¬ÐèÒªÖØÆôNginxЧÀÍ£¬Ê¹ÉèÖÃÉúЧ¡£ÔÚÖÕ¶ËÖÐÊäÈëÒÔÏÂÏÂÁ
sudo service nginx restart
µÇ¼ºó¸´ÖÆ
ÊäÈëÃÜÂëºó£¬NginxЧÀͽ«»áÖØÆô¡£
ËÄ¡¢ÉèÖÃHTTPÌøתHTTPS
ÈôÊÇÒª½«ËùÓÐHTTPÇëÇó¶¼Ìøתµ½HTTPS£¬¿ÉÒÔÔÚÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
server { listen 80; server_name www.example.com; return 301 https://$server_name$request_uri; }
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬listen 80;ÌåÏÖ¼àÌý80¶Ë¿Ú£¨¼´HTTPÐÒ飩£»return 301 https://$server_name$request_uri;ÌåÏÖ½«ËùÓÐÇëÇóÌøתµ½¶ÔÓ¦µÄHTTPSµØµã¡£
Îå¡¢ÉèÖÃÇ¿ÖÆHTTPS»á¼û
ÈôÊÇҪǿÖÆËùÓÐÇëÇó¶¼Í¨¹ýHTTPSÐÒé¾ÙÐлá¼û£¬¿ÉÒÔÔÚNginxÉèÖÃÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
server { listen 80; server_name www.example.com; return 301 https://$server_name$request_uri; } server { listen 443 ssl; server_name www.example.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private.key; # ÆäËûÉèÖÃ... }
µÇ¼ºó¸´ÖÆ
ÉèÖÃÖеĵÚÒ»¸öserver¿éºÍµÚ¶þ¸öserver¿é»®·Ö¼àÌý80¶Ë¿ÚºÍ443¶Ë¿Ú£¬»®·Ö´¦ÀíHTTPºÍHTTPSÇëÇó¡£ÉèÖÃÖеÄreturn 301 https://$server_name$request_uri;½«HTTPÇëÇóÇ¿ÖÆÌøתµ½¶ÔÓ¦µÄHTTPSµØµã¡£
½áÓï
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔʵÏÖNginxµÄHTTPSÉèÖ㬲¢ÇÒ¿ÉÒÔƾ֤ÐèҪѡÔñÊÇ·ñ¾ÙÐÐHTTPÌøתHTTPS»òÕßÇ¿ÖÆHTTPS»á¼û¡£ËäÈ»£¬ÏÖʵӦÓÃÖл¹Éæ¼°µ½ÆäËûһЩϸ½ÚÉèÖã¬ÈçÉèÖÃSSLÐÒé°æ±¾¡¢¼ÓÃÜÌ×¼þ¡¢HSTSµÈ¡£Ï£Íû±¾ÎÄÄܹ»Îª¸÷ÈËÌṩһЩ²Î¿¼£¬ÈÃÄãµÄÍøÕ¾Ô½·¢Çå¾²¿É¿¿¡£
ÒÔÉϾÍÊÇÔõÑùʵÏÖNginxµÄHTTPSÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡