[Nginx]rewrite使用、浏览器本地缓存配置及动静分离

高性能反向代理服务器–Nginx

本文简述:rewrite使用、浏览器本地缓存配置及动静分离

个人学习笔记,如有不足,欢迎指正。 2018-5-22

一、内容

1、Rewrite的使用

2、缓存配置及Gzip配置


二、笔记
1.1)rewrite的使用

rewrite通过ngx_http_rewrite_module模块支持url重写、支持if判断,但不支持else.

rewrite功能是:使用nginx提供的全局变量或子集设置的变量,结合正则表达式和标志位实现url重写以及重定向。

rewrite只能是放在server{},location{},if{}中 ,并且只能对域名后边的除去传递的参数外的字符串起作用。

1.2)常用指令

if 空格 (条件) {设定条件进行重写}

条件语法

  • “=” 判断相等,用于字符比较。

  • “~”用正则来匹配 (表示区分大小写),“~*” 不区分大小写

  • “-f -d -e”来判断是否为文件、目录、是否存在

    关于if (条件)中的条件,具体还有那些功能大家可以参见官方文档。

    以下是这段文字是Module ngx_http_rewrite_module 中的内容。

    A condition may be any of the following:

    a variable name; false if the value of a variable is an empty string or “0”;
    Before version 1.0.1, any string starting with “0” was considered a false value.
    comparison of a variable with a string using the “=” and “!=” operators;
    matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~*” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the 1.. 1.. 9 variables. Negative operators “!~” and “!~*” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
    checking of a file existence with the “-f” and “!-f” operators;
    checking of a directory existence with the “-d” and “!-d” operators;
    checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;
    checking for an executable file with the “-x” and “!-x” operators.

return指令

语法return code;

停止出来并返回指定状态码给客户端。

if($request_uri ~*\.sh){
    return 403
}

set指令

set variable value;

定义一个变量并复制,值可以是文本、变量或者文本变量混合体。

rewrite指令

语法rewrite regex replacement [flag]{last / break/ redirect 返回临时302/ permant 返回永久302}

last:停止处理后续的rewrite指令集、然后对当前重写的url在rewrite指令集上重新查找。

break:停止出来后续的rewrite指令集,并不会重新查找。

示例

  • 拦截以/开头的url ,重定向到百度网页。

    location / {
     #   root html;
     #   index index.html;
         rewrite ^/ http://www.baidu.com;   #重定向 
    }
  • 正则匹配url中请求的地址,假设我们请求的地址是192.168.0.85/images/www/wcl.png 会重写到/mic?file=wcl.png,于是变匹配到location /mic;通过try_files获取存在的文件进行返回。如果存在这个文件 便显示,如果不存在,接着向下匹配location = /image404.html最终返回404错误。

    location / {
             rewrite '^/images/([a-z]{3})/(.*)\.(png)$' /mic?file=$2.$3 last;##注意 此处last可以不写 也可以写break 作用不同
             set $image_file $2;
             set $image_file $3; 
         }
         location /mic {     
            root html;
            try_files /$arg_file /image404.html;
         }
         location = /image404.html {
              return 404  "image not found exception";
         }

    注意:上述代码中我添加注释的部分:如果是last,最后运行结果和不加last效果一样,显示结果404image not found exception ;如果是break,最终显示的结果是:404 Not Found

rewrite匹配规则

似乎我们看到rewritelocation功能相似,都实现了跳转。在这里我们要重要强调rewritelocation本质区别。

rewrite是在同一域名内,更改获取资源的路径。

location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。

在实际应用中rewrite也会写在location中,执行顺序是:

  • 执行server块的rewrite指令。

  • 执行location匹配

  • 执行选定的location中的rewrite指令

如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误


2.1)浏览器本地缓存配置及动静分离

语法expires 60s|m|h|d

示例:

  • 在这里,我在html目录下创建了一个images文件夹,该文件夹下有一张图片。

  • 修改index.html, 增加<img src=”图片”/>

  • 修改nginx.conf配置。配置两个location实现动静分离,并且在静态文件中增加expires的缓存期限 为5min。

  • location /{
            root html;
            index index.html index.htm;
    }
      location ~ \.(png|js|jpg|gif|css)$ {
             root html/images;
             expires 5m;   ##静态资源缓存  缓存在浏览器中 5min中之内不去去找服务器找这个文件
      }
  • 好了 运行nginx,在地址栏输入地址请求 。f12

  • 这里写图片描述

  • 第二次访问

  • 这里写图片描述

  • 其实我们可以f12查看这张图片的在此请求的时间,而不是从浏览器缓存中读取:

  • 这里写图片描述

2.2)Gzip压缩策略

浏览器请求–>告诉服务器当前浏览器可以致辞压缩类型–>服务端会把内容根据浏览器所所支持的压缩策略去进行压缩返回

–>浏览器拿到数据后解码;

常见的压缩方式:gzipdeflatesdch

在这里我看一下我的chrome的解压方式。

这里写图片描述

示例:

http {
    include                             mime.types;
    default_type                        application/octet-stream;
    sendfile                            on;
    keepalive_timeout                   65s;

    server {
        listen       80;
        server_name  localhost 192.*;

         gzip off;  #开启
         gzip_buffers 4 16k;  # 以16k为单位申请4倍的压缩使用内存 
         gzip_comp_level 4; # 压缩级别  级别越高  最大到9   越高越容易失真 同时 压缩涉及到运算 影响cpu性能

         gzip_min_length 500;# 压缩时 这个文件达到最小的长度不被压缩。
         gzip_types text/css text/xml application/javascript; #针对于那些类型的文件进行压缩 mime.types
        ## 浏览器缓存
        location /{
            root html;
            index index.html index.htm;
        }
        location ~ \.(png|png|js|jpg|gif|css)$ {
            root html/images;
            expires 5m;   ##静态资源缓存  缓存在浏览器中 5min中之内不去去找服务器找这个文件

        }
    }


}    

注意:

1、图片、mp3这样的二进制文件,没有必要进行压缩处理。因为这类文件压缩比很小,压缩过程会耗费CPU资源

2、太小的文件没有必要压缩,因为压缩以后会增加一些头信息,反而会当导致文件变大

3、Nginx默认只对text/html进行压缩,如果要对html之外的内容进行压缩传输,需要我们手动配置。配置类型可以参见mime.types


参考链接:
1.Nginx中文文档
2.nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值