我正在使用在Ubuntu 12.04.4上运行的nginx 1.4.4 。
nginx是一个Rails应用程序服务器集群的反向代理。
静态文件(主要是资产)直接提供,而不会击中应用程序服务器。
我已经将其设置为gzip响应,并在可用时使用预压缩文件。
http { gzip on; gzip_http_version 1.0; gzip_proxied any; # other ngx_http_gzip_module directives... server { # proxy configuration location ^~ /assets/ { gzip_static on; expires max; add_header Cache-Control public; # root is inherited try_files $uri =404; error_page 404 /404.html; } } }
这工作。
我已经使用真正的预压缩资产和名称相同但内容不同的虚拟非压缩资产进行了testing:
/assets/application-a45d6...e2593.css # dummy content /assets/application-a45d6...e2593.css.gz # real CSS
我可以看到, on和off gzip_static会导致nginx正确地提供文件的预期版本。
到现在为止还挺好。
但是 ,此设置仅在文件的非压缩版本也存在时才有效。 只有预压缩的版本将导致404。
该文件说:
gzip_static
使用“always”值(1.3.6),在所有情况下都使用gzip文件,而不检查客户端是否支持它。 如果磁盘上没有未压缩的文件,或者使用了ngx_http_gunzip_module,这将非常有用。
(是的:我已经尝试过,并且也尝试添加gunzip on没有任何改变)
这似乎表明, 只有压缩版本的文件是好的。 这是真的吗? 我的configuration有什么问题吗?
您可能发现了一个错误。 但是总的来说,无论如何你都需要这两个文件,原因有三:
gzip_static always;用gzip_static always;强制它们gzip_static always; 他们可能不了解它。 在Nginx 1.6上不需要压缩文件:
location ~ \.txt$ { gzip_static on; gunzip on; }
既curl http://localhost/index.txt和curl -H "Accept-Encoding: gzip" http://localhost/index.txt | gunzip curl -H "Accept-Encoding: gzip" http://localhost/index.txt | gunzip现在可以在我的根目录下使用/srv/www/localhost/index.txt.gz 。