若是你使用ELB來作負載均衡,在AWS上能夠很方便的使用SSL。若是不使用ELB就須要本身來配置SSL。
Let's encrypt 提供期限爲三個月的免費SSL證書,到期以後須要renew,官方還提供自動renew的工具certbothtml
certbot 是一個自動申請和續期SSL證書的工具。在官網certbot.eff.org能夠找到各類OS和Web服務器下的安裝方法。常見的Ubuntu和CentOS安裝起來十分方便。linux
在AWS EC2上,官方推薦的是OS是Amazon Linux,基於RHEL 6源碼從新編譯的,提供了Amazon本身的工具和源。certbot的安裝方式相似於RHEL 6/CentOS 6nginx
ssh到Serverweb
下載certbotchrome
wget https://dl.eff.org/certbot-auto chmod a+x certbot-auto
執行certbotsegmentfault
sudo ./certbot-auto --debug -v --server https://acme-v01.api.letsencrypt.org/directory certonly -d YOUR_WEBSITE_HERE
驗證api
How would you like to authenticate with the ACME CA? --------------------------- 1: Place files in webroot directory (webroot) 2: Spin up a temporary webserver (standalone) ---------------------------
選擇1certbot會把一個驗證文件放到webroot下,因此須要配置一下nginx的默認靜態目錄
選擇2certbot會啓動一個web服務,佔用443端口,因此須要暫停一下nginx,通常狀況下選擇2比較省事。安全
記得在AWS EC2的安全組中放開443端口
服務器
證書路徑session
Certificate: /etc/letsencrypt/live/YOUR_WEBSITE_HERE/cert.pem Full Chain: /etc/letsencrypt/live/YOUR_WEBSITE_HERE/fullchain.pem Private Key: /etc/letsencrypt/live/YOUR_WEBSITE_HERE/privkey.pem
啓用SSL以後,http須要默認跳轉到https,還有SSL證書的配置,下面是個配置的例子
server { listen 80; server_name YOUR_WEBSITE_HERE; # Redirect all HTTP requests to HTTPS with a 301 Moved Permanently response. return 301 https://YOUR_WEBSITE_HERE$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name YOUR_WEBSITE_HERE; # certs sent to the client in SERVER HELLO are concatenated in ssl_certificate ssl_certificate /etc/letsencrypt/live/YOUR_WEBSITE_HERE/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/YOUR_WEBSITE_HERE/privkey.pem; ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; access_log /var/log/nginx/YOUR_WEBSITE_HERE-access.log; error_log /var/log/nginx/YOUR_WEBSITE_HERE-error.log; location / { proxy_pass http://127.0.0.1:8003; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
使用root用戶
sudo -i
增長定時任務
crontab -e
增長一行,每一個月1號2點30分更新
30 2 1 * * /path/to/certbot-auto renew --pre-hook "service nginx stop" --post-hook "service nginx start"
dry run
./path/to/certbot-auto renew --dry-run
在chrome下須要全站都使用https地址欄纔會變綠,須要檢查一下網站裏面的各類URL,好比外鏈圖片或JS文件,都須要使用https才行。
參考資料: