1. 配置缓存nginx.conf

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log /opt/proj/nginx/logs/error.log;
  4. #error_log /opt/proj/nginx/logs/error.log notice;
  5. #error_log /opt/proj/nginx/logs/error.log info;
  6. pid /opt/nginx/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. #以下是开启gzip压缩缓存start
  23. gzip on;
  24. #该指令用于开启或关闭gzip模块(on/off)
  25. gzip_buffers 16 8k;
  26. #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。16 8k代表以8k为单位,安装原始数据大小以8k为单位的16倍申请内存
  27. gzip_comp_level 6;
  28. #gzip压缩比,数值范围是1-9,1压缩比最小但处理速度最快,9压缩比最大但处理速度最慢
  29. gzip_http_version 1.1;
  30. #识别http的协议版本
  31. gzip_min_length 256;
  32. #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是0,不管页面多大都压缩。这里我设置了为256
  33. gzip_proxied any;
  34. #这里设置无论header头是怎么样,都是无条件启用压缩
  35. gzip_vary on;
  36. #在http header中添加Vary: Accept-Encoding ,给代理服务器用的
  37. gzip_types
  38. text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  39. text/javascript application/javascript application/x-javascript
  40. text/x-json application/json application/x-web-app-manifest+json
  41. text/css text/plain text/x-component
  42. font/opentype font/ttf application/x-font-ttf application/vnd.ms-fontobject
  43. image/x-icon;
  44. #进行压缩的文件类型,这里特别添加了对字体的文件类型
  45. gzip_disable "MSIE [1-6]\.(?!.*SV1)";
  46. #禁用IE 6 gzip
  47. #开启gzip压缩缓存end
  48. server {
  49. listen 80;
  50. server_name localhost;
  51. #charset koi8-r;
  52. #access_log logs/host.access.log main;
  53. location / {
  54. proxy_pass http://127.0.0.1:8080;
  55. }
  56. location /distc/ {
  57. #所有静态文件直接读取硬盘
  58. root /opt/proj/;
  59. autoindex on;
  60. #缓存30天
  61. expires 30d;
  62. }
  63. #error_page 404 /404.html;
  64. # redirect server error pages to the static page /50x.html
  65. #
  66. error_page 500 502 503 504 /50x.html;
  67. location = /50x.html {
  68. root html;
  69. }
  70. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  71. #
  72. #location ~ \.php$ {
  73. # proxy_pass http://127.0.0.1;
  74. #}
  75. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  76. #
  77. #location ~ \.php$ {
  78. # root html;
  79. # fastcgi_pass 127.0.0.1:9000;
  80. # fastcgi_index index.php;
  81. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  82. # include fastcgi_params;
  83. #}
  84. # deny access to .htaccess files, if Apache's document root
  85. # concurs with nginx's one
  86. #
  87. #location ~ /\.ht {
  88. # deny all;
  89. #}
  90. }
  91. # another virtual host using mix of IP-, name-, and port-based configuration
  92. #
  93. #server {
  94. # listen 8000;
  95. # listen somename:8080;
  96. # server_name somename alias another.alias;
  97. # location / {
  98. # root html;
  99. # index index.html index.htm;
  100. # }
  101. #}
  102. # HTTPS server
  103. #
  104. #server {
  105. # listen 443 ssl;
  106. # server_name localhost;
  107. # ssl_certificate cert.pem;
  108. # ssl_certificate_key cert.key;
  109. # ssl_session_cache shared:SSL:1m;
  110. # ssl_session_timeout 5m;
  111. # ssl_ciphers HIGH:!aNULL:!MD5;
  112. # ssl_prefer_server_ciphers on;
  113. # location / {
  114. # root html;
  115. # index index.html index.htm;
  116. # }
  117. #}
  118. }

2. 验证

nginx开启gzip缓存 - 图1

3.测试

  1. 没加入nginx 8s以上的文件

nginx开启gzip缓存 - 图2
2.加入nginx并且开启nginx的gzip缓存
nginx开启gzip缓存 - 图3
pdfmake.min.js时间:18.03s—>10.83,少了8s左右
vfs_fonts.js时间: 24.53s—> 11.82s ,少 13s左右
daterangepicker.js时间: 21.66s—> 3.77s ,少了17s