动静文件分离、nginx的rewrite重写与HTTPS详情
2022-01-07 22:41
886 查看
本期内容概要
- 资源分离
- nginx的rewrite重写
- HTTPS
内容详细
1、动静分离
1.创建NFS挂载点(实现文件所有用户共享) [root@nfs ~]# mkdir /static [root@nfs ~]# vim /etc/exports 添加以下内容: /static 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666) [root@nfs ~]# systemctl restart nfs-server [root@nfs ~]# chown -R www.www /static/ 2.将静态资源放置于挂载点内 [root@web01 ~]# mkdir -p /opt/static/s/ [root@web01 ~]# mount -t nfs 172.16.1.31:/static /opt/static/ [root@web01 ~]# cp -r /opt/bbs/static/* /opt/static/s/ [root@web02 ~]# mkdir -p /opt/static/s/ [root@web02 ~]# mount -t nfs 172.16.1.31:/static /opt/static/ [root@web02 ~]# ll /opt/static/s total 0 drwxr-xr-x 5 www www 40 Jan 7 18:58 bootstrap drwxr-xr-x 2 www www 23 Jan 7 18:58 css drwxr-xr-x 2 www www 168 Jan 7 18:58 font drwxr-xr-x 4 www www 30 Jan 7 18:58 img [root@web03 ~]# mkdir -p /opt/static/s/ [root@web03 ~]# mount -t nfs 172.16.1.31:/static /opt/static/ [root@web03 ~]# ll /opt/static/s total 0 drwxr-xr-x 5 www www 40 Jan 7 18:58 bootstrap drwxr-xr-x 2 www www 23 Jan 7 18:58 css drwxr-xr-x 2 www www 168 Jan 7 18:58 font drwxr-xr-x 4 www www 30 Jan 7 18:58 img 3.挂载到lb [root@lb01 ~]# mkdir /opt/static/ [root@lb01 ~]# yum install nfs-utils -y [root@lb01 ~]# mount -t nfs 172.16.1.31:/static /opt/static/ [root@lb01 opt]# ll /opt/static/s total 0 drwxr-xr-x 5 www www 40 Jan 7 18:58 bootstrap drwxr-xr-x 2 www www 23 Jan 7 18:58 css drwxr-xr-x 2 www www 168 Jan 7 18:58 font drwxr-xr-x 4 www www 30 Jan 7 18:58 img [root@lb01 conf.d]# cd /etc/nginx/conf.d [root@lb01 conf.d]# vim python.conf 保持以下内容: upstream bbs { server 172.16.1.7:80 max_fails=3 fail_timeout=3s; server 172.16.1.8:80 max_fails=3 fail_timeout=3s; server 172.16.1.9:80 max_fails=3 fail_timeout=3s; } server { listen 80; server_name py.test.com; location / { proxy_pass http://bbs; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; include /etc/nginx/proxy_params; } location ~ \.(jpg|png|gif|js|css|woff|woff2|ttf)$ { root /opt/static; } } [root@lb01 conf.d]# nginx -t 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 [root@lb01 conf.d]# systemctl restart nginx 4.测试 如下图 输入域名:py.test.com 找到图片名 右键选择第一个选项 新窗口打开 显示出图片 代表成功 最终所有图片都可以正常显示 ''' 重启服务器之后要执行: [root@db01 ~]# systemctl restart mariadb [root@lb01 ~]# systemctl restart nginx web中启动: cd /opt/bbs/ uwsgi -d --ini myweb_uwsgi.ini --uid 666 ... '''
2、Rewrite基本概述
Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程 # 1.基本概述 01 地址跳转 用户访问www.linux.com这个URL是,将其定向至一个新的域名www.baidu.com 02 协议跳转 用户通过http协议请求网站时,将其重新跳转至https协议方式 03 伪静态 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性 04 搜索引擎 SEO优化依赖于url路径,好记的url便于搜索引擎录入 # 2.rewrite语法 Syntax : rewrite regex replacement [flag]; Default : — Context : server, location, if rewrite : 模块命令 regex : 请求的链接(支持正则表达式) replacement : 跳转的链接 [flag]; : 标签 例如: location /download/ { rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403; } # 3.Rewrite标记Flag rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
flag | 作用 |
---|---|
last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
2.1、测试 last break
# 测试 last break [root@db01 ~]# cd /etc/nginx/conf.d [root@lb01 conf.d]# ll total 8 -rw-r--r-- 1 root root 253 Jan 6 22:53 game.conf -rw-r--r-- 1 root root 515 Jan 7 19:15 python.conf [root@lb01 conf.d]# gzip game.conf [root@lb01 conf.d]# vim flag.conf server { server_name _; listen 80; location ~ ^/break { rewrite (.*) /test break; } location ~ ^/last { rewrite (.*) /test last; } location /test { default_type text/html; return 200 "test"; } } [root@lb01 conf.d]# nginx -t [root@lb01 conf.d]# systemctl restart nginx 浏览器访问: 192.168.15.5/last 回显结果 test 浏览器访问: 192.168.15.5/break 回显结果 404 # 底层原理 # break请求: 1.请求linux.rewrite.com/break 2.匹配 location ~ ^/break 会跳转到 linux.rewrite.com/test 3.请求跳转后,回去查找本地站点目录下的 /test 4.如果找到了,则返回/code/test/index.html的内容; 5.如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403 # last请求: 1.请求linux.rewrite.com/last 2.匹配 location ~ ^/last 会跳转到 linux.rewrite.com/test 3.如果找到了,则返回/code/test/index.html的内容; 4.如果没有找到,会重新对当前server发起请求,这个时候访问地址就变成 linux.rewrite.com/test 5.重新请求server会匹配到 location /test/ 直接返回该location的内容 6.如果也没有location匹配,再返回404;
2.2、测试 redirect 和 permanent
# 测试redirect和permanent的区别 重定向 [root@lb01 conf.d]# vim flag.conf 添加下面内容: location /redirect { rewrite (.*) http://www.baidu.com redirect; } location /permanent { rewrite (.*) http://www.baidu.com permanent; } [root@lb01 conf.d]# nginx -t [root@lb01 conf.d]# systemctl restart nginx 浏览器访问: 192.168.15.5/redirect 浏览器访问: 192.168.15.5/permanent 回显结果都是百度 关闭nginx 再测试 [root@lb01 conf.d]# systemctl stop nginx 浏览器访问: 192.168.15.5/redirect 回显结果 报错无法访问 浏览器访问: 192.168.15.5/redirect 回显结果 正常显示百度 # 底层原理 redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败 permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转
2.3、案例
# 在负载均衡中,将小游戏动静分离 nfs 中要有/opt/img目录 并包含小游戏的图片文件 [root@lb01 ~]# mkdir /opt/img [root@lb01 ~]# mount -t nfs 172.16.1.31:/opt/img /opt/img [root@lb01 ~]# vim /etc/nginx/conf.d/game.conf 添加以下文件内容 location ~ \.(gif|png|jpg)$ { root /opt/img; } [root@lb01 ~]# nginx -t [root@lb01 ~]# systemctl restart nginx
3、HTTPS
为什么需要使用HTTPS: 因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露 那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议 那么TLS是怎么保证明文消息被加密的呢? 在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,使应用层http协议没有感知的情况下做到了数据的安全加密
3.1、模拟网站劫持
3.1.1、配置正常网页
[root@web01 ~]# cd /opt [root@web01 opt]# mkdir code [root@web01 opt]# cd code [root@web01 code]# ll total 0 [root@web01 code]# vim index.html 写入以下内容 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学生信息注册页面</title> </head> <body> <h3 align="center">学生信息注册</h3> <form name="stu"action=""> <table> <tr><td>姓名:</td><td><input type="text"name="stuName"/></td></tr> <tr><td>性别:</td> <td><input type="radio"name="stuSex"checked="checked">男 <input type="radio"name="stuSex">女 </td> </tr> <tr><td>出生日期</td> <td><input type="text"name="stuBirthday"></td> <td>按格式yyyy-mm-dd</td> </tr> <tr><td>学校:</td><td><input type="text"name="stuSchool"></td></tr> <tr><td>专业:</td> <td><select name="stuSelect2"> <option selected>计算机科学与技术</option> <option>网络工程</option> <option>物联网工程</option> <option>应用数学</option> </select> </td> </tr> <tr><td>体育特长:</td> <td colspan="2"> <input type="checkbox"name="stuCheck" >篮球 <input type="checkbox"name="stuCheck" >足球 <input type="checkbox"name="stuCheck" >排球 <input type="checkbox"name="stuCheck" >游泳 </td> </tr> <tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr> <tr><td>密码:</td><td><input type="password"name="stuPwd" ></td></tr> <tr><td>个人介绍:</td> <td colspan="2"><textarea name="Letter"rows="4"cols="40"></textarea></td> </tr> <tr> <td><input type="submit"value="提交" ><input type="reset"value="取消" ></td> </tr> </table> </form> </body> </html> [root@web01 code]# cd /etc/nginx/conf.d [root@web01 conf.d]# ll total 44 -rw-r--r-- 1 root root 214 Jan 5 17:42 bbs.conf.gz -rw-r--r-- 1 root root 472 Nov 16 23:02 default.conf.gz -rw-r--r-- 1 root root 128 Jan 4 10:55 game1.conf.gz -rw-r--r-- 1 root root 162 Jan 4 16:44 game2.conf.gz -rw-r--r-- 1 root root 153 Jan 4 16:55 game3.conf.gz -rw-r--r-- 1 root root 152 Jan 4 17:02 game4.conf.gz -rw-r--r-- 1 root root 129 Jan 6 16:40 game5.conf -rw-r--r-- 1 root root 127 Dec 31 18:19 game.conf.gz -rw-r--r-- 1 root root 220 Jan 5 16:37 py.conf.gz -rw-r--r-- 1 root root 330 Jan 6 23:54 python.conf -rw-r--r-- 1 root root 272 Jan 5 14:58 test.conf.gz [root@web01 conf.d]# gzip game5.conf [root@web01 conf.d]# vim https.conf server { listen 80; server_name _; location / { root /opt/code; index index.html; } } [root@web01 conf.d]# nginx -t [root@web01 conf.d]# systemctl restart nginx 测试: 浏览器访问 192.168.15.7 显示下图即正常
3.1.2、模拟网站劫持
[root@lb01 conf.d]# cd /etc/nginx/conf.d [root@lb01 conf.d]# ll total 12 -rw-r--r-- 1 root root 432 Jan 7 21:18 flag.conf -rw-r--r-- 1 root root 253 Jan 6 22:53 game.conf -rw-r--r-- 1 root root 515 Jan 7 19:15 python.conf [root@lb01 conf.d]# gzip flag.conf game.conf [root@lb01 conf.d]# ll total 12 -rw-r--r-- 1 root root 203 Jan 7 21:18 flag.conf.gz -rw-r--r-- 1 root root 166 Jan 6 22:53 game.conf.gz -rw-r--r-- 1 root root 515 Jan 7 19:15 python.conf [root@lb01 conf.d]# vim https.conf server { listen 80; server_name _; location / { proxy_pass http://192.168.15.7; } } [root@lb01 conf.d]# nginx -t [root@lb01 conf.d]# systemctl restart nginx 测试: 浏览器访问 192.168.15.5 显示下图即正常
[root@lb01 conf.d]# vim https.conf server { listen 80; server_name _; location / { proxy_pass http://192.168.15.7; sub_filter '<title>学生信息注册页面</title>' '<title>澳门首家线上赌场</title>'; sub_filter '<h3 align="center">学生信息注册</h3>' '<h3 align="center">VIP用户信息注册</h3>'; sub_filter '<tr><td>性别:</td>' '<tr><td>爱好:</td>'; sub_filter '<option selected>计算机科学与技术</option>' '<option selected>按摩</option>'; sub_filter '<option>网络工程</option>' '<option>抽烟</option>'; sub_filter '<option>物联网工程</option>' '<option>喝酒</option>'; sub_filter '<option>应用数学</option>' '<option>烫头</option>'; sub_filter '<tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>' '<img src="https://blog.driverzeng.com/zenglaoshi/xingganheguan.gif">'; } } [root@lb01 conf.d]# cd [root@lb01 nginx-1.20.2]# cd nginx-1.20.2 [root@lb01 nginx-1.20.2]# ./configure --with-http_gzip_static_module --with-stream --with-http_ssl_module --with-http_sub_module [root@lb01 nginx-1.20.2]# make [root@lb01 nginx-1.20.2]# make install [root@lb01 nginx-1.20.2]# cd /usr/local/nginx/sbin [root@lb01 sbin]# ll total 6944 -rwxr-xr-x 1 root root 7110376 Jan 7 21:38 nginx [root@lb01 sbin]# rm -rf /usr/sbin/nginx [root@lb01 sbin]# mv nginx /usr/sbin/nginx [root@lb01 sbin]# ll total 0 [root@lb01 sbin]# nginx -t [root@lb01 sbin]# systemctl restart nginx 测试: 浏览器访问 192.168.15.5 显示下图即 网站被攻击了
3.2、加密流程
1、浏览器发起往服务器的443端口发起请求,请求携带了浏览器支持的加密算法和哈希算法 2、服务器收到请求,选择浏览器支持的加密算法和哈希算法 3、服务器下将数字证书返回给浏览器,这里的数字证书可以是向某个可靠机构申请的,也可以是自制的 4、浏览器进入数字证书认证环节,这一部分是浏览器内置的TLS完成的: 4.1 首先浏览器会从内置的证书列表中索引,找到服务器下发证书对应的机构,如果没有找到,此时就会提示用户该证书是不是由权威机构颁发,是不可信任的。如果查到了对应的机构,则取出该机构颁发的公钥 4.2 用机构的证书公钥解密得到证书的内容和证书签名,内容包括网站的网址、网站的公钥、证书的有效期等。浏览器会先验证证书签名的合法性(验证过程类似上面Bob和Susan的通信)。签名通过后,浏览器验证证书记录的网址是否和当前网址是一致的,不一致会提示用户。如果网址一致会检查证书有效期,证书过期了也会提示用户。这些都通过认证时,浏览器就可以安全使用证书中的网站公钥了 4.3 浏览器生成一个随机数R,并使用网站公钥对R进行加密 5、浏览器将加密的R传送给服务器 6、服务器用自己的私钥解密得到R 7、服务器以R为密钥使用了对称加密算法加密网页内容并传输给浏览器 8、浏览器以R为密钥使用之前约定好的解密算法获取网页内容
3.3、证书对比
对比 | 域名型 DV | 企业型 OV | 增强型 EV |
---|---|---|---|
绿色地址栏 | 小锁标记+https | 小锁标记+https | 小锁标记+企业名称+https |
一般用途 | 个人站点和应用; 简单的https加密需求 | 电子商务站点和应用; 中小型企业站点 | 大型金融平台; 大型企业和政府机构站点 |
审核内容 | 域名所有权验证 | 全面的企业身份验证; 域名所有权验证 | 最高等级的企业身份验证; 域名所有权验证 |
颁发时长 | 10分钟-24小时 | 3-5个工作日 | 5-7个工作日 |
单次申请年限 | 1年 | 1-2年 | 1-2年 |
赔付保障金 | —— | 125-175万美金 | 150-175万美金 |
3.4、自签证书
1.使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书) [root@web01 ~]# cd /etc/nginx [root@web01 nginx]# mkdir ssl [root@web01 nginx]# cd ssl [root@web01 nginx]# openssl genrsa -idea -out server.key 2048 回显内容: Generating RSA private key, 2048 bit long modulus ...............................+++ ........+++ e is 65537 (0x10001) Enter pass phrase for server.key:123456 # 密码自己输入 Verifying - Enter pass phrase for server.key:123456 # 密码自己输入 2.生成自签证书(公钥),同时去掉私钥的密码 [root@web01 ssl]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt 回显内容: Generating a 2048 bit RSA private key ......+++ .........................................+++ writing new private key to 'server.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]: # 直接回车 State or Province Name (full name) []: # 直接回车 Locality Name (eg, city) [Default City]: # 直接回车 Organization Name (eg, company) [Default Company Ltd]: # 直接回车 Organizational Unit Name (eg, section) []: # 直接回车 Common Name (eg, your name or your server's hostname) []: # 直接回车 Email Address []: # 直接回车 # req --> 用于创建新的证书 # new --> 表示创建的是新证书 # x509 --> 表示定义证书的格式为标准格式 # key --> 表示调用的私钥文件信息 # out --> 表示输出证书文件信息 # days --> 表示证书的有效期 # sha256 --> 加密方式 [root@web01 ssl]# ll total 8 -rw-r--r-- 1 root root 1220 Jan 7 21:57 server.crt -rw-r--r-- 1 root root 1704 Jan 7 21:57 server.key 3.开启证书 [root@web01 ssl]# cd /etc/nginx/conf.d [root@web01 conf.d]# vim https.conf 保持以下内容: server { listen 443 ssl; server_name _; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; location / { root /opt/code; index index.html; } } ''' Syntax: ssl on | off; Default: ssl off; Context: http, server Syntax: ssl_certificate file; Default: — Context: http, server Syntax: ssl_certificate_key file; Default: — Context: http, server ''' [root@web01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 conf.d]# systemctl restart nginx 测试: 浏览器访问 https://192.168.15.7 显示为下图(虽然不安全 但是有证书)
# 给所有网站同创建证书 [root@web01 conf.d]# scp https.conf 192.168.15.5:/etc/nginx/conf.d/ [root@web01 conf.d]# cd .. [root@web01 nginx]# scp -r /etc/nginx/ssl 192.168.15.5:/etc/nginx/ # lb01 [root@lb01 nginx]# cd /etc/nginx/conf.d [root@lb01 conf.d]# vim https.conf 保持为以下内容: upstream ssl { server 172.16.1.7; server 172.16.1.8; server 172.16.1.9; } server { listen 443 ssl; server_name _; ssl_certificate /etc/nginx/ssl/server.crt; ssl_certificate_key /etc/nginx/ssl/server.key; location / { proxy_pass http://ssl; include /etc/nginx/proxy_params; } } [root@lb01 conf.d]# nginx -t 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 [root@lb01 conf.d]# systemctl restart nginx # 避免冲突 web01要更改配置 [root@web01 ~]# cd /etc/nginx/conf.d [root@web01 conf.d]# vim https.conf 改为以下内容: server { # listen 443 ssl; listen 80; server_name _; # ssl_certificate /etc/nginx/ssl/server.crt; # ssl_certificate_key /etc/nginx/ssl/server.key; location / { root /opt/code; index index.html; } } [root@web01 conf.d]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@web01 conf.d]# systemctl restart nginx 测试: 浏览器访问 https://192.168.15.5/ 会循环显示小游戏和学生信息注册 即正常 如下图
# 优化 在 lb01中 [root@lb01 conf.d]# vim https.conf 添加以下内容: server { listen 80; server_name _; rewrite (.*) https://192.168.15.5 permanent; } [root@lb01 conf.d]# nginx -t 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 [root@lb01 conf.d]# systemctl restart nginx 测试: 浏览器访问 192.168.15.5 会自动访问到 https://192.168.15.5/ 就实现了自动跳转 https
相关文章推荐
- nginx反向代理tomcat、实现动静分离/配置文件
- nginx配置文件(反向代理+集群+动静分离)
- nginx详解(三)—FastCGI、https、动静分离、proxy、upstream和cache
- Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)
- nginx 简介、原理、配置文件、负载均衡、反向代理、动静分离、高可用。
- nginx动静分离后个别静态文件代理至后端
- nginx实现动静分离--附nginx配置文件详解
- Nginx之动静分离
- (总结)Nginx与Apache、Tomcat、Resin动静分离核心配置
- Nginx的特性功能-反向代理、负载均衡、缓存、动静分离、平滑升级
- 【架构】 Nginx动静分离
- Nginx动静分离经典案例配置
- keepalived(nginx)+httpd实现动静分离
- Nginx + Tomcat 动静分离
- nginx实战操作之动静分离
- Haproxy+Nginx+Tomcat实现动静分离页面
- nginx实现tomcat动静分离详解
- nginx 配置文件 PATHINFO重写规则
- nginx系列3——nginx反向代理、动静分离、负载均衡
- nginx rewrite重写规则配置详解