简体   繁体   English

Nginx Rails / Passenger不提供压缩的资产文件

[英]Nginx Rails/Passenger is not serving gzipped asset files

Nginx 1.10.1 Rails 5.0.1. Nginx 1.10.1 Rails 5.0.1。 The asset pipeline is making both the zipped and native versions of the css and js files, but only the uncompressed is served to the browser. 资产管道同时生成CSS和js文件的压缩版本和本机版本,但是只有未压缩的版本才提供给浏览器。 I can see both versions in public/assets and I can use curl to retrieve the zipped version by appending '.gz' to the css/js asset url delivered to me. 我可以在公共/资产中看到两个版本,也可以使用curl通过将'.gz'附加到提供给我的css / js资产网址中来检索压缩版本。

I am using a CDN (AWS CloudFront) but have tested without the CDN and anyway it should still point me to the zipped version, right? 我正在使用CDN(AWS CloudFront),但是已经在没有CDN的情况下进行了测试,无论如何,它仍应指向压缩版本,对吗?

nginx has --with-http_gzip_static_module . nginx具有--with-http_gzip_static_module Using this answer as a guide, my nginx config (edited) has: 使用此答案作为指导,我的nginx配置(已编辑)具有:

http {
    server {
        listen       80;
        server_name  idoimaging.com www.idoimaging.com;
        return 301 https://$server_name$request_uri;
    }

    server {
        listen 443 ssl;
        server_name  idoimaging.com www.idoimaging.com;
        root /var/www/idoimaging/current/public;
        location ~ ^/(assets)/ {
            gzip_static on;
        }
    }
}

I've also tried /assets/ as the regex in location . 我也尝试过/assets/作为location的正则表达式。 In my production.rb : 在我的production.rb

# Have also tried setting this to false
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.middleware.insert_before(Rack::Sendfile, Rack::Deflater)
config.assets.compress = true
config.assets.js_compressor = Uglifier.new(mangle: false)

I'm testing with Chrome with cache disabled, also for speed with curl https://idoimaging.com | grep assets 我正在使用禁用了缓存的Chrome浏览器进行测试,还可以使用curl https://idoimaging.com | grep assets来提高速度curl https://idoimaging.com | grep assets curl https://idoimaging.com | grep assets which I believe should have the same behaviour as the browser? 我认为curl https://idoimaging.com | grep assets应具有与浏览器相同的行为?

I don't know why nginx will not serve the gzip'ed js/css asset files when they are present. 我不知道为什么nginx在存在gzip后的js / css资产文件时不提供服务。

EDIT: I've also seen this guide that takes another approach: Use Rails as the static asset server instead of nginx. 编辑:我还看过本指南 ,它采用了另一种方法:将Rails用作静态资产服务器而不是nginx。 Would this be a better approach? 这会是更好的方法吗?

Given that Rails is producing the gzipped assets, we can be confident that the issue resides solely with nginx. 鉴于Rails正在生产压缩后的资产,我们可以确定问题仅在nginx上存在。 So let's focus there! 因此,让我们集中精力!

I believe Nginx is faster at serving static assets than Rails is, so I'd stay away from using it as a static asset server in any event. 我相信Nginx在提供静态资产方面比Rails更快,因此无论如何我都不会将其用作静态资产服务器。

Now looking at the URL you provided ( https://idoimaging.com ), your server appears to be providing gzipped files. 现在查看您提供的URL( https://idoimaging.com ),您的服务器似乎正在提供压缩文件。 So the issue is just with your testing approach (assuming this is the correct URL, and you have not changed the server configuration since this post). 因此,问题仅在于您的测试方法(假设这是正确的URL,并且自发布以来,您尚未更改服务器配置)。

Your curl command doesn't include the Accept-Encoding: gzip header, which tells the server your client is capable of handling gzipped files. curl命令不包含Accept-Encoding: gzip标头,该标头告诉服务器您的客户端能够处理压缩文件。 Without it, Nginx will serve the uncompressed versions. 没有它,Nginx将提供未压缩的版本。 You can see the difference in commands and outputs in this gist . 您可以在本要点中看到命令和输出的差异。 The difference is in the Content-Length and Content-Encoding response headers. 区别在于Content-LengthContent-Encoding响应头。

If you're seeing something different, let me know! 如果您看到不同的内容,请告诉我!

Edit 1 编辑1

That's odd, CloudFront appears to have cached redirects for both your CSS and your JS. 奇怪的是,CloudFront似乎已经缓存了CSS和JS的重定向。

richardseviora:Richards-MacBook-Pro@~> curl "https://cdn.idoimaging.com/assets/application-0cd41e63d35c1e5a7ab76ded23fbaf2ef1d1b786144134a80a1dfa9c765cff0d.css" -I -H "accept-encoding: gzip"
HTTP/1.1 301 Moved Permanently
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Server: nginx/1.10.1
Date: Thu, 23 Feb 2017 03:30:49 GMT
Location: https://idoimaging.com/assets/application-0cd41e63d35c1e5a7ab76ded23fbaf2ef1d1b786144134a80a1dfa9c765cff0d.css
Age: 942
X-Cache: Hit from cloudfront
Via: 1.1 d8b73f8fefd106d5c95f11977e132c46.cloudfront.net (CloudFront)
X-Amz-Cf-Id: ao8PwibmSj1JhmfbmuNfC2gYi9x-RTcCrJDAqLWAUIyOjP_3qYTGQA==


# It should look like this instead.
richardseviora:Richards-MacBook-Pro@~> curl -I -H "accept-encoding: gzip" "http://cdn.sweatrecord.com/assets/application-b932da0ddcf53d3650da5135b083224e863b349c784f3d1e3ca992b36ce3e31d.css"
HTTP/1.1 200 OK
Content-Type: text/css
Connection: keep-alive
Accept-Ranges: bytes
Content-Encoding: gzip
Date: Thu, 23 Feb 2017 03:50:13 GMT
Last-Modified: Mon, 30 Jan 2017 16:29:44 GMT
Server: Apache
Vary: Accept-Encoding,Origin
X-Cache: Miss from cloudfront
Via: 1.1 8b5947aba7280333032d4dcdd80b3489.cloudfront.net (CloudFront)
X-Amz-Cf-Id: FN9FyKl0RCpNTTqBwb0WyQhbDd-rEyyQ05eCtaFCD8YaH_FtjG7Q8Q==

This is Nginx issue, but I'm not sure where exactly because CloudFront will cache 301s . 这是Nginx的问题,但我不确定确切在哪里,因为CloudFront会缓存301s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM