我習慣喺隻 web server 之前放一隻 reverse proxy,而哩隻 reverse proxy 我通常係用 nginx,依家要做嘅嘢係:喺隻 nginx 加 SSL 俾不同嘅 websites (即係 multiple domains),而隻 SSL cert 就用 Let’s Encrypt 提供嘅免費服務。Let’s Encrypt 係免費嘅(有啲業界認為佢係搞破壞,我就舉手歡迎 😜),唯一嘅問題係每 90 天就要 renew 一次。
我哋嚟睇下點做:
以下係啲 Scripts:
攞 cert:
root@inst-2-nginx:/etc/nginx/certbot# ./certbot-auto certonly --standalone -d jb5.marche.com.hk Saving debug log to /var/log/letsencrypt/letsencrypt.log Obtaining a new certificate Performing the following challenges: tls-sni-01 challenge for jb5.marche.com.hk Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/jb5.marche.com.hk/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/jb5.marche.com.hk/privkey.pem Your cert will expire on 2017-11-15. To obtain a new or tweaked version of this certificate in the future, simply run certbot-auto again. To non-interactively renew *all* of your certificates, run "certbot-auto renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le root@inst-2-nginx:/etc/nginx/certbot#
http conf file:
server { listen 80; server_name jb5.marche.com.hk; #access_log /var/log/nginx/marche.access.log; #error_log /var/log/nginx/marche.error.log debug; location / { proxy_pass http://iis1-apps; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for; proxy_max_temp_file_size 0; client_max_body_size 512m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; #proxy_cache one; #proxy_cache_key $host; #proxy_cache_valid 200 1h; #proxy_cache_use_stale error timeout invalid_header; } }
https conf file:
## Handle http:// connections server { listen 80; server_name jb5.marche.com.hk; return 301 https://$server_name$request_uri; } ## Handle https:// connections server { listen 443 ssl; server_name y5.directoutput.com.hk; access_log /var/log/nginx/jb5.marche.access.log; error_log /var/log/nginx/jb5.marche.error.log debug; ## # SSL Settings ## ssl on; # Set cert locations ssl_certificate /etc/letsencrypt/live/jb5.marche.com.hk/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/jb5.marche.com.hk/privkey.pem; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers RC4:HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; keepalive_timeout 60; location / { ## # Proxy settings ## proxy_pass http://10.240.0.3; # Convert all inbound request into http:// request to upstream server proxy_redirect http:// $scheme://; proxy_set_header Accept-Encoding ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; add_header Front-End-Https on; proxy_max_temp_file_size 0; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; client_max_body_size 512m; client_body_buffer_size 512k; ## # Filter response settings ## # Filter all response content with http://jb5.marche.com.hk into https://jb5.marchet.com.hk sub_filter http://jb5.marche.com.hk/ https://jb5.marche.com.hk/; # Filter not only response type text/html but also text/css and text/xml sub_filter_types text/css text/xml; # Filter all response sub_filter_once off; } }
cron.monthly:
#!/bin/bash /etc/nginx/certbot/certbot-auto renew --text --no-self-upgrade > /var/log/letsencrypt/letsencrypt_cron.log 2>&1 supervisorctl stop nginx supervisorctl start nginx
P.S. 如果 cert 的時候出現 error: can not bind to IPv4 or IPv6,你要先停咗隻 nginx: systemctl stop nginx,cert 完再開番: systemctl start nginx,因為 –standalone 會用自己嘅 web service,同 nginx 有沖突。