RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
电话:0833-2438125
服务时间:9:00-21:00
你可能遇到了下面的问题
关闭右侧工具栏
实现https加密以及重定向
    发布时间:2022-07-31   访问量:13318

一.实现https加密

我们知道现在到了 https 的时代了,每个优秀的网站几乎都已经开启 https。开启了 https 加密访问之后,登录你的网站,浏览器地址栏就会出现一把绿色的锁,这就是使用了超文本传输安全协议(HTTPS),是以安全为目标的HTTP通道,简单来说就是HTTP安全版。


https由两个部分组成:HTTP+SSL/TLS,在http基础上加上了一层加密信息模块,服务端和客户端的信息插损胡都会通过TLS进行加密,传输的数据都是加密后的数据


为了解决HTTP协议的这些缺陷,需要使用另一种协议:HTTPS。为了数据传输的安全性,HTTPS在http的基础上加了SSL协议,SSL依靠证书验证身份,并为浏览器和服务器之间通信加密;


SSL证书是一种数字证书,使用Secure Socket Layer协议在浏览器和web服务器之间建立一条安全通道,从而实现数据信息在客户端和服务器之间的加密传输,保证双方传递信息的安全性,不可被第三方窃听,而且用户可以通过服务器证书验证所访问网站是否真实可靠;


加密的HTTPS和HTTP的区别:


超文本传输协议HTTP协议被用于在web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的加密数据,如果攻击者截取了web浏览器和网站服务器之间的传输报文,就可以直接读取其中信息,因此,http协议不适合传输一些敏感信息;

实验如下所示:


第一步:1.关闭nginx服务,并重新编译(主要是为了添加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           #编译过程使用加密的时候,需要安装这个

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




替代之前的二进制文件并再次将图像模块放入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/下生成证书并将证书都放到nginx的配置文件的目录下


[root@nodel1 objs]# cd /etc/pki/tls/certs/

[root@nodel1 certs]# make cert.pem            #生成证书



Country Name (2 letter code) [XX]:cn        # 国家代号,中国输入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                               #组织名称

Common Name (eg, your name or your server's hostname) []:nodel1               #主机名或者你的名字

Email Address []:root@westos.org                   #邮箱地址


[root@nodel1 certs]# cp cert.pem /usr/local/nginx/conf/                        #    将证书都放到nginx的配置文件的目录下

1

2

3

4

5

6

7

8

9

10

11

12

13





第三步:编写配置文件并开启nginx服务(注意:此时并没有设置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配置解析并编辑默认发布文件


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

里面写入的内容如下所示:

www.westos.org

1

2

3

4

5

6

7



第五步:测试并重新编辑nginx的配置文件(此刻是https)


测试:此时如果在浏览器测试是不会出现绿色的锁的,因为没有设置https加密



再次编写nginx配置文件,然后检测其是否有语法错误并重启服务,使其可以实现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            #检测语法是否有错

[root@nodel1 sbin]# systemctl reload nginx

1

2

3

4



第六步:在真机中添加域名解析并在浏览器访问


[root@foundation15 ~]# vim /etc/hosts

172.25.15.1  nodel1    www.westos.org

1

2



在浏览中输入www.westos.org后会自动补齐https:并显示添加证书



查看一下证书信息






二.重定向

第一种:重定向到具体文件


前提:

(一)将www.westos.org重定向到https://www.westos.org(上述实验已经完成,这里进行扩充)


我们想要重定向到具体的文件,问题如下:


在物理机中访问www.westos.org,显示的HTTP状态码是302(302表示暂时重定向,301表示永久重定向),且可以成功重定向到https://www.westos.org。


但是我们在物理机中访问www.westos.org/vim.jpg,发现其不可以成功重定向到https://www.westos.org/vim.jpg,只是可以重定向到https://www.westos.org,不能重定向到具体的文件。



实验步骤如下所示:


[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






此时在物理机中访问www.westos.org/redhat.jpg,可以成功重定向到https://www.westos.org/redhat.jpg




此时我们可以看见,是302暂时重定向,我们想要将暂时重定向设置为永久重定向,就需要进行以下操作


[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







此时在去物理机中访问:




第二种:不同网站之间的重定向


将www.westos.org/bbs/index.html重定向到/bbs.westos.org/index.html

第一步:编辑nginx配置文件,配置bbs.westos.org的测试页


[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



检测是否有错误:





[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的测试页

1

2

3

4

5

6



在真机里面添加解析:


[root@foundation15 ~]# vim /etc/hosts

172.25.15.1  nodel1    www.westos.org  bbs.westos.org

1

2





测试:




第二步:修改nginx配置文件并重启nginx服务


[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






测试:



以上的方法虽然实现了不同网页的重定向,但是在测试方面我们发现,还得加个bbs,这样也太不方便了,为了解决这个问题,我们进行以下操作。


第三步:完善


将bbs这个目录复制/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





第四步:测试