Nginx 不压缩网页

Nginx does not compress web pages

我的 Nginx website configgzip 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= 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 版本:

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 

但是如果 open a webpage with Developer Tools:

我看不到 Content-Encoding: gzip

EDIT1

它不压缩 application/javascript,但压缩 text/htmltext/html 未在 gzip_types:

中列出

实际上 adding/remoing gzip on; 配置指令没有效果。

更改为 this 没有帮助:

gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

EDIT2

gzip_static on; 来自 here 没有帮助。

没有任何帮助,它拒绝工作,this没有帮助:

# 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)”;

在我的 /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,是Google Chrome浏览器,按F5实际上不会重新加载javascript(如果没有勾选Disable Cache)

在您的 /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;

在Chrome Dev Tools中,确保在刷新和测试时勾选“禁用缓存”

您也可以尝试在静态 URL 上进行测试,如果配置正确,Nginx 将 return Content-Encoding: gzip

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