nginx开启gzip缓存 2019-01-24

配置缓存nginx.conf


#user  nobody;
worker_processes  1;

#error_log  /opt/proj/nginx/logs/error.log;
#error_log  /opt/proj/nginx/logs/error.log  notice;
#error_log  /opt/proj/nginx/logs/error.log  info;

pid        /opt/nginx/nginx.pid;


events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    
    #以下是开启gzip压缩缓存start
    gzip on;
    #该指令用于开启或关闭gzip模块(on/off)

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

    gzip_comp_level 6;
    #gzip压缩比,数值范围是1-9,1压缩比最小但处理速度最快,9压缩比最大但处理速度最慢

    gzip_http_version 1.1;
    #识别http的协议版本

    gzip_min_length 256;
    #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。这里我设置了为256

    gzip_proxied any;
    #这里设置无论header头是怎么样,都是无条件启用压缩

    gzip_vary on;
    #在http header中添加Vary: Accept-Encoding ,给代理服务器用的

    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    #进行压缩的文件类型,这里特别添加了对字体的文件类型

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    #禁用IE 6 gzip
    #开启gzip压缩缓存end
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
          proxy_pass http://127.0.0.1:8080;  

        }

        location /distc/ {
            #所有静态文件直接读取硬盘
            root   /opt/proj/;
            autoindex   on;
            #缓存30天
            expires 30d;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

验证:

9877466-698fee86020de338.png
图片.png

测试 移动网络热点测试

  • 没加入nginx
    8s以上的文件

    9877466-bfe05c6ce26a68b4.png
    a.png
  • 加入nginx并且开启nginx的gzip缓存

    9877466-7c5c84ed1c46dfce.png
    b.png
pdfmake.min.js时间:18.03s-->10.83,少了8s左右
vfs_fonts.js时间: 24.53s--> 11.82s ,少 13s左右
daterangepicker.js时间: 21.66s—> 3.77s ,少了17s

参考地址:https://www.cnblogs.com/mitang/p/4477220.html

转载自:https://blog.csdn.net/weixin_34174105/article/details/87105638

You may also like...