gzip is enabled in nginx and is still not working - nginx

My config is:
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied expired no-cache no-store private auth;
gzip_types *;
But it doesn't work:

Related

Nginx does not compress web pages

My Nginx website config with gzip on:
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/letsencrypt/live/...
ssl_certificate_key /etc/letsencrypt/live/...
gzip on;
gzip_types text/plain application/xml text/css text/js text/xml application/javascript text/javascript application/json application/xml+rss;
root /home/devnote/www;
index index.php index.html;
access_log /var/log/nginx/devnote-ssl.access.log;
error_log /var/log/nginx/devnote-ssl.error.log info;
server_name ...
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Rewrite for multi site files
rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
}
location ~ \.php$ {
fastcgi_connect_timeout 60;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/www-devnote.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
}
OS: Ubuntu 16.04, Nginx version:
nginx -V
built with OpenSSL 1.0.2g 1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads
But I do not see Content-Encoding: gzip if I open a webpage with Developer Tools:
EDIT1
It does not compress application/javascript, but comresses text/html whille text/html is not listed in gzip_types:
actually adding/remoing gzip on; directive to the config does not have an effect.
Changing to this does not help:
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
EDIT2
gzip_static on; from here does not help.
Nothing helps, it refuses to work, this does not help:
# output compression saves bandwidth
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+x$
# make sure gzip does not lose large gzipped js or css files
# see http://blog.leetsoft.com/2007/07/25/nginx-gzip-ssl.html
gzip_buffers 16 8k;
# Disable gzip for certain browsers.
gzip_disable “MSIE [1-6].(?!.*SV1)”;
In my /etc/nginx/nginx.conf I have this:
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
In Chrome Dev Tools, ensure that "Disable cache" is checked when refreshing and testing.
You can also try testing on static URLs, and Nginx will return Content-Encoding: gzip when configured properly:
location ~ ^/test.js$ {
gzip_types application/javascript;
gzip on;
default_type application/javascript;
return 200 "/***** hello *****/";
}
$ curl --compressed -kIX GET https://example.com/test.js
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 01 Dec 2021 00:00:00 GMT
Content-Type: application/javascript; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Content-Encoding: gzip
It is not Nginx, it is Google Chrome browser, pressing F5 does not actually reload the javascript (if Disable Cache is not checked).
Uncomment this in your /etc/nginx/nginx.conf:
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

nginx reverse proxy working only if port 80 available

I hope somebody can help me to solve/understand this issue as I am totally stuck.
I am a ec2 instance running nginx. This ec2 instance is behind a load balancer.
My nginx configuration, is to reverse the traffic to an external service hosted on azure.
But for demonstration purpose lets assume this external service is example.com
all my infra is working, and my nginx configuration files is like this:
# This is the new file
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
server_names_hash_bucket_size 128;
server {
listen 80; ## listen for ipv4; this line is default and implied
root /usr/share/nginx/html;
index index.html;
server_tokens off; # disable the Server nginx header
# enable gzip
gzip on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
location / {
# try_files $uri /index.html; # redirect all request to index.html
proxy_pass https://example.com;
}
}
server {
listen 443; ## listen for ipv4; this line is default and implied
root /usr/share/nginx/html;
index index.html;
server_tokens off; # disable the Server nginx header
# enable gzip
gzip on;
gzip_disable "msie6";
gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
text/plain
text/css
text/js
text/xml
text/javascript
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
location / {
# try_files $uri /index.html; # redirect all request to index.html
proxy_pass https://example.com;
}
}
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
With this configuration everything works just fine. But if I remove the server on port 80 and keep only the 443 and reload nginx. On the browser I get the error Bad Gateway
Is the server 80 required for nginx? I checked all the documentation and couldn't find anything.
Please can anyone enlighten me on this issue or just point me on the right path? thank you so much for any help you can provide or hint.
And please if my question is not 100% clear, don't hesitate to ask for details

gzip compression showing off even though enabled

I'm unsure as to why gzip it showing as failed on both webpagetest and gtmetrix when in my nginx config I have it enabled.
I'm currently running nginx version: nginx/1.10.1
Here is my current nginx config
# HTTPS
server {
listen 443 ssl http2 default_server;
root /home/www/youtstream;
server_name dev.yout.com
gzip_disable "msie6";
access_log off;
error_log off;
gzip_vary on;
gzip on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
ssl on;
ssl_certificate /srv/dehydrated/certs/dev.yout.com/fullchain.pem;
ssl_certificate_key /srv/dehydrated/certs/dev.yout.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static {
alias /home/www/youtstream/static/;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|mp4|woff2)$ {
expires max;
}
Here is the results of webpagetest and gtmetrix

Nginx enable gzip

I want enable the gzip compression on my nginx server. The nginx.conf file is here:
http {
# Enable Gzip
server {
location ~* \.(?:ico|woff|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location /api {
try_files $uri $uri/ /api/index.php;
}
location / { ##merge
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types
# text/html is always compressed by HttpGzipModule
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/json
application/xml
application/rss+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;
gzip_static on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
}
}
Unfortunately the gzip compression not working, Google Pagespeed and Gtmetrix not detect this.
Where can I place the gzip conf?
In The http{} server{} or location{} tag?
I already tried in the http and in the location tags too
You can put the gzip configuration anywhere, but if you want to apply it to all websites / files it is best to put it in the http section - this will then be the default for all server and location blocks. I would also "shorten" / change your config to the following:
http {
gzip on;
gzip_min_length 500;
gzip_proxied any;
gzip_comp_level 4;
gzip_types text/css text/javascript text/xml text/plain text/x-component application/javascript application/json application/xml application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml;
gzip_vary on;
gzip_disable "msie6";
... here come your server blocks / rest of your config
}
I use that configuration and it works fine for me - you can also test it in your browser first (for example with Firebug) before testing it with external services.
Using gzip_static only makes sense if you actually generate gzipped files for Nginx (as filename + .gz), so this has nothing to do with enabling gzip and should only be a possible second step.

Use LZF Compression instead GZIP with NginX

Is there a way to change the handler for compression from gzip to lzf in nginx ?
Actual config:
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 1024;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
can be it translated to ?
lzf on;
..
..

Resources