升級你的hexo爲https

本文以Debian 8爲服務器栗子

最近升級了我的博客爲https協議,寫一個詳細的教程幫助更多人升級https。html

https的詳細原理在此文中省略,簡略來講,既是客戶端在訪問服務器時須要一個數字證書(裏面包括公鑰),它由權威機構(CA, Certificate Authority)頒發,來確認公鑰確實是服務器發出來的。nginx

證書價格不菲,大概7天須要幾美金到幾百美金不等(根據認證的等級和域名覆蓋範圍區分),因此不少證書都是商業級的,提供給商業網站使用。git

獲取免費證書

那麼我的將沒法獲取數字證書麼?不是的,爲了鼓勵https的普及,EFF成立了免費證書最大的提供商爲Let’s Encrypt,能夠提供免費證書。那麼小型的網站,就可使用免費證書升級爲https啦。github

固然Let’s Encrypt生成的證書,只能是單域名的,並且只有最低級的域名驗證。web

克隆letsencrypt客戶端

$ git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
若是遇到權限問題,記得先建立/opt/letsencrypt文件夾再,更改文件夾權限爲可寫入。

註冊證書

Nginx指向靜態路徑註冊證書

註冊一個域名證書很是簡單,使用letsencrypt就能生成https所需的證書。固然,用letsencrypt生成的證書只支持域名驗證,只須要用letsenctypt的自動註冊證書命令,證實這個域名是本身的是用的便可。shell

$ cd /opt/letsencrypt
$ ./letsencrypt-auto certonly -a webroot --webroot-path=/var/www/me -d me.chanchun.com.cn # 可使用多個 -d 添加多個域名

letsencrypt能夠幫咱們自動註冊證書,--webroot-path是靜態資源所指的路徑。-d是域名域名,也能夠多個-d增長多個域名。最後確保使用https的域名都被letsencrypt註冊。服務器

後續將會讓你繼續輸入郵箱信息session

若是出現Congratulations!字樣,則證實證書已被自動註冊。app

Nginx轉發型註冊證書

若是是使用Nginx直接重定向服務器本地服務,非靜態資源,就省略--webroot-path-a參數。網站

$ cd /opt/letsencrypt
$ ./letsencrypt-auto certonly -d ca.chanchun.com.cn

輸入命令以後

How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Nginx Web Server plugin (nginx)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):

選擇1便可,後續將會讓你繼續輸入郵箱信息

若是出現Congratulations!字樣,則證實證書已被自動註冊。

Nginx配置

直到這一步,證書已經就緒,只要配置好Nginx便可完美升級https。Nginx安裝、啓動教程請具體請教Nginx教程。

轉發到本地服務Nginx配置Sample:

#http重定向到https配置 證書驗證配置
server {
    listen 80;
    server_name ca.chanchun.com.cn; # 這裏寫你的域名
    location ^~ /.well-known/acme-challenge/ { # 不要修改 letsencrypt須要驗證你的域名
        default_type "text/plain";
        proxy_pass http://localhost:3000; # 填寫你須要轉發的服務器地址
    }
    location = /.well-known/acme-challenge/ { # 不要修改 letsencrypt須要驗證你的域名
        return 404;
    }
    return 301 https://$server_name$request_uri;
}

# https配置
server {
    # SSL Configuration
    listen 443 ssl;
    server_name ca.chanchun.com.cn; # 這裏寫你的域名

    # copy from https://cipherli.st
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off; # Requires nginx >= 1.5.9
    ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;

    # specify cert files
    ssl_certificate /etc/letsencrypt/live/ca.chanchun.com.cn/fullchain.pem; # 中間填寫你的域名
    ssl_certificate_key /etc/letsencrypt/live/ca.chanchun.com.cn/privkey.pem; # 中間填寫你的域名
    location / {
        proxy_pass http://localhost:3000; # 填寫你須要轉發的服務器地址
    }
}

指向靜態文件Nginx配置Sample:

server {
    listen 80;
    server_name me.chanchun.com.cn; # 這裏寫你的域名
    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /var/www/me; # 這裏寫你的靜態文件目錄
    }
    location = /.well-known/acme-challenge/ {
        return 404;
    }
    return 301 https://$server_name$request_uri;
}

server {
    # SSL Configuration
    listen 443 ssl;
    server_name me.chanchun.com.cn;  # 這裏寫你的域名
   # specify cert files
    ssl_certificate /etc/letsencrypt/live/me.chanchun.com.cn/fullchain.pem;  # 中間寫你的域名
    ssl_certificate_key /etc/letsencrypt/live/me.chanchun.com.cn/privkey.pem;  # 中間寫你的域名
    location / {
        root  /var/www/me; # 這裏寫你的靜態文件目錄
        index index.html index.htm; # 這裏寫你暴露的靜態文件
    }
}

Nginx配置各有各的配置方法,這裏只要保證四點:

  • 域名配置正確
  • 靜態文件目錄路徑、本地服務目錄路徑配置正確
  • .well-known/acme-challenge目錄配置正確
  • 要保證80默認端口和443ssl端口都有配置

若是配置好Nginx,那麼訪問網站就會有綠色的小鑰匙啦,說明你的https站點搭建好。

自動更新證書

letsencrypt證書最多隻有90天,90天以後咱們須要從新註冊證書,固然這個能夠交給服務器本身作啦。

驗證本身的證書是否能夠更新

$ cd /opt/letsencrypt
$ ./letsencrypt-auto renew --dry-run
此命令只是驗證 不會更新證書

若是出現Congratulations!字樣或者已經更新字樣則證實能夠自動更新。若是出現錯誤,或者說路徑找不到的狀況,大多數狀況是.well-known/acme-challenge目錄配置沒有正確的配置成功

編寫crontab腳本

$ crontab -e

腳本內容

30 2 * * 1 /opt/letsencrypt/letsencrypt-auto renew >> /var/log/le-renew.log
若是遇到權限問題,可先建立/var/log目錄再設置其權限爲可寫入

成功

享受https的綠色小鎖吧!

相關文章
相關標籤/搜索