Nginx使用try_files和gzip_static仅提供未压缩的服务

7
穿上我的系统管理员帽子,我的一个应用程序开发人员给我提出了这个问题:
在他的应用目录中,他有app/assets/javascripts和app/public/javascripts中的javascript文件。在public/javascripts目录中,既有未压缩的文件,也有.gz文件。期望nginx提供压缩版本的javascript。
对于该应用程序的nginx配置文件,我已经添加了以下内容:
  location /javascripts {
    gzip on;
    gzip_static on;
    try_files /srv/app_dir/current/public/$uri /srv/app_dir/current/assets/$uri;
  }

使用这些参数,应用程序可以正常工作。所有的.js文件都被找到并提供服务。但是nginx从未找到并提供压缩版本的文件。

有什么建议来解决这个问题吗?


我的nginx版本:nginx -V: nginx version: nginx/0.8.54 由gcc 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)编译构建 启用TLS SNI支持 配置参数:--prefix=/opt/nginx-0.8.54 --conf-path=/etc/nginx/nginx.conf --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module --add-module=/var/chef/cache/headers-more-nginx-module --add-module=/var/chef/cache/chunkin-nginx-module - Mojo
2个回答

0

我刚刚尝试了@hendry关于nginx/1.13.8的答案,但它并没有起作用。当我使用"gzip_static always;" 而不是 "gzip_static on;"时,它开始正常工作。

location /fd/ {
    gunzip on;
    gzip_static always;
}

cURL输出:

$ curl --silent -H "Accept-encoding: gzip" "http://myhost.com/fd/test.txt" | file -
/dev/stdin: gzip compressed data, from Unix
$ curl --silent "http://myhost.com/fd/test.txt" | file -
/dev/stdin: ASCII text

不适用于 try_files。您需要保留原始文件。https://serverfault.com/questions/571733/nginx-gzip-static-why-are-the-non-compressed-files-required/965094#965094 - rofrol

0

在Nginx 1.6上,不需要使用未压缩的文件:

    location ~ \.txt$ {
        gzip_static on;
        gunzip on;
    }

现在我的根目录中只需有/srv/www/localhost/index.txt.gz,就可以使用curl http://localhost/index.txtcurl -H "Accept-Encoding: gzip" http://localhost/index.txt | gunzip两种方式正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接