nginx搭建https伺服器

2022-06-20 17:21:07 字數 3214 閱讀 7296

一、https簡介

1.https簡介

https其實是有兩部分組成:http + ssl / tls,也就是在http上又加了一層處理加密資訊的模組。服務端和客戶端的資訊傳輸都會通過tls進行加密,所以傳輸的資料都是加密後的資料

2.https協議原理

首先,客戶端與伺服器建立連線,各自生成私鑰和公鑰,是不同的。伺服器返給客戶端乙個公鑰,然後客戶端拿著這個公鑰把要搜尋的東西加密,稱之為密文,並連並自己的公鑰一起返回給伺服器,伺服器拿著自己的私鑰解密密文,然後把響應到的資料用客戶端的公鑰加密,返回給客戶端,客戶端拿著自己的私鑰解密密文,把資料呈現出來

二、證書和私鑰的生成

注意:一般生成的目錄,應該放在nginx/conf/ssl目錄

1.建立伺服器證書金鑰檔案 server.key:

openssl genrsa -des3 -out server.key 1024

輸入密碼,確認密碼,自己隨便定義,但是要記住,後面會用到。

2.建立伺服器證書的申請檔案 server.csr

openssl req -new -key server.key -out server.csr

輸出內容為:

enter

pass phrase for root.key: ← 輸入前面建立的密碼

country name (2 letter code) [au]:cn ←

國家代號,中國輸入cn

state or province name (full name) [some-state]:beijing ←

省的全名,拼音

locality name (eg, city) :beijing ← 市的全名,拼音

organization name

(eg, company) [internet widgits pty ltd]:mycompany corp. ← 公司英文名

organizational unit name (eg, section) : ← 可以不輸入

common name (eg, your

name) : ← 此時不輸入

email address :[email protected] ← 電子郵箱,可隨意填

please

enter the following 『extra』 attributes

to be sent with your certificate

request

a challenge password : ← 可以不輸入

an optional company name : ←

可以不輸入

4.備份乙份伺服器金鑰檔案

cp server.key server.key.org

5.去除檔案口令

openssl

rsa -in server.key.org -out server.key

6.生成證書檔案server.crt

openssl

x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

三、配置檔案

1.下面為配置檔案 /usr/local/nginx/conf/vhost/daj.conf

server       

}

四、開啟nginx的ssl模組

原因是nginx缺少http_ssl_module模組,編譯安裝時帶上--with-http_ssl_module配置就可以了

2.如果已經安裝過nginx,想要新增模組看下面

1)切換到nginx原始碼包

cd /usr/local/src/nginx-1.11.3

2)檢視ngixn原有的模組

/usr/local/nginx/sbin/nginx

-v3)重新配置

4)重新編譯,不需要make 

install安裝。否則會覆蓋

make

5)備份原有已經安裝好的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

6)將剛剛編譯好的nginx覆蓋掉原來的nginx(ngixn必須停止)

cp ./objs/nginx /usr/local/nginx/sbin/ 

這時,會提示是否覆蓋,請輸入yes,直接回車預設不覆蓋

7)啟動nginx,檢視nginx模組,發現已經新增

nginx搭建https伺服器

https hypertext transfer protocol over secure socket layer 是以安全為目標的http通道,簡單來講就是http的安全版。即http下加入ssl層,https的安全基礎是ssl,因此加密的詳細內容就需要ssl。它是乙個uri scheme 抽象...

Nginx搭建https伺服器

https簡介 https hypertext transfer protocol over secure socket layer 是以安全為目標的http通道,簡單來講就是http的安全版。即http下加入ssl層,https的安全基礎是ssl,因此加密的詳細內容就需要ssl。它是乙個uri s...

nginx 搭建https伺服器

安裝在之前的文章中已經寫過了。但是之前的安裝出現了錯誤導致https沒有安裝成功。所以這裡提一下大致的流程以及問題 openssl v ersion 命令用來列印版本以及 openssl 其他各種資訊。用法 cpp view plain copy openssl version a v b o f ...