简体   繁体   English

如何在Nginx Web服务器上启用gzip?

[英]How to enable gzip on a Nginx web server?

I tried to enable gzip on my website but with not good results. 我试图在我的网站上启用gzip,但效果不佳。 Checking with http://checkgzipcompression.com/ gzip shows enabled but when I go to https://gtmetrix.com/ to test my website's performance and speed it seems that gzip is not enabled for some files (eg. JavaScript files and SVG files). 使用http://checkgzipcompression.com/进行检查gzip显示已启用,但是当我转到https://gtmetrix.com/来测试我的网站的性能和速度时,似乎某些文件(例如JavaScript文件和SVG)未启用gzip文件)。

What am I doing wrong? 我究竟做错了什么?

In order to enable gzip I used an .htaccess and pasted the following code: 为了启用gzip,我使用了.htaccess并粘贴了以下代码:

<IfModule mod_mime.c>
   AddEncoding gzip svgz
</IfModule>

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>

Before mod_deflate.c I also tried the following code too: mod_deflate.c之前,我也尝试了以下代码:

<ifModule mod_gzip.c>
  mod_gzip_on Yes
  mod_gzip_dechunk Yes
  mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  mod_gzip_item_include mime ^application/x-javascript.*
  mod_gzip_item_include mime ^text/.*
  mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  mod_gzip_item_exclude mime ^image/.* 
  mod_gzip_item_include handler ^cgi-script$
</ifModule>

Server Information 服务器信息

server  nginx
vary    Accept-Encoding

NGINX does not have support for .htaccess files. NGINX不支持.htaccess文件。

Like Apache: .htaccess 像Apache:.htaccess

You can't do this. 你做不到 You shouldn't. 你不应该 If you need .htaccess, you're probably doing it wrong. 如果需要.htaccess,则可能做错了。

In order to enable Gzip compression on your NGINX web server, first open your NGINX's default configuration file: sudo vim /etc/nginx/nginx.conf , and replace the existing Gzip settings with the following: 为了在NGINX Web服务器上启用Gzip压缩,请首先打开NGINX的默认配置文件: sudo vim /etc/nginx/nginx.conf ,然后用以下内容替换现有的Gzip设置:

nginx.conf (you can modify the settings below according to your needs) nginx.conf (您可以根据需要修改以下设置)

  # Enable Gzip
  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;

Restart NGINX 重新启动NGINX

service nginx restart or /etc/init.d/nginx restart service nginx restart /etc/init.d/nginx restart

NGINX Gzip documentation : http://nginx.org/en/docs/http/ngx_http_gzip_module.html NGINX Gzip文档: http ://nginx.org/en/docs/http/ngx_http_gzip_module.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM