开发者社区> 问答> 正文

nginx使用gzip无效求解 : 配置报错 

有两个请求,hello和status,返回的类型都是text/plain, status是nginx自带的,hello是自己添加的模块,测试gzip,发现status返回结果压缩,而hello返回结果不压缩,返回内容都大于1字节,编译时也加了--with-http_gzip_static_module,实在费解:
nginx.conf配置如下:
gzip on;     gzip_min_length 1;     gzip_buffers 4 16k;     gzip_http_version 1.1;     gzip_comp_level 5;     gzip_types text/plain application/x-javascript text/css application/xml text/javascript;     gzip_vary on;
[root@localhost ~]# curl  "http://127.0.0.1/hello"  (说明hello是有数据返回的) hello_world, wahahahahahahhahahahahahahahahahaahahahahahahahahahahahahahahah
gzip测试结果:
[root@localhost ~]# curl  -I -H"accept-encoding:gzip" "http://127.0.0.1/hello" (新增的模块) HTTP/1.1 200 OK

Server: nginx/1.9.8

Date: Mon, 11 Apr 2016 06:10:00 GMT

Content-Type: text/plain

Content-Length: 77

Connection: keep-alive

[root @localhost ~]# curl  -I -H"accept-encoding:gzip" "http://127.0.0.1/status"   (nginx自带的状态测试)

HTTP/1.1 200 OK

Server: nginx/1.9.8

Date: Mon, 11 Apr 2016 06:10:06 GMT

Content-Type: text/plain

Connection: keep-alive

Vary: Accept-Encoding

Content-Encoding: gzip
看代码两个模块实现几乎一样,但是gzip测试结果不一样,求解?

展开
收起
kun坤 2020-06-04 13:18:31 1540 0
1 条回答
写回答
取消 提交回答
  • 引用来自“莫扎特的代码”的评论

    站上hello模块代码啊 首先,感谢您的回帖, 1.可能我没说清楚,问题中的hello不是自己写的,是引用深入浅出nginx里面的例子; 2.我用了本站点的helloworld例子,http://www.oschina.net/code/snippet_937163_26019#43351  重新实验,结果是一样的,hello依然不能返回压缩后的结果。 编译:./configure --prefix=/usr/local/nginx_gzip --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=/root/vmwareTmp/mytest 3.请问您实验结果是否可以? 注:本站的例子修改:if (!(r->method & NGX_HTTP_GET))改成 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) ,否则curl -I -H不允许。问题依旧。。。 ######站上hello模块代码啊 ######status和hello的机制应该是有些许不同的,连日志都不一样,在没有添加日志情况下,hello的请求信息打出来了,而status并没有。。。 ######要疯了,干。。。######操作系统版本:Linux centos 2.6.32-504.el6.x86_64 ###### ngx_http_hello_handler 函数中, 添加:r->headers_out.content_type_len = sizeof("text/plain") - 1; 去掉:r->headers_out.content_length_n = content_length; ######

    引用来自“悬崖”的评论

    ngx_http_hello_handler 函数中, 添加:r->headers_out.content_type_len = sizeof("text/plain") - 1; 去掉:r->headers_out.content_length_n = content_length; 按照您的方法,问题解决了,非常感谢!您的gdb过程和解决思路对我们初学nginx是非常有意义,希望可以看到您就 这个问题解决过程的文章!######http://my.oschina.net/u/1445488/blog/659216 大概写了一下过程。 ######客户端需要声明支持gzip,服务端才会使用
    2020-06-05 13:18:55
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
《Nginx 代理系统常用手册》 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载