若是你使用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才行。
参考资料: