一、Http與Https的區(qū)別
HTTP:是互聯(lián)網(wǎng)上應(yīng)用最為廣泛的一種網(wǎng)絡(luò)協(xié)議,是一個(gè)客戶端和服務(wù)器端請(qǐng)求和應(yīng)答的標(biāo)準(zhǔn)(TCP),用于從WWW服務(wù)器傳輸超文本到本地瀏覽器的傳輸協(xié)議,它可以使瀏覽器更加高效,使網(wǎng)絡(luò)傳輸減少。
HTTPS:是以安全為目標(biāo)的HTTP通道,簡(jiǎn)單講是HTTP的安全版,即HTTP下加入SSL層,HTTPS的安全基礎(chǔ)是SSL,因此加密的詳細(xì)內(nèi)容就需要SSL。HTTPS協(xié)議的主要作用可以分為兩種:一種是建立一個(gè)信息安全通道,來保證數(shù)據(jù)傳輸?shù)陌踩?;另一種就是確認(rèn)網(wǎng)站的真實(shí)性。
HTTPS和HTTP的區(qū)別主要如下:
1、https協(xié)議需要到ca申請(qǐng)證書,一般免費(fèi)證書較少,因而需要一定費(fèi)用。
2、http是超文本傳輸協(xié)議,信息是明文傳輸,https則是具有安全性的ssl加密傳輸協(xié)議。
3、http和https使用的是完全不同的連接方式,用的端口也不一樣,前者是80,后者是443。
4、http的連接很簡(jiǎn)單,是無狀態(tài)的;HTTPS協(xié)議是由SSL+HTTP協(xié)議構(gòu)建的可進(jìn)行加密傳輸、身份認(rèn)證的網(wǎng)絡(luò)協(xié)議,比http協(xié)議安全。
二、使用openssl生成證書
openssl是目前最流行的SSL密碼庫工具,其提供了一個(gè)通用、健壯、功能完備的工具套件,用以支持SSL/TLS協(xié)議的實(shí)現(xiàn)。
比如生成到:/usr/local/ssl
1
|
openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /usr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt |
生成過程:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# openssl req -x509 -nodes -days 36500 -newkey rsa:2048 -keyout /u sr/local/ssl/nginx.key -out /usr/local/ssl/nginx.crt Generating a 2048 bit RSA private key ...............................................................................+ ++ ...............+++ writing new private key to '/usr/local/ssl/nginx.key' ----- You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:beijing Locality Name (eg, city) [Default City]:beijing Organization Name (eg, company) [Default Company Ltd]:xxxx Organizational Unit Name (eg, section) []:xxxx Common Name (eg, your name or your server's hostname) []:xxxx(一般是域名) Email Address []:[email protected] # ll total 8 -rw-r--r--. 1 root root 1391 Apr 21 13:29 nginx.crt -rw-r--r--. 1 root root 1704 Apr 21 13:29 nginx.key |
三、Nginx安裝http_ssl_module模塊
Nginx如果未開啟SSL模塊,配置Https時(shí)提示錯(cuò)誤。
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:xxx
nginx缺少http_ssl_module模塊,編譯安裝的時(shí)候帶上--with-http_ssl_module配置就行了。
本場(chǎng)景是服務(wù)器已經(jīng)安裝過nginx,但是未安裝http_ssl_module。
1.進(jìn)入到源碼包,如:
1
|
cd /app/download/nginx-1 .12.2 |
2.configure:
1
2
3
4
|
. /configure --prefix= /usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #可能需要的依賴包 yum -y install pcre-devel openssl openssl-devel |
3.make:
1
|
make |
4.不需要執(zhí)行make install,否則就覆蓋安裝了。
5.備份原有的nginx,如:
1
|
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak |
6.然后將剛剛編譯好的nginx覆蓋掉原有的nginx(nginx需要停止)
1
|
cp . /objs/nginx /usr/local/nginx/sbin/ |
7.查看安裝情況:
1
|
/usr/local/nginx/sbin/nginx -V |
1
2
3
4
5
|
nginx version: nginx /1 .12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix= /usr/local/nginx --with-http_stub_status_module --with-http_ssl_module |
四、nginx配置https
貼部分配置信息:
1
2
3
4
5
6
|
server { listen 80; server_name www.yourdomain.com; rewrite ^(.*) https: // $server_name$1 permanent; #http 跳轉(zhuǎn) https } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
server { listen 443 ssl; server_name www.yourdomain.com; ssl_certificate /usr/local/ssl/nginx .crt; ssl_certificate_key /usr/local/ssl/nginx .key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; #禁止在header中出現(xiàn)服務(wù)器版本,防止黑客利用版本漏洞攻擊 server_tokens off; #如果是全站 HTTPS 并且不考慮 HTTP 的話,可以加入 HSTS 告訴你的瀏覽器本網(wǎng)站全站加密,并且強(qiáng)制用 HTTPS 訪問 fastcgi_param HTTPS on; fastcgi_param HTTP_SCHEME https; access_log /usr/local/nginx/logs/httpsaccess .log; } |
先檢驗(yàn)配置的對(duì)不對(duì):
1
|
/usr/local/nginx/sbin/nginx -t |
1
2
|
nginx: the configuration file /usr/local/nginx/conf/nginx .conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx .conf test is successful |
重啟nginx:
1
|
/usr/local/nginx/sbin/nginx -s reload |
訪問:
到此這篇關(guān)于Nginx下配置Https證書詳細(xì)過程的文章就介紹到這了,更多相關(guān)Nginx配置Https證書內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/smartdt/article/details/80027579