title: Enable gzip on Nginx without "'http' directive is not allowed here" error
body: <p>I've inherited a code base that needs gzip enabled. I added these lines to my nginx staging.conf file (which shows up in two more places: /etc/nginx/sites-enabled/ and /etc/nginx/sites-available/):</p>
  
  <pre><code>http {
      # enable gzip compression
      gzip             on;
      gzip_min_length  1100;
      gzip_buffers     4 32k;
      gzip_types       text/plain application/x-javascript text/xml text/css;
      gzip_vary        on;
      gzip_disable     "MSIE [1-6]\.(?!.*SV1)";
      # end gzip configuration
  }
  </code></pre>
  
  <p>But when I try to restart nginx, it fails (without any error message), and running "sudo nginx" gets me this error: <code>nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/staging.conf:37</code></p>
  
  <p>This is the entire conf file:</p>
  
  <pre><code># Myexample staging nginx setup. This is meant to be included in /etc/nginx/sites-available.
  
  ssl_certificate      /home/django/myexample.io/conf/nginx/wildcard-ssl.crt;
  ssl_certificate_key  /home/django/myexample.io/conf/nginx/wildcard-ssl.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !RC4 !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS";
  
  server {
      listen 80;
      listen 443 ssl;
      server_name www.stagingpy.myexample.io;
  
      return 301 $scheme://stagingpy.myexample.io$request_uri;
  }
  
  server {
      listen 80;
      listen 443 ssl;
      server_name stagingpy.myexample.io;
  
      access_log /var/log/nginx/myexample_access.log;
      error_log  /var/log/nginx/myexample_error.log;
  
      location ^~ /apple-touch-icon { root /home/django/myexample.io/static/ico/; expires  1h; }
      location = /favicon.ico       { root /home/django/myexample.io/static/ico/; expires  1h; }
      location = /humans.txt        { root /home/django/myexample.io/static/txt/; expires  1h; }
      location = /robots.txt        { root /home/django/myexample.io/static/txt/; expires  1h; }
      location /static/             { root /home/django/myexample.io/           ; expires 30d; }
  
      location / {
          uwsgi_pass  unix:///var/run/uwsgi/app/staging/socket;
          include     uwsgi_params;
      }
  }
  
  http {
      # enable gzip compression
      gzip             on;
      gzip_min_length  1100;
      gzip_buffers     4 32k;
      gzip_types       text/plain application/x-javascript text/xml text/css;
      gzip_vary        on;
      gzip_disable     "MSIE [1-6]\.(?!.*SV1)";
      # end gzip configuration
  }
  </code></pre>
  
  <p>I've tried putting the http block after the server blocks and before the server blocks, and I've tried putting the server blocks into the http block, but none of these things have worked. I get the same problem on my production server (the production.conf file looks pretty much the same, except without the "stagingpy" subdomain). I also tried taking the gzip lines out of the http block altogether, and that prompted this error upon running "sudo nginx": <code>nginx: [emerg] "gzip" directive is duplicate in /etc/nginx/sites-enabled/staging.conf:38</code>; however, I don't see any other gzip lines when I look at the file at that path.</p>
  
  <p>Where should the http block go so that I can restart nginx successfully and have gzip enabled for file compression?</p>
  
  <p>Edit: I also tried <a href="http://davidwalsh.name/enable-gzip" rel="nofollow">creating a gzip.conf file in conf.d as shown here</a>, but even after I removed the http block from the staging.conf file, I got the error about gzip being a duplicate (<code>nginx: [emerg] "gzip" directive is duplicate in /etc/nginx/conf.d/gzip.conf:1</code>). This leads me to believe that gzip is somewhere in a conf file already, but running <code>find /etc/nginx/ -type f -name "*.conf" | grep gzip -n</code> only gives me the gzip.conf file. According to some online gzip tests, the site has gzip enabled, but others say it doesn't. </p>