Enable gzip in Nginx without specifying the http directive is not allowed here "error
I have inherited a code base that requires gzip. I added these lines to the nginx staging.conf file (which shows up in two other places: / etc / nginx / sites-enabled / and / etc / nginx / sites-available /):
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
}
But when I try to restart nginx it fails (without any error message) and running "sudo nginx" gives me this error: nginx: [emerg] "http" directive is not allowed here in /etc/nginx/sites-enabled/staging.conf:37
This is the whole conf file:
# 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
}
I tried to put the http block after the server blocks and before the server blocks, and I tried to put the server blocks in the http block, but none of these things 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 have also tried to bring gzip-line from http bloc as a whole, and it has caused this error when you run "sudo nginx could": nginx: [emerg] "gzip" directive is duplicate in /etc/nginx/sites-enabled/staging.conf:38
; however, I don't see any other gzip lines when I look at the file in this path.
Where should the http block be so that I can successfully restart nginx and enable gzip to compress the file?
Edit: I also tried to create a gzip.conf file in conf.d as shown here , but even after I removed the http block from the staging.conf file, I got an error stating that gzip is a duplicate ( nginx: [emerg] "gzip" directive is duplicate in /etc/nginx/conf.d/gzip.conf:1
). This makes me think that gzip is already in the conf file, but running find /etc/nginx/ -type f -name "*.conf" | grep gzip -n
it only gives me the gzip.conf file. According to some online gzip tests, the site has gzip, but others say it doesn't.
It looks like there is another nginx.conf file that I didn't know about (don't know why it didn't show up when running find) that has gzip in it, and hence a recurring error. After checking my site on several other sites (for example, http://www.whatsmyip.org/http-compression-test/ ), it turns out that http://checkgzipcompression.com/ is incorrect about the site not compressing. I'm not sure why Google PageSpeed ββstill offers compression, but that as far as I could get it.