巨人的肩膀
具体操作
- 确定nginx配置文件路径:
输入: nginx -t 结果: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 则nginx配置文件的全路径为:/usr/local/nginx/conf/nginx.conf
- 在nginx文件中开启gzip压缩 并添加压缩文件类型等配置,开启gzip压缩的可选范围:所有域名、某个域名、某个location,对应需添加gzip配置的位置为:http中、server中、location中,下面介绍http中和location中的配置:
- 2-1【http中】先找到 http模块中,gzip配置,增加下面的配置即可:
# 启用压缩,提高资源静态资源获取速度 gzip on; gzip_disable "msie6"; gzip_min_length 20; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/javascript text/xml application/xml+rss image/png image/x-ms-bmp;
- 2-2、【location中】在location中添加一下配置即可:
location ~.*.(html|js|css|txt|xml|png|bmp)$ { root /data/wwwroot/test/pro/static/assets/; # 启用压缩,提高资源静态资源获取速度 gzip on; gzip_disable "msie6"; gzip_min_length 20; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/javascript text/xml application/xml+rss image/png image/x-ms-bmp; }
-
nginx重新加载配置
# 先确认配置是否有错,如有错按提示修改即可 nginx -t # 重载配置 nginx -s reload
-
验证:
到测速平台输入资源地址并验证即可,可知道是否压缩、压缩类型、压缩率等信息