Nginx rewrite模块详解、expires缓存配置及gzip压缩策略

原文链接: https://blog.csdn.net/wangpf2011/article/details/85108207

一、rewrite使用
rewrite的主要功能是实现URI地址的重定向,将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI,即使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端。

1)rewrite指令语法结构:rewrite regex replacement [flag]

rewrite使用位置:server{}, location{}, if{}

regex 常用正则表达式说明:

flag标记说明:

2)if指令语法结构:if (condition) { ... }

引入新的上下文,条件满足时,执行配置块中的配置指令,大括号内的rewrite指令将被执行。if使用位置:server{}, location{}

condition条件说明:

3)set指令语法结构:set variable value;

用户自定义变量,变量定义和调用都要以$开头。set使用位置:server{}, location{}, if{}

4)return指令语法结构:return code [text];

停止处理并返回指定响应码给客户。return使用位置:server{}, location{}, if{}

rewrite使用示例:

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  myformat  '$remote_addr - $remote_user [$time_local] "$request" ';
    access_log  logs/my.log  myformat;
    sendfile        on;
    keepalive_timeout  65;
 
    server {
        listen       8003;
        server_name  www.wf.com;
        location / {
            rewrite '^/images/(.*)\.(png|jpg)$' /img?file=$1.$2;
        set $image_file $1;
        set $image_type $2;
        }
    location /img {
        root html;
        try_files /$arg_file /image404.html;
    }
    location /image404.html {
        return 404 "image not found exception";
    }
    }    
}


如上配置中/images/feixiang.jpg会重写到/img?file=feixiang.jpg,于是匹配到 location /img。然后通过try_files获取存在的文件进行返回,如果文件不存在则直接返回404错误。

表面看rewrite和location功能有点像,都能实现跳转,其主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。很多情况下rewrite也会写在location里,它们的执行顺序是:(1)执行server块的rewrite指令;(2)执行location匹配;(3)执行选定的location中的rewrite指令。如果其中某步URI被重写,则重新循环执行(1)~(3),直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。

二、浏览器本地缓存配置及动静分离
expires语法: expires 60s|m|h|d

expires使用位置:location{}

expires使用示例:

http {
    ......
    server {
        listen       8004;
        server_name  www.wf.com;
        location / {
            root html;
            index index.html index.htm;
        }
        location ~ \.(png|jpg|js|css|gif) {
            root html/images;
            expires 5m;
        }
    }
}

(1)在html目录下创建一个images文件,在该文件中放一张图片

(2)修改index.html, 增加<img src="feixiang.jpg"  alt="泉城广场" />

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

三、gzip压缩策略
浏览器请求url,同时声明当前浏览器可以支持压缩类型(gzip、deflate等),服务端会把内容根据浏览器所支持的压缩策略去进行压缩并返回给浏览器,浏览器拿到数据以后进行解码。

gzip使用示例:

http {
    ......
    server {
        listen       8004;
        server_name  www.wf.com;
        gzip on;
        gzip_buffers 4 16k;
        gzip_comp_level 7;
        gzip_min_length 500;
        gzip_types text/css text/xml application/javascript;
 
        location / {
            root html;
            index index.html index.htm;
        }
        location ~ \.(png|jpg|js|css|gif) {
            root html/images;
            expires 5m;
        }
    }
}

gzip语法说明:

语法

描述

gzip on|off

是否开启gzip压缩

gzip_buffers 4 16k

设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k代表以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。

gzip_comp_level [1-9]

压缩级别,级别越高,压缩越小,但是会占用CPU资源

gzip_disable

正则匹配,表示什么样的浏览器不进行gzip

gzip_min_length

开始压缩的最小长度(小于多少就不做压缩)

gzip_http_version 1.0|1.1

表示开始压缩的http协议版本

gzip_proxied

nginx 做前端代理时启用该选项,表示无论后端服务器的headers头返回什么信息,都无条件启用压缩

gzip_types text/pliain,application/xml

对那些类型的文件做压缩 (类型参考文件conf/mime.conf)

gzip_vary on|off

是否传输gzip压缩标识

gzip使用注意事项:

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

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

(3)Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,需要我们进行手动配置。

猜你喜欢

转载自blog.csdn.net/IT_Luobin/article/details/102644459