日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

用 Nginx 基于 Let's Engypt 免费证书打造快速安全的 HTTPS 网站

發布時間:2023/12/20 Nginx 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用 Nginx 基于 Let's Engypt 免费证书打造快速安全的 HTTPS 网站 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我大EOI的官網正式上線啦!為了打造公司第一個正式上線的公開站點,我們著實費了不少心思,其中之一就是如何把它搞得快速安全。我們用 Nuxt 做了 SSR,而且啟用了 PWA module,Lighthouse 得分在 90 分以上。官網在保證 IE9 兼容性的基礎上,還使用了諸如 InteractionObserver 等新特性努力提升速度。

另外一點就是安全性了。雖說是一個介紹性的網站,HTTPS 還是要上的。我想到了近來一直很火的 Let's Engypt 免費證書,既給公司省錢,又省去了申請證書的麻煩。

這次就是用全球最快的 Web 服務器 Nginx + 免費好用的 Let's Engypt 證書打造我們公司官網的一些記錄,或者說是心得分享。

編譯 Nginx

公司使用的是阿里云服務器,CentOS 系統。CentOS 7 自帶 OpenSSL 1.0.1e,不支持 ALPN,在新版的 Chrome 瀏覽器上不能啟用 HTTP 2。

我選擇自己編譯 Nginx,這樣還可以把 br 壓縮模塊 和 證書透明模塊 也編譯進去。當然如果你不需要后兩個模塊,可以直接用外國網友編譯好的 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 補丁

$ 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 也編譯進去

$ 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

打補丁

$ 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。我選擇使用官方的編譯參數加入特定模塊后直接替換 Nginx 可執行文件的方式,以支持使用 systemctl 以服務的方式啟動。OpenSSL 以靜態鏈接的方式編譯到 Nginx 內部,以免對系統其它程序造成干擾

$ ./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。確保預先安裝了官方的 rpm 包

$ sudo mv /usr/sbin/nginx /usr/sbin/nginx.old $ sudo cp objs/nginx /usr/sbin/nginx

申請證書

Let's Engypt 官方的 certbot 在 CentOS 系統上有各種依賴問題,各種嘗試后放棄了。網上搜索資料后,使用了小巧的 acme.sh。

acme.sh 使用純 Shell 腳本寫成,而且可以申請新式的 ECC 證書,非常方便使用。

首先下載 acme.sh 源碼

$ git clone https://github.com/Neilpang/acme.sh.git $ cd acme.sh

www.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

未完待續

總結

以上是生活随笔為你收集整理的用 Nginx 基于 Let's Engypt 免费证书打造快速安全的 HTTPS 网站的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。