ÔõÑùʹÓÃNginx¾ÙÐÐSSLÖ¤ÊéµÄ¶¯Ì¬¼ÓÔغ͸üÐÂ
ÔõÑùʹÓÃnginx¾ÙÐÐsslÖ¤ÊéµÄ¶¯Ì¬¼ÓÔغ͸üÐÂ
¸ÅÊö£º
ÔÚÏÖ´ú»¥ÁªÍøµÄÇéÐÎÖУ¬±£»¤Óû§Êý¾ÝµÄÇå¾²ÐÔÖÁ¹ØÖ÷Òª¡£ÆäÖУ¬Ê¹ÓÃSSL/TLSÖ¤Êé¶ÔWebЧÀÍÆ÷¾ÙÐмÓÃÜͨѶÊÇÒ»ÖÖ³£¼ûµÄ·½·¨¡£È»¶ø£¬¹Å°åµÄ·½·¨ÐèÒªÊÖ¶¯ÐÞ¸ÄNginxÉèÖÃÎļþ²¢ÖØмÓÔØЧÀÍÆ÷£¬ÕâÑù»áµ¼ÖÂÍøÕ¾ÔÚÖ¤Êé¸üÐÂʱ·ºÆðÍ£»úʱ¼ä¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃNginxÄ£¿éºÍ¾ç±¾ÊµÏÖSSLÖ¤ÊéµÄ¶¯Ì¬¼ÓÔغ͸üУ¬ÒÔÌá¸ßÍøÕ¾µÄÎȹÌÐԺͿÉÓÃÐÔ¡£
ʵÏÖÀú³Ì£º
×°ÖÃÐëÒªµÄÒÀÀµ£º
Ê×ÏÈ£¬È·±£Ð§ÀÍÆ÷ÉÏÒѾװÖÃÁËNginxºÍOpenSSL¡£ÁíÍ⣬»¹ÐèҪװÖÃLuaJIT¿ª·¢°ü.
sudo apt-get install nginx openssl libluajit-5.1-dev
µÇ¼ºó¸´ÖÆ
½¨ÉèÖ¤Êé´æ´¢Ä¿Â¼£º
ÔÚЧÀÍÆ÷ÉϽ¨ÉèÒ»¸öĿ¼À´´æ´¢SSLÖ¤ÊéºÍÃÜÔ¿Îļþ¡£
sudo mkdir -p /etc/nginx/ssl
µÇ¼ºó¸´ÖÆ
½¨ÉèLua¾ç±¾£º
½¨ÉèÒ»¸öLua¾ç±¾£¬ÓÃÓÚ¶¯Ì¬¼ÓÔغ͸üÐÂSSLÖ¤Ê顣н¨Ò»¸öÃûΪ “ssl_cert_updater.lua” µÄÎļþ£¬²¢Ìí¼ÓÒÔÏ´úÂ룺
local ssl_cert_path = "/etc/nginx/ssl/cert.pem" local ssl_key_path = "/etc/nginx/ssl/key.pem" local function update_ssl_cert() -- ´ÓÔ¶³ÌЧÀÍÆ÷ÏÂÔØ×îеÄSSLÖ¤ÊéÎļþºÍÃÜÔ¿Îļþ£¬²¢ÉúÑĵ½Ö¸¶¨Â·¾¶ os.execute("wget -O " .. ssl_cert_path .. " https://example.com/cert.pem") os.execute("wget -O " .. ssl_key_path .. " https://example.com/key.pem") -- ÖØмÓÔØNginxÉèÖÃÎļþ os.execute("nginx -s reload") end update_ssl_cert()
µÇ¼ºó¸´ÖÆ
¸üÐÂNginxÉèÖÃÎļþ£º
±à¼NginxÉèÖÃÎļþ£¬Ìí¼ÓLua¾ç±¾µÄÈë¿Ú¡£·¿ªÄ¬ÈϵÄNginxÉèÖÃÎļþ “/etc/nginx/nginx.conf”£¬ÕÒµ½ “http” Ä£¿éµÄλÖ㬲¢ÔÚÆäÖÐÌí¼ÓÒÔÏ´úÂ룺
lua_shared_dict ssl_cert_cache 10m; lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 3; init_by_lua_block { require "ssl_cert_updater" }
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂ뽫¼ÓÔØLua¾ç±¾²¢¾ÙÐгõʼ»¯¡£
ÉèÖÃ׼ʱʹÃü£º
ʹÓÃCrontab»òÆäËû׼ʱʹÃü¹¤¾ßÀ´°´ÆÚÖ´ÐÐLua¾ç±¾¡£±à¼CrontabÎļþ£º
crontab -e
µÇ¼ºó¸´ÖÆ
Ìí¼ÓÒÔÏÂÐУ¨Ê¾ÀýΪÿÖÜÒ»µÄÆÆÏþ2µãÖ´ÐУ©£º
0 2 * * 1 lua /path/to/ssl_cert_updater.lua
µÇ¼ºó¸´ÖÆ
²âÊÔ¸üУº
ÏÖÔÚ£¬Äã¿ÉÒÔÊÖ¶¯ÔËÐÐLua¾ç±¾ÒÔ¼ì²é¸üÐÂÊÇ·ñÕý³£ÊÂÇ飺
lua /path/to/ssl_cert_updater.lua
µÇ¼ºó¸´ÖÆ
×ܽ᣺
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇÀֳɵØʵÏÖÁËNginxµÄSSLÖ¤ÊéµÄ¶¯Ì¬¼ÓÔغ͸üС£Ã¿µ±Ö¤ÊéÓâÆÚ»òÐèÒª¸üÐÂʱ£¬¾ç±¾½«×Ô¶¯ÏÂÔØ×îеÄÖ¤ÊéÎļþ²¢ÖØмÓÔØNginxЧÀÍÆ÷¡£ÕâÑù¿ÉÒÔ×èÖ¹ÍøÕ¾µÄÍ£»úʱ¼ä£¬²¢°ü¹ÜÓû§Êý¾ÝµÄÇå¾²ÐÔ¡£Í¨¹ýʹÓÃLua¾ç±¾ºÍ׼ʱʹÃü£¬ÎÒÃÇÄܹ»ÊµÏÖ×Ô¶¯»¯µÄÖ¤Êé¸üУ¬²¢Ìá¸ßÍøÕ¾µÄÎȹÌÐԺͿÉÓÃÐÔ¡£
Çë×¢ÖØ£¬±¾ÎĽöÌṩ»ù±¾µÄʾÀý£¬ÏêϸµÄʵÑé¼Æ»®¿ÉÒÔƾ֤ÏÖʵÐèÇó½øÒ»²½ÓÅ»¯¡£Í¬Ê±£¬È·°ü¹ÜÊéÏÂÔصÄÔ´ÍøÖ·¿É¿¿£¬²¢¶ÔЧÀÍÆ÷µÄÇå¾²ÐÔ¾ÙÐÐÊʵ±µÄÆÀ¹ÀºÍ±£»¤¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃNginx¾ÙÐÐSSLÖ¤ÊéµÄ¶¯Ì¬¼ÓÔغ͸üеÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡