一.實(shí)現(xiàn)https加密
我們知道現(xiàn)在到了 https 的時(shí)代了,每個(gè)優(yōu)秀的網(wǎng)站幾乎都已經(jīng)開(kāi)啟 https。開(kāi)啟了 https 加密訪(fǎng)問(wèn)之后,登錄你的網(wǎng)站,瀏覽器地址欄就會(huì)出現(xiàn)一把綠色的鎖,這就是使用了超文本傳輸安全協(xié)議(HTTPS),是以安全為目標(biāo)的HTTP通道,簡(jiǎn)單來(lái)說(shuō)就是HTTP安全版。
https由兩個(gè)部分組成:HTTP+SSL/TLS,在http基礎(chǔ)上加上了一層加密信息模塊,服務(wù)端和客戶(hù)端的信息插損胡都會(huì)通過(guò)TLS進(jìn)行加密,傳輸?shù)臄?shù)據(jù)都是加密后的數(shù)據(jù)
為了解決HTTP協(xié)議的這些缺陷,需要使用另一種協(xié)議:HTTPS。為了數(shù)據(jù)傳輸?shù)陌踩?,HTTPS在http的基礎(chǔ)上加了SSL協(xié)議,SSL依靠證書(shū)驗(yàn)證身份,并為瀏覽器和服務(wù)器之間通信加密;
SSL證書(shū)是一種數(shù)字證書(shū),使用Secure Socket Layer協(xié)議在瀏覽器和web服務(wù)器之間建立一條安全通道,從而實(shí)現(xiàn)數(shù)據(jù)信息在客戶(hù)端和服務(wù)器之間的加密傳輸,保證雙方傳遞信息的安全性,不可被第三方竊聽(tīng),而且用戶(hù)可以通過(guò)服務(wù)器證書(shū)驗(yàn)證所訪(fǎng)問(wèn)網(wǎng)站是否真實(shí)可靠;
加密的HTTPS和HTTP的區(qū)別:
超文本傳輸協(xié)議HTTP協(xié)議被用于在web瀏覽器和網(wǎng)站服務(wù)器之間傳遞信息,HTTP協(xié)議以明文方式發(fā)送內(nèi)容,不提供任何方式的加密數(shù)據(jù),如果攻擊者截取了web瀏覽器和網(wǎng)站服務(wù)器之間的傳輸報(bào)文,就可以直接讀取其中信息,因此,http協(xié)議不適合傳輸一些敏感信息;
實(shí)驗(yàn)如下所示:
第一步:1.關(guān)閉nginx服務(wù),并重新編譯(主要是為了添加ssl模塊)
[root@nodel1 conf]# systemctl stop nginx
[root@nodel1 conf]# cd /mnt
[root@nodel1 mnt]# ls
[root@nodel1 mnt]# cd nginx-1.17.1/
[root@nodel1 nginx-1.17.1]# make clean
[root@nodel1 nginx-1.17.1]# yum install openssl-devel -y #編譯過(guò)程使用加密的時(shí)候,需要安裝這個(gè)
1
2
3
4
5
6
[root@nodel1 nginx-1.17.1]# ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_image_filter_module=dynamic --with-http_ssl_module
[root@nodel1 nginx-1.17.1]# make
1
2
替代之前的二進(jìn)制文件并再次將圖像模塊放入modules目錄下
[root@nodel1 nginx-1.17.1]# cd objs/
[root@nodel1 objs]# cp nginx -f /usr/local/nginx/sbin/nginx
[root@nodel1 objs]# ls
[root@nodel1 objs]# cp ngx_http_image_filter_module.so /usr/local/nginx/modules
1
2
3
4
第二步:在/etc/pki/tls/certs/下生成證書(shū)并將證書(shū)都放到nginx的配置文件的目錄下
[root@nodel1 objs]# cd /etc/pki/tls/certs/
[root@nodel1 certs]# make cert.pem #生成證書(shū)
Country Name (2 letter code) [XX]:cn # 國(guó)家代號(hào),中國(guó)輸入CN
State or Province Name (full name) []:shannxi #省
Locality Name (eg, city) [Default City]:xi'an #市
Organization Name (eg, company) [Default Company Ltd]:westos #公司英文名
Organizational Unit Name (eg, section) []:linux #組織名稱(chēng)
Common Name (eg, your name or your server's hostname) []:nodel1 #主機(jī)名或者你的名字
Email Address []:[email protected] #郵箱地址
[root@nodel1 certs]# cp cert.pem /usr/local/nginx/conf/ # 將證書(shū)都放到nginx的配置文件的目錄下
1
2
3
4
5
6
7
8
9
10
11
12
13
第三步:編寫(xiě)配置文件并開(kāi)啟nginx服務(wù)(注意:此時(shí)并沒(méi)有設(shè)置https加密,只是普通的http)
[root@nodel1 objs]# cd /usr/local/nginx/conf/
[root@nodel1 conf]# vim nginx.conf
131 server {
132 listen 80;
133 server_name www.westos.org;
134 location / {
135 root /web;
136 index index.html;
137 }
138 }
139 }
[root@nodel1 conf]# systemctl start nginx
[root@nodel1 conf]# systemctl status nginx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
第四步:在nodel1配置解析并編輯默認(rèn)發(fā)布文件
vim /etc/hosts
172.25.15.1 nodel1 www.westos.org
1
2
[root@nodel1 conf]# cd
[root@nodel1 ~]# mkdir /web
[root@nodel1 ~]# cd /web
[root@nodel1 web]# vim index.html
[root@nodel1 web]# cat index.html
里面寫(xiě)入的內(nèi)容如下所示:
www.westos.org
1
2
3
4
5
6
7
第五步:測(cè)試并重新編輯nginx的配置文件(此刻是https)
測(cè)試:此時(shí)如果在瀏覽器測(cè)試是不會(huì)出現(xiàn)綠色的鎖的,因?yàn)闆](méi)有設(shè)置https加密
再次編寫(xiě)nginx配置文件,然后檢測(cè)其是否有語(yǔ)法錯(cuò)誤并重啟服務(wù),使其可以實(shí)現(xiàn)http->https
[root@nodel1 web]# cd /usr/local/nginx/conf/
[root@nodel1 conf]# vim nginx.conf
111 # HTTPS server
112
113 server {
114 listen 443 ssl; #ssl端口
115 server_name www.westos.org;
116
117 ssl_certificate cert.pem;
118 ssl_certificate_key cert.pem;
119
120 ssl_session_cache shared:SSL:1m;
121 ssl_session_timeout 5m;
122
123 ssl_ciphers HIGH:!aNULL:!MD5;
124 ssl_prefer_server_ciphers on;
125
126 location / {
127 root /web;
128 index index.html index.htm;
129 }
130 }
131
132 server {
133 listen 80;
134 server_name www.westos.org;
135 rewrite ^/(.*)$ https://www.westos.org/;
136
137 location / {
138 root /web;
139 index index.html;
140 }
141 }
142 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@nodel1 conf]# cd -
/usr/local/nginx/sbin
[root@nodel1 sbin]# ./nginx -t #檢測(cè)語(yǔ)法是否有錯(cuò)
[root@nodel1 sbin]# systemctl reload nginx
1
2
3
4
第六步:在真機(jī)中添加域名解析并在瀏覽器訪(fǎng)問(wèn)
[root@foundation15 ~]# vim /etc/hosts
172.25.15.1 nodel1 www.westos.org
1
2
在瀏覽中輸入www.westos.org后會(huì)自動(dòng)補(bǔ)齊https:并顯示添加證書(shū)
查看一下證書(shū)信息
二.重定向
第一種:重定向到具體文件
前提:
(一)將www.westos.org重定向到https://www.westos.org(上述實(shí)驗(yàn)已經(jīng)完成,這里進(jìn)行擴(kuò)充)
我們想要重定向到具體的文件,問(wèn)題如下:
在物理機(jī)中訪(fǎng)問(wèn)www.westos.org,顯示的HTTP狀態(tài)碼是302(302表示暫時(shí)重定向,301表示永久重定向),且可以成功重定向到https://www.westos.org。
但是我們?cè)谖锢頇C(jī)中訪(fǎng)問(wèn)www.westos.org/vim.jpg,發(fā)現(xiàn)其不可以成功重定向到https://www.westos.org/vim.jpg,只是可以重定向到https://www.westos.org,不能重定向到具體的文件。
實(shí)驗(yàn)步驟如下所示:
[root@nodel1 sbin]# cd -
/usr/local/nginx/conf
[root@nodel1 conf]# vim nginx.conf
132 server {
133 listen 80;
134 server_name www.westos.org;
135 rewrite ^/(.*)$ https://www.westos.org/$1;
[root@nodel1 conf]# systemctl reload nginx
1
2
3
4
5
6
7
8
9
10
11
此時(shí)在物理機(jī)中訪(fǎng)問(wèn)www.westos.org/redhat.jpg,可以成功重定向到https://www.westos.org/redhat.jpg
此時(shí)我們可以看見(jiàn),是302暫時(shí)重定向,我們想要將暫時(shí)重定向設(shè)置為永久重定向,就需要進(jìn)行以下操作
[root@nodel1 conf]# vim nginx.conf
132 server {
133 listen 80;
134 server_name www.westos.org;
135 rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
[root@nodel1 conf]# systemctl reload nginx
1
2
3
4
5
6
7
8
此時(shí)在去物理機(jī)中訪(fǎng)問(wèn):
第二種:不同網(wǎng)站之間的重定向
將www.westos.org/bbs/index.html重定向到/bbs.westos.org/index.html
第一步:編輯nginx配置文件,配置bbs.westos.org的測(cè)試頁(yè)
[root@nodel1 conf]# vim nginx.conf
143 server {
144 listen 80;
145 server_name bbs.westos.org;
146
147 location / {
148 root /bbs;
149 index index.html;
150 }
151 }
152 }
[root@nodel1 conf]# systemctl reload nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
檢測(cè)是否有錯(cuò)誤:
[root@nodel1 conf]# cd
[root@nodel1 ~]# mkdir /bbs
[root@nodel1 ~]# cd /bbs
[root@nodel1 bbs]# vim index.html
[root@nodel1 bbs]# cat index.html
bbs.westos.org #bbs的測(cè)試頁(yè)
1
2
3
4
5
6
在真機(jī)里面添加解析:
[root@foundation15 ~]# vim /etc/hosts
172.25.15.1 nodel1 www.westos.org bbs.westos.org
1
2
測(cè)試:
第二步:修改nginx配置文件并重啟nginx服務(wù)
[root@nodel1 conf]# vim nginx.conf
[root@nodel1 bbs]# cd /usr/local/nginx/conf/
[root@nodel1 conf]# vim nginx.conf
135 #rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
136 rewrite ^/bbs/(.*)$ https://bbs.westos.org/$1 permanent;
[root@nodel1 conf]# systemctl reload nginx
1
2
3
4
5
6
7
8
9
測(cè)試:
以上的方法雖然實(shí)現(xiàn)了不同網(wǎng)頁(yè)的重定向,但是在測(cè)試方面我們發(fā)現(xiàn),還得加個(gè)bbs,這樣也太不方便了,為了解決這個(gè)問(wèn)題,我們進(jìn)行以下操作。
第三步:完善
將bbs這個(gè)目錄復(fù)制/web目錄下
[root@nodel1 conf]# cp -r /bbs /web/
[root@nodel1 conf]# cd /web/
[root@nodel1 web]# ls
bbs index.html
1
2
3
4
[root@nodel1 conf]# vim nginx.conf
132 server {
133 listen 80;
134 server_name www.westos.org bbs.westos.org;
135 #rewrite ^/(.*)$ https://www.westos.org/$1 permanent;
136 #rewrite ^/bbs/(.*)$ https://bbs.westos.org/$1 permanent;
137 if ($host = "bbs.westos.org"){
138 rewrite ^/(.*)$ https://www.westos.org/bbs/$1 permanent;
139 }
140
141 location / {
142 root /web;
143 index index.html;
144 }
145 }
146 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@nodel1 nginx]# ./sbin/nginx -t
[root@nodel1 conf]# systemctl reload nginx
1
2
第四步:測(cè)試