nginx 模块动静分离和Rewrite规则

Nginx作为静态资源web服务

静态资源web服务-CDN场景

Nginx资源存储中心会把静态资源分发给“北京Nginx”,“湖南Nginx”,“山东Nginx”。

然后北京User发送静态资源请求,通过CDN,找到离自己最近的“北京Nginx”。

静态资源核心配置

文件读取 sendfile

sendfile 是一种高效传输文件的模式.
sendfile设置为on表示启动高效传输文件的模式。sendfile可以让Nginx在传输文件时直接在磁盘和tcp socket之间传输数据。如果这个参数不开启,会先在用户空间(Nginx进程空间)申请一个buffer,用read函数把数据从磁盘读到cache,再从cache读取到用户空间的buffer,再用write函数把数据从用户空间的buffer写入到内核的buffer,最后到tcp socket。开启这个参数后可以让数据不用经过用户buffer。

sendfile语法

  • Syntax:sendfile on | off;
  • Default:sendfile off;
  • Context:httpserverlocationif in location

语法解释:

Enables or disables the use of sendfile().

Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():

配置语法-tcp_nopush

作用:

  •  sendfile开启的情况下,提高网络包的传输效率。

tcp_nopush语法

  • Syntax: tcp_nopush on | off;
  • Default:tcp_nopush off;
  • Context:http, server, location

语法解释

Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows

sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;

sending a file in full packets.

在 sendfile 开启的情况下,提高网络数据包的传输效率。
tcp_nopush指令,在连接套接字时启用Linux系统下的TCP_CORK。该选项告诉TCP堆栈附加数据包,并在它们已满或当应用程序通过显式删除TCP_CORK指示发送数据包时发送它们。 这使得发送的数据分组是最优量,并且因此提高了网络数据包的传输效率。
也就是说 tcp_nopush=on 时,结果就是数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞,虽然有一点点延迟。
 
配置语法
  • Syntax: tcp_nopush on | off;
  • Default: tcp_nopush off;
  • Context: http, server, location

配置语法-tcp_nodelay

 作用:

  • keepalive连接下,提高网络包的传输实时性。

tcp_nodelay语法

  • Syntax: tcp_nodelay on | off;
  • Default: tcp_nodelay on;
  • Context: http, server, location

在 keepalive 连接下,提高网络数据包的传输实时性。
tcp_nodelay选项和tcp_nopush正好相反,数据包不等待,实时发送给用户。

配置语法-gzip压缩

作用:

  • 压缩传输,提高传输效率。
  • 开启压缩,可以加快资源响应速度,同时节省网络带宽资源。

gzip语法

  • Syntax: gzip on | off;
  • Default: gzip off; Context:
  • http, server, location, if in location

 配置压缩比,压缩等级配置(压缩等级越高,越消耗服务器资源)

  • Syntax: gzip_comp_level level;
  • Default: gzip_comp_level 1;
  • Context: http, server, location

 gzip协议版本配置

  • Syntax: gzip_http_version 1.0 | 1.1;
  • Default: gzip_http_version 1.1;
  • Context: http, server, location

 压缩扩展模块,预读gzip功能 ngx_http_gzip_static_module

  • Syntax: gzip_static on | off | always;
  • Default: gzip_static off;
  • Context: http, server, location

 应用支持gunzip的压缩方式 ngx_http_gunzip_module

  • Syntax: gunzip on | off;
  • Default: gunzip off;
  • Context: http, server, location
  • Syntax: gunzip_buffers number size;
  • Default: gunzip_buffers 32 4k|16 8k;
  • Context: http, server, location

配置语法-gzip_static

作用:

 传输预压缩静态文件给客户端(.gz文件为预压缩)

gzip_static语法

  • Syntax: gzip_static on | off | always;
  • Default: gzip_static off;
  • Context: http, server, location

语法解释:

Enables (“on”) or disables (“off”) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary.

With the “always” value (1.3.6), gzipped file is used in all cases, without checking if the client supports it. It is useful if there are no uncompressed files on the disk anyway or the ngx_http_gunzip_module is used.

The files can be compressed using the gzip command, or any other compatible one. It is recommended that the modification date and time of original and compressed files be the same.

 gzip 压缩图片 小案例

   sendfile on;
  location ~ .*\.(jpg|gif|png)$ {
        #gzip on;
        #gzip_http_version 1.1;
        #gzip_comp_level 2;
        #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/images;
    }

访问

http://192.168.1.112/wei.png

可以看到图片的大小是 239KB

 

    sendfile on;
    location ~ .*\.(jpg|gif|png)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/images;
    }

参数解读:

  • gzip on – nginx是否开启gzip压缩功能;
  • gzip_min_length 1000 – nginx限制最小gzip压缩资源大小;
  • gzip_proxied – nginx作为反向代理时,允许gzip压缩请求或响应头里字段;
  • gzip_types – nginx允许gzip压缩的静态资源类型;
  • gzip_http_version 1.1 – nginx允许gzip压缩的HTTP请求版本;
  • gzip_comp_level – nginx允许压缩的级别,共9个等级,级别越高压缩率越大;

再次刷新页面

打开 giz 的功能大小是 182 B

 gzip 压缩文本 小案例

   sendfile on;
  location ~ .*\.(txt|xml)$ {
        #gzip on;
        #gzip_http_version 1.1;
        #gzip_comp_level 1;
        #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/doc;
    }

访问

http://192.168.1.112/access.txt

 

可以看到没开启 gzip 的大小是 175 kb

开启 gizp 功能

    sendfile on;
    location ~ .*\.(txt|xml)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 1;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/doc;
    }

再次刷新页面

 可以看到大小为 182 B

gzip 文件预读案例演示

开启gzip 预读功能

    sendfile on;
    location ~ ^/download {
        gzip_static on;
        tcp_nopush on;
        root /opt/app/code;
    }

参数解读:

  • gzip_static on – nginx是否开启预读gzip文件功能;
  • tcp_nopush on – nginx是否一次性发送整个文件,提高传输效率;
  • root /opt/app/code  –  指定根目录;

注意:代码的目录是 /opt/app/code

访问/download/test.img 的时候会在 /opt/app/code/download/ 下面去找 test.img.zp 或test.img, 出错可以通过查看错误日志

pwd
ls

 关闭 gzip_static on 后访问出错

猜你喜欢

转载自www.cnblogs.com/crazymagic/p/11020351.html
今日推荐