【问题标题】:Rails server do not send content-encoding GzipRails 服务器不发送内容编码 Gzip
【发布时间】:2019-07-26 06:07:53
【问题描述】:

当我在本地的 env Product 中运行项目时。我已经运行 cmd assets:precompile 来编译资产。

我使用内容编码 gzip 在本地请求,它可以工作(图片中的示例)。

但是当我部署到服务器时,请求获取不响应内容编码 gzip 的资产。

我在主机上使用 ngix 服务器。 我想设置服务器以使用编码 gzip 发送资产。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4


    【解决方案1】:

    在生产环境中运行时,Rails 通常让 Nginx 处理所有资产。 这是由config.public_file_server.enabled 配置设置决定的。

    所以,你需要做的是更新 nginx 配置,让它知道发送压缩文件,如下所示:

      location ~ ^/(assets|packs)/ {
        sendfile           on;
        sendfile_max_chunk 1m;
        tcp_nopush on;
        gzip_static  on;
        if_modified_since before;
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
      }
    

    重要的部分是gzip_static,它告诉 nginx 寻找已经压缩的文件(由assets:precompile 创建) 您还可以在 nginx 配置中启用 gzip 来告诉它压缩所有内容,并启用 gzip_proxied 来告诉它压缩来自 rails 本身的响应。

    另外,看看这里的所有选项:https://nginx.org/en/docs/http/ngx_http_gzip_module.html

    【讨论】:

      猜你喜欢
      • 2012-09-01
      • 1970-01-01
      • 2018-02-27
      • 1970-01-01
      • 2016-11-24
      • 2012-08-09
      • 2015-01-17
      • 2012-08-20
      • 1970-01-01
      相关资源
      最近更新 更多