使用OpenSSL自建CA + Nginx配置HTTPS

來源:互聯網
上載者:User

標籤:const   研究   uri   安裝   先行者   ngui   git   功能   失敗   

Ubuntu 16.04(ECS),OpenSSL 1.0.2g  1 Mar 2016,Nginx 1.10.3 (Ubuntu),

瀏覽器:Chrome 67,Firefox 61,Edge 40,IE 11

 

序言

孤之前從來沒有建立過HTTPS網站,感覺很進階、很難,雖然也讀過不少博文、資料,十年前在大學時,也使用過OpenSSL操作過建立認證,但後來都忘記了。

前同事說建立HTTPS網站很容易的,當時自己是不信的,並發生了一些爭論,在此表示歉意。

由於自己搭建了網站,提供了註冊、登入功能,因此,有安全方面的考慮。前兩日研究了前端加密、Base64編碼等方案(crypto-js),但都無法很好地保證使用者資料安全。

最付出不少精時後,最終決定將網站升級為HTTPS網站,本文記錄了實驗過程中的關鍵步驟——兩步:

1.自建CA並簽名Server認證

2.配置Nginx伺服器

 

自建CA並並簽名Server認證

本步驟完全參考了自建CA並簽名server認證實現https by Andy____Li,只是對一些檔案名稱、配置參數進行了修改。

注意:在執行此步驟之前,請確保Linux作業系統上安裝了OpenSSL。

可以分為下面的小步驟(拷貝了參考連結中的命令):

1.自建CA

產生CA私匙
openssl genrsa -out icatchtek.key 2048

執行結果:提示建立成功,可又提示unable to什麼的,為什嗎?還需要dig,

 

Generating RSA private key, 2048 bit long modulus
................+++
...........................................................................................+++
unable to write ‘random state‘
e is 65537 (0x10001)

 

產生CA認證請求
openssl req -new -key icatchtek.key -out icatchtek.csr
PS:認證請求是對簽名的請求,需要使用私密金鑰進行簽名

此命令輸入後,需要填寫資訊。

產生CA根憑證
openssl x509 -req -in icatchtek.csr -extensions v3_ca -signkey icatchtek.key -out icatchtek.crt
PS:認證是自簽名或CA簽名過的憑據,用來進行身份認證

執行結果:

Signature ok
subject=/C=CN/ST=Guangdong/L=Shenzhen/OU=.../CN=.../emailAddress=[email protected]
Getting Private key
unable to write ‘random state‘

 

2.自建server端認證

產生server私匙
openssl genrsa -out smarthome_server.key 2048

結果同上。

產生server認證請求
openssl req -new -key smarthome_server.key -out smarthome_server.csr

結果同上,需輸入參數。

產生server認證:下面命令的紫色部分就是上面建立的一系列檔案,不用弄錯了。
產生server認證,需要一份設定檔,可參閱:vi openssl.cnf
openssl x509 -days 365 -req -in smarthome_server.csr -extensions v3_req -CAkey icatchtek.key -CA icatchtek.crt -CAcreateserial -out smarthome_server.crt -extfile openssl.cnf

 

關於生產Server認證這一步,孤是直接拷貝了參考連結中的內容,並進行了修改。關於此檔案怎麼寫,孤是不太熟悉的,還需要看更多資料,比如參考連結中的OpenSSL主設定檔openssl.cnf。

下面是自己的open.cnf(私密資訊沒有)(在參考連結中,[req]下面的幾個section是有縮排的,不知道為什麼):

[req]distinguished_name = req_distinguished_namereq_extensions = v3_req[req_distinguished_name]countryName = Country Name (2 letter code)countryName_default = CNstateOrProvinceName = GuangdongstateOrProvinceName_default = GuangdonglocalityName = ShenzhenlocalityName_default = ShenzhenorganizationalUnitName  = ...organizationalUnitName_default  = ...commonName = ...commonName_max  = 64[ v3_req ]# Extensions to add to a certificate requestbasicConstraints = CA:FALSEkeyUsage = nonRepudiation, digitalSignature, keyEnciphermentsubjectAltName = @alt_names[alt_names]IP.1 = xxx.xxx.xxx.xxxDNS.1 = www.example.com

 

說明,上面的每一步都會在 執行命令的 目前的目錄下 建立相應的檔案!下面是孤實驗過程中建立的:

$ ls
ben_server.crt ben_server.key icatchtek.csr icatchtek.srl
ben_server.csr icatchtek.crt icatchtek.key openssl.cnf

 

配置Nginx伺服器

按照參考連結的說法,是在server 塊內 listen 連接埠後添加下面的語句:

ssl on;
ssl_certificate /home/ubuntu/webvideo/nginx/conf/smarthome_server.crt;
ssl_certificate_key /home/ubuntu/webvideo/nginx/conf/smarthome_server.key;

 

注意,*.crt、*.key檔案的路徑需要更改,孤是將它們放在/etc/nginx/conf.d/ca裡面的。

 

作為Nginx新手,孤的Nginx設定檔中只有一個Server的,連接埠是80。可HTTPS網站的預設連接埠不是443嗎?

修改前的Nginx設定檔:

server {listen 80 default_server;listen [::]:80 default_server;# SSL configuration## listen 443 ssl default_server;# listen [::]:443 ssl default_server;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332## Read up on ssl_ciphers to ensure a secure configuration.# See: https://bugs.debian.org/765782## Self signed certs generated by the ssl-cert package# Don‘t use them in a production server!## include snippets/snakeoil.conf;

修改後的設定檔:

server {listen 80 default_server;listen [::]:80 default_server;# SSL configuration#listen 443 ssl default_server;listen [::]:443 ssl default_server;ssl on;ssl_certificate      /etc/nginx/conf.d/ca/smarthome_server.crt;ssl_certificate_key  /etc/nginx/conf.d/ca/smarthome_server.key;## Note: You should disable gzip for SSL traffic.# See: https://bugs.debian.org/773332

 

分別訪問http、https對應的網頁,http的訪問失敗了、但https訪問成功:原因是,只有一個伺服器,可自己監聽了兩個連接埠,還需要結合nginx的rewrite技術,將http請求轉到對應的https請求。

 

最後,建立了兩個Server:一個sever監聽80連接埠、一個監聽443連接埠,前者http連結被rewrite到後者的連結,設定檔如下:

80連接埠後面沒有default_server了——不知道自己為何如此配置;在只有一個server時,其server_name為_,現在有兩個了,怎麼配置呢?能否相同?

server {        listen 80;        listen [::]:80;        rewrite ^(.*)$ https://$host$1 permanent;}server {        # SSL configuration        #        listen 443 ssl default_server;        listen [::]:443 ssl default_server;        ssl on;        ssl_certificate /etc/nginx/conf.d/ca/ben_server.crt;        ssl_certificate_key /etc/nginx/conf.d/ca/ben_server.key;

 

上面的配置完畢後,就可以通過http、https訪問網站了,不過,http的是轉向了https,所以,頁面最後看到的是https。

但是,因為瀏覽器有認證安全校正的機制,因此,不是所有瀏覽器都可以成功開啟頁面,測試結果是,只有Firefox在設定後可以開啟頁面,其它幾種瀏覽器都失敗了。

-Chrome瀏覽器

 

-Firefox瀏覽器:最終訪問成功。

 

IE瀏覽器:

 

 

網頁不能訪問,原因是瀏覽器認為 頒發認證的CA 不安全。參考連結說把自己的CA根憑證加入到瀏覽器。

在嘗試過後,發現問題並沒有解決——Chrome、IE都試了試,不行啊!懷疑和自己建立認證的過程有關係,也可能和最新版本的瀏覽器的安全機制升級有關係,更可能的是前者。

而且,如上面所講,自己在建立key是出現了unable to write ‘random state‘錯誤!後面可以解決此問題後再做嘗試,繼續dig。

怎麼匯入呢?Chrome的設定-搜尋“認證”,IE、Edge的Internet選項:其實兩者配置的是一個東西,其屬於作業系統。

 

後記

總算知道怎麼讓自己的網站變為HTTPS的了,自建的認證既然無法正常使用,那麼,去申請認證吧——知道一個免費申請的機構,當然,付費的就很多了。

Nginx配置不熟悉;

雖然本文用OpenSSL做了一些操作,可是,為何這麼做呢?官方文檔在哪裡呢?設定檔怎麼寫呢?現在只能踩著先行者的腳步往前走,感謝!

路漫漫啊,

 

參考連結

自建CA並簽名server認證實現https by Andy____Li

https SSL 自建認證的製作 by Erice_e

OpenSSL主設定檔openssl.cnf by 園友 我為什麼堅持寫部落格

nginx 將http請求轉寄到https請求

 

使用OpenSSL自建CA + Nginx配置HTTPS

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.