Nginx doesn't compress CSS files - gzip

I'm trying to figure out why Nginx doesn't compress CSS. Nginx config looks valid and typical for the tasks like that.
Failed test:
curl -H "Accept-Encoding: gzip" -I https://<hostname>/sites/default/files/css/css_xLFDRTFqZTZeUg7Pab0gP4cpz5TWo3PCH-KBo_HKQ6A.css
HTTP/1.1 200 OK
Server: nginx
Content-Type: text/css
Content-Length: 1123
ETag: "587c4cdc-463"
Cache-Control: max-age=2592000
Accept-Ranges: bytes
The response Content-Encoding should be gzip instead of text/css.
Nginx config:
http {
...
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 2;
gzip_http_version 1.1;
gzip_min_length 10240;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/x-icon application/vnd.ms-fontobject font/opentype application/x-font-ttf;
gzip_vary on;
gzip_proxied any;
gzip_disable msie6;
...
}

Your gzip_min_length is set too large - you can see the Content-Length in your request debug and its way less than the minimum threshold.
Remove that altogether or set it much lower.

Related

Getting Apache Substitute for multipart/mixed to work

I have created an employee portal for the company I work for but am having an issue on rewriting my Internal Hostname with the Portal's Hostname for a Content-Type of "multipart/mixed".
Below is the content I am trying to replace.
HTTP/1.1 202 Accepted
Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips
Content-Type: multipart/mixed; boundary=294D9B061EABA796BC8484AAC5CB4BB90
dataserviceversion: 2.0
Content-Length: 604
Cache-Control: no-cache
X-Frame-Options: SAMEORIGIN
Date: Sun, 16 Sep 2018 16:03:06 GMT
Connection: keep-alive
X-Akamai-Staging: ESSL
--294D9B061EABA796BC8484AAC5CB4BB90
Content-Type: application/http
Content-Length: 436
content-transfer-encoding: binary
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 339
dataserviceversion: 2.0
{"d":{"results":[{"__metadata":{"id":"https://www.internalhostname.abc/sap/opu/odata/sap/ZABC_MY_PAYSTUBS_SRV/ConcurrentEmploymentSet('anotherValue')","uri":"https://www.internalhostname.abc/sap/opu/odata/sap/ZABC_MY_PAYSTUBS_SRV/ConcurrentEmploymentSet('anotherValue')","type":"ZABC_MY_PAYSTUBS_SRV.ConcurrentEmployment"},"Pernr":"anotherValue","AssignmentText":""}]}}
--294D9B061EABA796BC8484AAC5CB4BB90--
I am using the Apache HTTP 2.4. And am familiar with the Substitute command.
I have a section:
<Location "/sap">
SetOutputFilter INFLATE
AddOutputFilterByType SUBSTITUTE application/http
AddOutputFilterByType SUBSTITUTE application/json
AddOutputFilterByType SUBSTITUTE application/xml
AddOutputFilterByType SUBSTITUTE multipart/mixed
Substitute 's|www.internalhostname.abc|portal.abc.com|ni'
</Location>

Apache deflate javascript compress ratio 1.00x

If I ran gzip in CLI, I can get a good compression ratio:
bundle.js: 75.3% -- replaced with bundle.js.gz
But in Apache, even I set deflate, it did compress, but with same file size. Below is my Apache config:
LoadModule deflate_module libexec/apache2/mod_deflate.so
<IfModule deflate_module>
DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE application/javascript text/plain text/css
CustomLog /var/log/deflate_log DEFLATE
</IfModule>
Below is the response:
ETag "8342b-53dc33d01d2c0-gzip"
Server Apache/2.4.23 (Unix)
Content-Type application/javascript
Last-Modified Sat, 01 Oct 2016 01:00:35 GMT
Date Sun, 02 Oct 2016 01:14:20 GMT
Connection Keep-Alive
Vary Accept-Encoding
Accept-Ranges bytes
Keep-Alive timeout=5, max=98
Content-Encoding gzip
Transfer-Encoding Identity
The network transfer size is same as before, and the ratio is 1.00x. I narrowed it down to only js get not compressed, instead css get good compression ratio of 6.22x. Is there something wrong with the js file?
I got it!
I noticed there is no "content-length" header in the response. So I went back to check Apache document. It says:
The DeflateBufferSize directive specifies the size in bytes of the
fragments that zlib should compress at one time. If the compressed
response size is bigger than the one specified by this directive then
httpd will switch to chunked encoding (HTTP header Transfer-Encoding
set to Chunked), with the side effect of not setting any
Content-Length HTTP header. This is particularly important when httpd
works behind reverse caching proxies or when httpd is configured with
mod_cache and mod_cache_disk because HTTP responses without any
Content-Length header might not be cached.
As my js file is 500k, much over the default 8k setting, so I added the following in conf file, now everything is good:
<IfModule deflate_module>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE application/javascript text/plain text/css
DeflateBufferSize 8096000
</IfModule>

Nginx + Passenger turn on gzip not take effect

Try to turn on Nginx gzip feature to save some bandwidth, but no luck.
Searching for hours still can not make it work.
Nginx is installed by Passenger. Here is the information about Nginx in my server:
nginx version: nginx/1.4.1
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt=-Wno-error --add-module=/home/lijc/.rvm/gems/ruby-1.9.3-p392#rails3.2/gems/passenger-4.0.2/ext/nginx
Nginx.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
passenger_root /home/lijc/.rvm/gems/ruby-1.9.3-p392#rails3.2/gems/passenger-4.0.2;
passenger_ruby /home/lijc/.rvm/wrappers/ruby-1.9.3-p392#rails3.2/ruby;
include mime.types;
default_type application/octet-stream;
sendfile on;
client_max_body_size 100m;
keepalive_timeout 65;
gzip on;
gzip_min_length 1k;
gzip_disable "MSIE [1-6]\.";
gzip_http_version 1.1;
gzip_types text/plain text/css application/x-javascript application/xml application/json application/atom+xml application/rss+xml;
gzip_vary on;
server {
listen 80;
server_name localhost;
root /home/lijc/web/rails/jasli2team/evolution/public;
passenger_enabled on;
}
}
Then restart Nginx and Passenger. However with Chrome dev tools. The header return by my site still not including Content-Encoding:gzip
Here is the header return by my site:
Cache-Control:must-revalidate, private, max-age=0
Content-Type:text/html; charset=utf-8
Date:Wed, 05 Jun 2013 02:55:47 GMT
ETag:"f5a1b272c96c5786342ec4bfd9b6e608"
Proxy-Connection:close
Server:nginx/1.4.1 + Phusion Passenger 4.0.2
Status:200 OK
Vary:Accept-Encoding
Via:1.0 localhost.localdomain:1080 (squid/2.6.STABLE16)
X-Cache:MISS from localhost.localdomain
X-Cache-Lookup:MISS from localhost.localdomain:1080
X-Powered-By:Phusion Passenger 4.0.2
X-Rack-Cache:miss
X-Request-Id:8353874387ca7510158dd5bf93b37ab9
X-Runtime:0.042863
X-UA-Compatible:IE=Edge,chrome=1
I have no idea what is wrong or missing. Any help will be appreciated.

How do you enable gzip compression for favicon.ico?

How do you enable Apache to compress favicon?
FAILED - (11.6 KB, compressed = 5.7 KB - savings of 5.9 KB) - http://website/favicon.ico
Judging from your example, the fail is from the WebPagetest.
Add the line below to the file /etc/apache2/mods-enabled/deflate.conf (for Apache 2.2 on Ubuntu 12.04):
AddOutputFilterByType DEFLATE image/x-icon
Reload apache afterwards.
Here is my configuration for assets compression with Nginx :
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain
application/x-javascript
text/xml
text/css
font/woff
application/font-woff
font/truetype
font/opentype
font/eot
application/vnd.ms-fontobject
image/svg+xml
image/x-icon;
gzip_vary on;
You could use imagemagick to compress the ico file:
compress -color 16 PATHTOORIGINAL/file.ico PATHTONEW/file.ico

How do I use mod_deflate in Apache 2.4.1 with Haproxy?

I have a strange issue whereby including the following syntax in my Apache 2.4.1 httpd.conf causes "502 Bad Gateway" errors when retrieving swf files via HAproxy:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/x-javascript text/javascript
When I remove this config line the 502 Bad Gateway error goes away.
The server returns these response headers on a successful request:
Date: Wed, 11 Apr 2012 20:24:12 GMT
Server: Apache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
200 OK
I fixed this by updating to Apache 2.4.2 (there was a mod_deflate seg fault bug in 2.4.1) and adding:
Header append Vary User-Agent
Beneath the AddOutputFilterByType line.