关闭Nginx Gzip以获取特定的查询字符串(与nodejs一起使用)

时间:2013-11-06 10:26:51

标签: node.js nginx gzip

我正在使用nginx来gzip静态文件&来自nodejs服务器的json响应。

对于一个特定请求(使用类似"?fn=foo"的查询字符串),我需要发送非gzip json响应。

我试图用nginx位置模块实现这一点,基于查询字符串的正则表达式,但查询字符串不在用于匹配nginx位置的URI中 我试图在我的主要位置路线中放置一个if ($arg_fn = "foo") {gzip off;},但它失败了404而不是。

有什么想法吗? 是否有可能用nginx实现这一目标?或者有没有办法告诉nodejs到nginx不要gzip这个响应?

我的nginx配置文件:

worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}

http {
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
    proxy_temp_path /var/cache/nginx/proxy_temp;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    gzip_comp_level 6;
    gzip_vary on;
    gzip_min_length  1000;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/json;
    gzip_buffers 16 8k;

    map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
    }

    upstream upstream {
      server 127.0.0.1:8887;
      server 127.0.0.1:8888;
      keepalive 64;
    }

    server {
        listen       80;
        listen       443 ssl;
        server_name  _;

        ssl_certificate /etc/nginx/ssl/zellno-ssl-bundle.crt;
        ssl_certificate_key /etc/nginx/ssl/zellno-key.pem;


        location / {
            if ($arg_fn = "comp") {
                gzip off;
            }

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_pass http://upstream/;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }

        error_page  404              /404.html;
        location = /40x.html {
            root   /usr/share/nginx/html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您的问题是您说$args_fn带有“s”,而它应该是$arg_fn的单数形式,请尝试并告诉我它是如何进行的。

修改: 试试这个以确保输入if块

location / {
    if ($arg_fn = "comp") {
        return 444;
        #gzip off;
    }
}

如果它返回错误代码,则if正在运行但不是gzip,如果它不起作用,那么我们需要首先修复if