Nginx gzip 压缩不适用于 css 或 js
Nginx gzip compression not working for css or js
gzip 压缩适用于 html 页面,但不适用于 css 或 js,我发现了几个类似的问题,包括以下内容,并尝试了建议的解决方案,我试过了(gzip_buffers、gzip_min_length、gzip_comp_level) 的各种设置,但 none 有效。
还有什么可能影响这个,为什么 js 或 css 文件没有压缩?
NGINX gzip not compressing JavaScript files
enable gzip compression with nginx
我在 nginx conf 中有以下 http 块:
http {
upstream fastcgi_backend {
server 127.0.0.1:9000;
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log off;
#access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Enable Gzip compression
gzip on;
gzip_vary on;
gzip_buffers 128 4k;
# Compression level (1-9)
gzip_comp_level 5;
# Don't compress anything under 256 bytes
gzip_min_length 256;
# Compress output of these MIME-types
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-font-opentype
application/x-font-truetype
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/eot
font/opentype
font/otf
image/svg+xml
image/x-icon
image/vnd.microsoft.icon
text/css
text/plain
text/javascript
text/x-component;
# Disable gzip for bad browsers
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;
# request timed out -- default 60
client_body_timeout 10;
}
检查 conf.d
文件夹中是否有其他可能覆盖 nginx.conf
的 conf 文件,您应该会看到至少一个包含 hostname/IP Nginx 正在服务的服务器块的文件。看起来像这样:
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
...
}
您可以随时搜索服务器块:$ grep -rl "server_name" /etc/nginx
gzip 压缩适用于 html 页面,但不适用于 css 或 js,我发现了几个类似的问题,包括以下内容,并尝试了建议的解决方案,我试过了(gzip_buffers、gzip_min_length、gzip_comp_level) 的各种设置,但 none 有效。
还有什么可能影响这个,为什么 js 或 css 文件没有压缩?
NGINX gzip not compressing JavaScript files
enable gzip compression with nginx
我在 nginx conf 中有以下 http 块:
http {
upstream fastcgi_backend {
server 127.0.0.1:9000;
}
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log off;
#access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Enable Gzip compression
gzip on;
gzip_vary on;
gzip_buffers 128 4k;
# Compression level (1-9)
gzip_comp_level 5;
# Don't compress anything under 256 bytes
gzip_min_length 256;
# Compress output of these MIME-types
gzip_types
application/atom+xml
application/javascript
application/json
application/rss+xml
application/vnd.ms-fontobject
application/x-font-ttf
application/x-font-opentype
application/x-font-truetype
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/eot
font/opentype
font/otf
image/svg+xml
image/x-icon
image/vnd.microsoft.icon
text/css
text/plain
text/javascript
text/x-component;
# Disable gzip for bad browsers
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
# allow the server to close connection on non responding client, this will free up memory
reset_timedout_connection on;
# request timed out -- default 60
client_body_timeout 10;
}
检查 conf.d
文件夹中是否有其他可能覆盖 nginx.conf
的 conf 文件,您应该会看到至少一个包含 hostname/IP Nginx 正在服务的服务器块的文件。看起来像这样:
server {
listen 80;
listen [::]:80;
server_name example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
...
}
您可以随时搜索服务器块:$ grep -rl "server_name" /etc/nginx