Dmitriano
Dmitriano

Reputation: 2060

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:

enter image description here

EDIT1

It does not compress application/javascript, but comresses text/html whille text/html is not listed in gzip_types:

enter image description here

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;

Upvotes: 1

Views: 650

Answers (2)

Alexey Starinsky
Alexey Starinsky

Reputation: 4305

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;

Upvotes: 0

anthumchris
anthumchris

Reputation: 9072

In Chrome Dev Tools, ensure that "Disable cache" is checked when refreshing and testing.

Disable cache checkbox

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

Upvotes: 1

Related Questions