隨着國內各大網站紛紛開啓全站 HTTPS 時代,HTTPS 已再也不是支付等敏感操做過程的專屬,開啓 HTTPS 對於我的網站或者小型網站也再也不高不可攀。 今天博主就以本身的網站 www.rapospectre.com 爲例敘述一下爲本身網站點亮 HTTPS 小綠鎖的過程。html
HTTPS( Hypertext Transfer Protocol over Secure Socket Layer ),是以安全爲目標的 HTTP 通道,簡單講是 HTTP 的安全版。即 HTTP 下加入 SSL 層,HTTPS 的安全基礎是 SSL ,所以加密的詳細內容就須要 SSL 。 它是一個 URI scheme( 抽象標識符體系 ),句法類同 http :體系。用於安全的 HTTP 數據傳輸。 https:URL 代表它使用了 HTTP,但 HTTPS 存在不一樣於 HTTP 的默認端口及一個加密/身份驗證層(在 HTTP 與 TCP 之間)。這個系統的最初研發由網景公司進行,提供了身份驗證與加密通信方法,如今它被普遍用於萬維網上安全敏感的通信,例如交易支付方面。jquery
HTTP 超文本傳輸協議 ( HTTP-Hypertext transfer protocol ) 是一種詳細規定了瀏覽器和萬維網服務器之間互相通訊的規則,經過因特網傳送萬維網文檔的數據傳送協議。nginx
從概念裏能夠看到,要開啓 HTTPS 相當重要的一點就是 ssl 層的身份驗證,而身份驗證須要用到 ssl 證書,之前少有免費 ssl 證書,因此小站基本不會選擇 https ,而如今網上提供我的免費 ssl 證書的機構愈來愈多,這使得免費升級站點爲 https 成爲可能。git
網上已經有很多機構提供我的免費 ssl 證書,有效期幾個月到幾年不等,博主使用的是 StartSSL, 申請成功後有效期 3 年,到期後可免費續租。 具體申請過程不復雜,註冊後根據提示驗證網站 + 生成證書便可,若是不清楚能夠 Google 一下。github
要注意 StartSSL 驗證網站擁有者時是給域名全部者的郵箱發驗證郵件,若是域名開啓了隱私保護請暫時關閉。shell
而後在本身服務器中生成 SSL 證書的 csr ,記住生成輸入的祕密,以後要用到:ubuntu
openssl req -new -sha256 -key rapospectre.com_secure.key -out rapospectre.com.csr
假設以上文件生成在 /var/tmp
文件夾下瀏覽器
在 StartSSL 填寫 csr 文件內容,生成 SSL 證書並下載, 生成成果後如圖:安全
點擊 Retrieve 下載證書,解壓縮後包含各類服務器的 crt ,博主使用 nginx 作反代,因此選擇 nginxserver 解壓縮後獲得 www.rapospectre.com_bundle.crt
將此文件上傳到服務器,假設傳到 /var/tmp/
文件夾服務器
以 nginx 爲例,打開 /etc/nginx/nginx.conf
,加入配置:
server { listen 443 ssl; ssl_certificate /var/tmp/www.rapospectre.com_bundle.crt; ssl_certificate_key /var/tmp/rapospectre.com_secure.key; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #選擇特定的加密方式, 避免已知的漏洞 ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED'; #讓瀏覽器記住直接訪問 https 的網址, 再也不去 http 重定向。 add_header Strict-Transport-Security 'max-age=31536000; preload'; add_header X-Frame-Options DENY; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; keepalive_timeout 70; ssl_dhparam /var/tmp/dhparam2048.pem; #禁止服務器自動解析資源類型 add_header X-Content-Type-Options nosniff; #防XSS攻擊 add_header X-Xss-Protection 1; server_name www.rapospectre.com rapospectre.com;
在以前的 80 端口進行重定向配置:
server { listen 80; server_name rapospectre.com www.rapospectre.com; return 301 https://www.rapospectre.com$request_uri; }
將網站全部以 http 方式獲取的資源所有改成 https 方式或自動方式獲取, eg:
<script src="http://xx.cdn.com/jquery.js"></script> 改成 <script src="https://xx.cdn.com/jquery.js"></script> 或 <script src="//xx.cdn.com/jquery.js"></script>
重啓服務器,提示輸入以前生成 csr 的密碼,輸入密碼,重啓成功,訪問 https://www.rapospectre.com 能夠看到 HTTPS 已經正常工做!
順手來一發 SSLLABS測試,wtf 只有 F?
看圖發現由於
This server is vulnerable to the OpenSSL Padding Oracle vulunerability ( CVE-2016-2107 )
原來是 OpenSSL 漏洞的鍋,升級 OpenSSL 到 1.0.2h 版 ( 後續版本應該也能夠,博主一開始升級到了最新的 1.1.0a 結果服務器掛了 ) 便可修復漏洞:
Fix OpenSSL Padding Oracle vulnerability (CVE-2016-2107) - Ubuntu 14.04
# Based on http://fearby.com/article/update-openssl-on-a-digital-ocean-vm/ $ apt-get update $ apt-get dist-upgrade $ wget ftp://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2h.tar.gz $ tar -xvzf openssl-1.0.2h.tar.gz $ cd openssl-1.0.2h $ ./config --prefix=/usr/ $ make depend $ sudo make install $ openssl version # OpenSSL 1.0.2h 3 May 2016 # now restart your nginx or other server $ nginx -s reload
開啓 http2 ,nginx 在 1.9.5 之後的版本纔開始支持 http2 ,以前一直使用的是 spdy 而 ubuntu 自帶的 nginx 是 1.4.6 的古董, 因此須要從新編譯安裝新版的 nginx ,博主選擇了安裝最新的 nginx 1.11.4:
1. 下載 nginx 到 /var/tmp/nginx
:
wget http://nginx.org/download/nginx-1.11.4.tar.gz
2. 解壓nginx-1.11.4.tar.gz文件
tar zxvf nginx-1.11.4.tar.gz
3. 進入ngixn-1.11.4文件夾
cd nginx-1.2.5
4. 查看nginx原來的配置
nginx -V
上面的命令將輸出相似以下信息:
--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module
咱們在後面加上 http2 模塊與 上一步中 openssl 源碼( 是源碼路徑不是安裝 )路徑:
--with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h
注意,若是以上信息內包含 --with-spdy_module
請去除,nginx 1.9.5 以後已棄用 spdy
5. 執行configure命令,後面跟上原來nginx的配置
./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h
configure時可能遇到的幾個錯誤:
--with-http_xslt_module 時提示 the HTTP XSLT module requires the libxml2/libxslt libraries
apt-get install libxml2 libxml2-dev libxslt-dev
--with-http_image_filter_module 時提示 the HTTP image filter module requires the GD library.
apt-get install libgd2-xpm-dev
--with-http_geoip_module 時提示 the GeoIP module requires the GeoIP library.
apt-get install geoip-database libgeoip-dev
./configure: error: the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev
再次執行 configure 命令, 而後make && make install
。 編譯好之後objs目錄下多出一個nginx文件,用它替換舊的 nginx 文件:
mv /usr/sbin/nginx /usr/sbin/nginx-backup cp objs/nginx /usr/sbin/nginx
執行/usr/sbin/nginx -t 命令檢查配置文件返回下面的信息:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
表示 nginx 升級成功,修改 nginx 配置,加入 http2 支持:
listen 443 ssl http2 fastopen=3 reuseport;
重啓 nginx 訪問正常後再測一發:
搞定,我的網站加入 HTTPS 而且 SSLABS 評分 A+ 。 快來試試吧~
( 博主網站圖片上傳到七牛,而七牛免費彷佛帳戶不支持 https 連接,因此有些文章好比說這篇會提示網頁內有不安全的內容 )
原文地址:https://www.rapospectre.com/b...
做者:rapospectre