Nginx和gzip不工作.我错在哪里了


Nginx and gzip not working. Where I wrong

我使用nginx和php-fpm,但无法使用gzip压缩任何内容。服务器是Centos 6.5,带有已编译的nginx,命令:--sbin path=/usr/sbin/nginx--with-http_stub_status_module--conf path=/etc/nginx/nginx.conf--pid path=/var/run/nginx.pid--error log path=/var/log/nginx/error.log--http log path=var/log/nginx/access.log--user=nginx--with-http_gzip_static_module

这里是nginx.conf:

    include       /etc/nginx/mime.types;
default_type  application/octet-stream;
access_log off;
log_not_found off;
sendfile        on;
tcp_nopush     on;
tcp_nodelay on;
client_body_timeout   30;
client_header_timeout 30;
keepalive_timeout     45;
send_timeout          30;
reset_timedout_connection on;
output_buffers 1 8M;
gzip              on;
gzip_static       on;
gzip_buffers      16 8k;
gzip_comp_level   5;
gzip_http_version 1.1;
gzip_min_length   2;
gzip_types        text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;
gzip_vary         on;
gzip_proxied any;
#gzip_disable "MSIE [1-6]'.(?!.*SV1)";
gzip_disable msie6;
server_names_hash_max_size 1024;
server_names_hash_bucket_size 128;
open_file_cache max=20000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
client_body_buffer_size 1024k;
fastcgi_buffers 64 64k;
include /etc/nginx/conf.d/domains/*.conf;

这里是domain.conf:

    server {
    listen xx.xx.xx.xx:80;
    server_name xxxxxxxx.com;
    if ( $host ~* ^(?!(www|cdn)'.) ) {
       rewrite .* $scheme://www.$host$request_uri permanent;
    }
    index index.php;
    charset utf-8;.
    if ($request_uri ~* "^(.*(?<!expert)/)index'.php$") {
        return 301 $1;
    }
    root /home/ftpaccess/xxxxxxx/www;
    location ~* ^.+'.(txt|js|css|doc|xls|pdf|ppt|rtf|jpg|jpeg|gif|bmp|ico|zip|tgz|gz|bz2|exe|tar|wav|avi|flv|mpg|mpeg|mp4|mp3|png)$ {
        expires 720h;
        add_header Pragma public;
        add_header Cache-Control "public";
    }
    location ~ /'.ht {
        deny all;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt { access_log off; log_not_found off; allow all;}
    location = /apple-touch-icon.png { access_log off; log_not_found off; }
    location = /apple-touch-icon-precomposed.png { access_log off; log_not_found off; }
    error_page 404 /404.php;
    include /etc/nginx/conf.d/redirect/xxxxxx.conf;
    location ~ '.php$ {
        include /etc/nginx/php.conf;
        include        /etc/nginx/fastcgi_params;
    }
}

和php.conf:

fastcgi_split_path_info ^(.+'.php)(/.+)$;
try_files $uri =404;
fastcgi_pass   unix:/var/run/php5-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;

curl-I-H"接受编码:gzip,deflate"http://www.xxxxxx.com响应:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 03 Nov 2014 22:14:23 GMT
Content-Type: text/html; charset: iso-8859-1
Connection: keep-alive
Set-Cookie: PHPSESSID=0pi4o2i83gvof758gep5nc01m5; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

curl-I-H"接受编码:gzip,deflate"http://www.xxxxxxx.com/js/pop.js响应:

HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Mon, 03 Nov 2014 22:15:21 GMT
Content-Type: application/javascript
Content-Length: 5241
Last-Modified: Mon, 03 Nov 2014 21:21:28 GMT
Connection: keep-alive
Expires: Wed, 03 Dec 2014 22:15:21 GMT
Cache-Control: max-age=2592000
Pragma: public
Cache-Control: public
Accept-Ranges: bytes

我哪里做错了,怎么解决?几乎什么都试过了,但都没能成功。

您的gzip_types不包括您引用的类型。

正如您所看到的,您的配置涵盖了以下类型:text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss,但您同时引用了text/javascriptapplication/javascript