用 Nginx 基于 Let's Engypt 免费证书打造快速安全的 HTTPS 网站
我大EOI的官網(wǎng)正式上線啦!為了打造公司第一個正式上線的公開站點,我們著實費了不少心思,其中之一就是如何把它搞得快速安全。我們用 Nuxt 做了 SSR,而且啟用了 PWA module,Lighthouse 得分在 90 分以上。官網(wǎng)在保證 IE9 兼容性的基礎(chǔ)上,還使用了諸如 InteractionObserver 等新特性努力提升速度。
另外一點就是安全性了。雖說是一個介紹性的網(wǎng)站,HTTPS 還是要上的。我想到了近來一直很火的 Let's Engypt 免費證書,既給公司省錢,又省去了申請證書的麻煩。
這次就是用全球最快的 Web 服務(wù)器 Nginx + 免費好用的 Let's Engypt 證書打造我們公司官網(wǎng)的一些記錄,或者說是心得分享。
編譯 Nginx
公司使用的是阿里云服務(wù)器,CentOS 系統(tǒng)。CentOS 7 自帶 OpenSSL 1.0.1e,不支持 ALPN,在新版的 Chrome 瀏覽器上不能啟用 HTTP 2。
我選擇自己編譯 Nginx,這樣還可以把 br 壓縮模塊 和 證書透明模塊 也編譯進(jìn)去。當(dāng)然如果你不需要后兩個模塊,可以直接用外國網(wǎng)友編譯好的 rpm 包:https://brouken.com/repo
下載 OpenSSL 源碼
首先下載 OpenSSL,解包
$ wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz $ tar -zxvf openssl-1.1.0f.tar.gz $ cd openssl-1.1.0f我還打了 Cloudflare 的 SSL 補(bǔ)丁
$ wget https://github.com/cloudflare/sslconfig/raw/master/patches/openssl__1.1.0_chacha20_poly1305.patch $ patch -p1 < openssl__1.1.0_chacha20_poly1305.patch下載第三方 Nginx 模塊
下載 ngx_brotli 以支持 br 壓縮方式
$ git clone https://github.com/google/ngx_brotli.git下載 nginx-ct 以支持證書透明(Certificate Transparency)
$ git clone https://github.com/grahamedgecombe/nginx-ct.git順帶把方便好用的 headers-more-nginx-module 也編譯進(jìn)去
$ git clone https://github.com/openresty/headers-more-nginx-module.git下載并編譯 Nginx
下載 Nginx 源代碼
$ wget http://nginx.org/download/nginx-1.13.4.tar.gz $ tar -zxvf nginx-1.13.4.tar.gz $ cd nginx-1.13.4.tar.gz打補(bǔ)丁
$ wget https://github.com/cloudflare/sslconfig/raw/master/patches/nginx_1.13.1_http2_hpack.patch $ patch -p1 < nginx_1.13.1_http2_hpack.patch編譯 Nginx。我選擇使用官方的編譯參數(shù)加入特定模塊后直接替換 Nginx 可執(zhí)行文件的方式,以支持使用 systemctl 以服務(wù)的方式啟動。OpenSSL 以靜態(tài)鏈接的方式編譯到 Nginx 內(nèi)部,以免對系統(tǒng)其它程序造成干擾
$ ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx --group=nginx --with-file-aio --with-threads --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -Wl,-E' --with-openssl=`realpath ../openssl` --with-openssl-opt="enable-ec_nistp_64_gcc_128 enable-weak-ssl-ciphers" --add-module=`realpath ../ngx_brotli` --add-module=`realpath ../nginx-ct` --add-module=`realpath ../headers-more-nginx-module` $ make替換官方的 Nginx。確保預(yù)先安裝了官方的 rpm 包
$ sudo mv /usr/sbin/nginx /usr/sbin/nginx.old $ sudo cp objs/nginx /usr/sbin/nginx申請證書
Let's Engypt 官方的 certbot 在 CentOS 系統(tǒng)上有各種依賴問題,各種嘗試后放棄了。網(wǎng)上搜索資料后,使用了小巧的 acme.sh。
acme.sh 使用純 Shell 腳本寫成,而且可以申請新式的 ECC 證書,非常方便使用。
首先下載 acme.sh 源碼
$ git clone https://github.com/Neilpang/acme.sh.git $ cd acme.shwww.eoitek.com 使用了 RSA/ECC 雙證書
$ ./acme.sh --issue -d www.eoitek.com -w /home/eoi/eoi-portal $ ./acme.sh --issue -d www.eoitek.com -w /home/eoi/eoi-portal --keylength ec-256未完待續(xù)
總結(jié)
以上是生活随笔為你收集整理的用 Nginx 基于 Let's Engypt 免费证书打造快速安全的 HTTPS 网站的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu上使用octopress+g
- 下一篇: sed行文本处理工具