Day 58 NginxHttps

Day 58 NginxHttps

1.1 keepalived

    7x24小时不DOWN场景

    2台服务器

                  优先级150      virtu_router_id  50      lb01

                  优先级100      virtu_router_id  50      lb02

1.2     列脑(主和备上面都有虚拟IP --->俗称VIP

        解决办法:执行脚本

1.2.1  keepalived造成故障:主和备上都编写一个脚本:

           1.备判断自己是否能ping通主

           2.检查自己是否存在VIP

           3.建议使用kill命令杀死备机的keepalived

1.2.2  Nginx故障,导致请求通过VIP找到Master服务器无法提供服务。

            1.检查Nginx的进程是否存在,如果存在则sleep 5秒,再次检查

            2.如果不存在,则尝试启动一次Nginx

            3.如果启动成功,进入下一步,sleep 5

            4.如果启动不成功,强制杀掉keepalived让地址漂移至备机

            5.建议写在Master上面即可。

1.2.3  keepalived用在哪

            1.国企,传统互联网,全是物理服务器

1.2.4  keepalived不能用在哪

            1.互联网--->使用公有云的  (LB)

            2.公有云不能使用keepalived工具,公有云本身负载均衡支持高可用

1.2.5 面试被问到:你们高可用如何实现的??

            我们使用公有云的LB负载均衡,本身厂商就支持高可用,所以这一块我们没做考虑。

            但:如果贵公司使用的是硬件服务器,那么也可以使用keepalived开源软件实现高可用。

    虚拟机上面:

    一台服务器的https

    前端负载均衡,后端是web服务器,实现https

    阿里云:

    ecs 运行一个nginx  启用https

slb+ecs实现https

1.3 Rewrite

1.3.1  什么是rewrite

    RewriteURL重写,主要实现url地址重写, 以及重定向, 就是把传入Web的请求重定向到其他URL的过程。

1.3.2  Rewrite使用场景

    1.URL地址跳转,

        1.如用户访问bgx.com将其跳转到xuliangwei.com

        2.当用户通过http的方式访问bgx.com时,将其跳转至https的方式访问bgx.com

    2.URL伪静态, 将动态页面显示为静态页面方式的一种技术, 便于搜索引擎的录入, 同时减少动态URL地址对外暴露过多的参数, 提升更高的安全性。

    3.搜索引擎SEO优化依赖于url路径, 以便支持搜索引擎录入  

  

$request_filename;

http://ds.oldboy.com/nginx.png  --》 /soft/code/images/nginx.png

$request_uri;

http://ds.oldboy.com/nginx.png  --》 /nginx.png

1.3.3 配置开启NginxRewrite

设置/etc/nginx/nginx.conf配置文件中的错误日志级别为notice(nginx中最低级别的错误)

error_log       /var/log/nginx/error.log notice;

1.3.4 http模块层, 增加一行rewrite_log日志

rewrite_log on;

1.3.5 配置rewrite,测试日志是否生效

    location / {   

    rewrite  ^/  https://www.xuliangwei.com;

    }

1.3.6 重启nginx具体可能是 service nginx reload等命令OK,接下来你可以到错误日志目录查看具体的rewrite信息了。

2018/09/29 09:29:54 [notice] 1389#1389: *6 "^/" matches "/", client: 10.0.0.1, server: ds.oldboy.com, request: "GET / HTTP/1.1", host: "ds.oldboy.com"

2018/09/29 09:29:54 [notice] 1389#1389: *6 rewritten redirect: "https://www.xuliangwei.com",

  

1.4 r.oldboy.com

1.4.1 eg 1:用户访问/abc/1.html实际上真实访问是/cc/bb/2.html

   1.先配置好真实的路径

server {

        listen 80;

        server_name r.oldboy.com;

 

        location / {

                root /code;

                index index.html;

        }

}

2.准备对应的站点目录

[root@web03 ~ ]# mkdir /code/cc/bb -p

[root@web03 ~ ]# echo "cc_bb_2" > /code/cc/bb/2.html

[root@web03 ~ ]# systemctl restart nginx

 

3.配置rewrite跳转规则

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

 

    location / {

       root /code;

       index index.html;

    }

      #不区分大小写匹配

    location ~* /abc/1.html {

       rewrite /abc/1.html /cc/bb/2.html;

    }

}

3.配置rewrite跳转规则【修订1次】

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

 

    location / {

       root /code;

       index index.html;

    }

      #不区分大小写匹配

    location ~* /abc/1.html {

       rewrite (.*) /cc/bb/2.html redirect;

    }

}

4.配置rewrite跳转规则【修订2次】

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

 

    location / {

       root /code;

       index index.html;

    }

      #不区分大小写匹配

    location ~* /abc/1.html {

        return 302 /cc/bb/2.html;

    }

}

1.4.2 eg 2:用户访问/2018/ccc/bbb/2.html实际上真实访问是/2014/ccc/bbb/2.html

#http://www.bgx.com/2018/cc/bb/2.html ==> http://www.bgx.com/2014/cc/bb/2.html

1.先配置好真实的路径

server {

        listen 80;

        server_name r.oldboy.com;

 

        location / {

                root /code;

                index index.html;

        }

}

 

2.准备对应的站点目录

[root@web03 ~ ]# mkdir /code/2014/cc/bb -p

[root@web03 ~ ]# echo "2014" > /code/2014/cc/bb/2.html

[root@web03 ~ ]# systemctl restart nginx

 

3.配置rewrite跳转规则

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

    location / {

       root /code;

       index index.html;

    }

      #不区分大小写匹配

    location ~* /2018/cc/bb/1.html {

       rewrite (.*) /2014/cc/bb/2.html;

    }

}

1.4.3 eg  3:用户访问/test目录下任何内容, 实际上真实访问是http://www.xuliangwei.com    

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

    location /test {

       rewrite (.*) https://www.xuliangwei.com;

    }

}

1.4.4 eg 4:用户访问course-11-22-33.html 实际上真实访问是  /course/11/22/33/course_33.html

1.先配置好真实的路径

server {

        listen 80;

        server_name r.oldboy.com;

 

        location / {

                root /code;

                index index.html;

        }

}

 

2.准备对应的站点目录

[root@web03 ~ ]# mkdir /code/course/11/22/33/ -p

[root@web03 ~ ]# echo "bgx123.com" > /code/course/11/22/33/course_33.html

[root@web03 ~ ]# systemctl restart nginx

 

3.配置rewrite跳转规则

[root@web03 conf.d]# cat rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

 

    location / {

       root /code;

       index index.html;

 

    #仅针对一条规则

    #rewrite ^/course-11-22-33.html /course/11/22/33/course_33.html;

    # 如果课程路径都一样, 那这条规则灵活度更高

    rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html;

    }

}

1.4.5 eg 5:http请求,跳转至https

server {

        listen 80;

        server_name bgx.com;

        rewrite ^(.*) https://$server_name$1 redirect;

        #return 302 https://$server_name$request_uri;

}

 

server {

    listen 443;

    server_name bgx.com;

    ssl on;

}

rewrite指令根据表达式来重定向URI, 或者修改字符串。 可以应用于server,location, if环境下, 每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:

flag

last        本条规则匹配完成后,停止匹配,不在匹配后面的规则

break        本条规则匹配完成后,停止匹配,不在匹配后面的规则

redirect     返回302临时重定向, 地址栏会显示跳转后的地址

permanent    返回301永久重定向, 地址栏会显示跳转后的地址

1.4.6 临时跳转302

location /test {

        rewrite (.*) https://www.xuliangwei.com redirect;

}

每一次浏览器都会询问服务端(浏览器不会记录r.oldboy.com/test---》)

1.4.7 永久跳转301

location /test {

        rewrite (.*) https://www.xuliangwei.com permanent;

}

只要浏览器访问一次,会记录这个跳转,下次再也不会询问,直接跳转

对比flagbreaklast

1.4.8 公司目前产品有一个url地址是r.oldboy.com/2017_old随着时间的推移,公司希望客户通过新的url访问www.oldboy.com/2019_new需要保证浏览器的url地址不发生变化

root@web03 conf.d]# vim rewrite.conf

server {

    listen 80;

    server_name r.oldboy.com;

    root /code;

 

    location ~ ^/2019_new {

        rewrite ^/2019_new /2017_old/ break;

    }

    location ~ ^/2020_new {

        rewrite ^/2020_new /2017_old/ last;

    }

    location ~ ^/2017_old {

        root /code;

        index index.html;

    }                                    

[root@web03 conf.d]# mkdir /code/2017_old -p

[root@web03 conf.d]# echo "2017_code" > /code/2017_old/index.html

[root@web03 conf.d]# systemctl restart nginx

1.5 lastbreak对比总结:

last   r.oldboy.com/2020_new    ---->    r.oldboy.com/2017_old

1.last进行Rewrite匹配成功, 停止当前这个请求, 并根据rewrite匹配的规则重新向Server发起一个请求。

2.新请求的内容是域名+URL, 对应的地址为www.oldboy.com/2017_old

break   r.oldboy.com/2019_new    ---->  /2017_old     不存在404

1.break进行Rewrite匹配成功后, 不会像last重新发起请求, 首先查找站点目录下/code/2017_old/默认返回页是否存在, 如不存在则404, 如存在继续往下匹配

2.根据Rewrite匹配的规则, 跳转至r.oldboy.com/2017_old/URL地址, 匹配成功后则不继续匹配

2.1 配置HTTPS前预备知识

2.1.1  HTTPS证书购买选择

    HTTPS证书的选择

        专业版OV型证书       不显示企业名称

        高级版EV型证书        显示企业名称

2.1.2  HTTPS证书购买的类型    oldboy.com  (www m image 二级域名)

        保护1个域名 www

        保护5个域名 www images cdn test m

        通配符域名 *.oldboy.com

2.1.3  HTTPS注意事项

        Https不支持续费,证书到期需重新申请进行替换

        https不支持三级域名解析  test.m.oldboy.com

        Https显示绿色, 说明整个网站的URL都是https的。

        Https显示×××, 因为网站代码中包含http的不安全连接。

        Https显示红色,要么证书是假的,要么证书过期。

    自己颁发的证书统统都是假的(我们只管使用)

2.1.4  配置ssl认证证书

1.检查当前环境

//openssl必须是1.0.2

[root@Nginx ~]# openssl version

OpenSSL 1.0.2k-fips  26 Jan 2017

//nginx必须有ssl模块

[root@Nginx ~]# nginx -V

 --with-http_ssl_module

[root@Nginx ~]# mkdir /etc/nginx/ssl_key -p

[root@Nginx ~]# cd /etc/nginx/ssl_key 

 2.使用openssl充当CA权威机构创建私钥(生产不可能使用此方式生成证书,不被互联网CA权威承认的黑户证书)

[root@Nginx ssh_key]# openssl genrsa -idea -out server.key 2048

3.生成自签证书,同时去掉私钥的密码

[root@Nginx ssl_key]# openssl req -days 36500 -x509 \

-sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt

4.配置Nginx

[root@web03 conf.d]# cat https.conf

server {

    listen 443;

    server_name s.oldboy.com;

    ssl on;

       ssl_certificate   ssl_key/server.crt;

       ssl_certificate_key  ssl_key/server.key;

    location / {

       root /code;

       index index.html;

    }

}

5.创建目录,启动服务

[root@web03 conf.d]# mkdir /code

[root@web03 conf.d]# echo "Https...." > /code/index.html

[root@web03 conf.d]# systemctl restart nginx

6.配置Host解析

10.0.0.9 s.oldboy.com

7.强制跳转访问http的请求至https

[root@web03 conf.d]# cat https.conf

server {

    listen 443;

    server_name s.oldboy.com;

    ssl on;

       ssl_certificate   ssl_key/server.crt;

       ssl_certificate_key  ssl_key/server.key;

    location / {

       root /code;

       index index.html;

    }

}

server {

    listen 80;

    server_name s.oldboy.com;

    rewrite (.*) https://$server_name$request_uri redirect;

}

如果添加了flag标记,则会有跳转到https的信息  301。

如果没有添加flag标记,则没有跳转https的信息  200。

2.2 rewrite

2.2.1  开启rewrite日志

    测试访问rewrite日志是否能查看到

    测试rewrite的匹配规则

    rewriteflag标记

            lastbreak

            redirectpermanent

2.2.2  https加密

        1.证书的购买

        2.https证书购买一个域名,还是多个域名,还是泛域名  oldboy.com

        3.https不支持三级域名解析


猜你喜欢

转载自blog.51cto.com/13859649/2287597
58