vue配置gzip压缩,nginx也开启gzip,但是nginx中gzip_static不能与try_files共存是什么问题?

新手上路,请多包涵

nginx中配置如下
location / {

#root   html;
root /usr/local/nginx/html;
index  index.html index.htm;
try_files $uri $uri/ /index.html; 
gzip_static on;

}

访问页面打开时空白页,调试查看index中的dist/build.js并没有正确的加载。

于是将try_files $uri $uri/ /index.html; 注释掉,页面时可以访问,.gz文件也能解压。但是注释掉后刷新带路径及参数的网址自然不能访问。

又将gzip_static on;注释掉,try_files $uri $uri/ /index.html; 取消注释。页面也能正确访问,但是不支持gz解压。

请问这种问题应该怎么解决?

阅读 4.1k
2 个回答
新手上路,请多包涵

两个办法:

  1. 不用 try_files 用 if + rewrite
  2. 配置 gzip_static always + gunzip on;,给 .gz 文件创建一个空的未压缩文件。例如在 build.js.gz 同目录创建一个空的 build.js
新手上路,请多包涵

把压缩的资源(js,css)都放到同一个文件夹,nginx匹配上了,不用try_files,html不压缩,用try_files

location /assets/ {
    root /shared/httpd/test/;
    gzip_static on;
}

location / {
    root /shared/httpd/test/;
    try_files $uri $uri/ /index.html;
    gzip_static on;
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题