美文网首页
nginx开启gzip缓存 2019-01-24

nginx开启gzip缓存 2019-01-24

作者: 小知大知 | 来源:发表于2019-01-24 11:40 被阅读10次

配置缓存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;
    #    }
    #}
}

验证:

图片.png

测试 移动网络热点测试

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


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


    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

相关文章

  • nginx常用配置

    gzip压缩 开启缓存 隐藏nginx版本信息 常用配置

  • nginx开启gzip缓存 2019-01-24

    配置缓存nginx.conf 验证: 测试 移动网络热点测试 没加入nginx8s以上的文件a.png 加入ngi...

  • 【Nginx】开启 gzip和缓存

    Nginx 开启 gzip和缓存 时间:2016-09-23 16:42:37 nginx 是一个高性能的 Web...

  • vue打包体积大:加载时间长

    加速nginx: 开启gzip和缓存 nginx 是一个高性能的 Web 服务器,之前也写过一些关于 nginx ...

  • 前端性能优化

    组件的按需引入 webpack打包 js切割 静态资源使用cnd nginx 开启gzip压缩 合理利用缓存

  • Nginx 常用配置

    Nginx 相关配置, 包括开启 gzip,设置缓存时间,包括前后端开发设置代理来避免跨域等 友情链接 Nginx...

  • nginx的Gzip配置

    1 nginx的Gzip配置 1.1 gzip的指令 1.gzip :该指令用于开启或者关闭Gzip 功能 默认情...

  • Vue开启Gzip Nginx开启Gzip

    安装compression-webpack-plugin vue.config.js配置Gzip压缩 配置Nginx

  • 页面打开速度优化

    1、开启gzip压缩,浏览器是可以识别gzip压缩的。现在的服务基本上都使用nginx做转发,开启gzip也是很容...

  • nginx开启gzip

    1.开启gzip nginx实现资源压缩的原理是通过ngx_http_gzip_module模块拦截请求,并对需要...

网友评论

      本文标题:nginx开启gzip缓存 2019-01-24

      本文链接:https://www.haomeiwen.com/subject/rtfyjqtx.html