nginx 上神秘的 gzip 故障

nginx 上神秘的 gzip 故障

· 我的网站上显示 gzip 未启用

· 甚至连我的主机提供商 asmallorange 的技术支持人员都无法找出问题的原因

· 希望这里有人可以

· 我已经记录了我的精确的设置(仅此而已)

1)我安装了 centos 7 minimal

2)我通过 putty 登录

3)然后我输入以下命令

3a)yum 更新

3b)yum clean all

3c)yum 安装 epel-release

3d)yum 安装 nginx

3e)systemctl 启用 nginx

3f)vi /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    server_tokens       off;

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

    limit_conn_zone     $binary_remote_addr zone=addr:8m;
    limit_req_zone      $binary_remote_addr zone=mylimit:16m rate=16r/s;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  abcj.me;
        root         /srv;
        index        index-l1.html

        gzip            on;
        gzip_vary       on;
        gzip_min_length 1024;
        gzip_types      text/css text/plain;

        client_body_timeout 8;
        client_header_timeout 8;

        location ~*  \.(css|ico|jpeg|jpg|js|mp3|mp4|pdf|png|txt|webp|zip)$ {
            expires 16d;
        }

        location ~*  \.(html)$ {
            expires 1d;
        }

        error_page 404 /404-l1.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {
        listen 80;
        server_name www.abcj.me;
        return 301 http://abcj.me$request_uri;
    }
}

3g)服务 nginx 重启

4)我通过 winscp 将 index-l1.html 上传到 srv

· nginx -V 2>&1 | tr -- - '\n' | grep 模块

· 表明我已

·· http_gzip_static_module

·· http_gunzip_module

· curl -H“接受编码:gzip”-Ihttp://abcj.me/index-l1.html

·· 不显示 gzip 接受

·https://www.giftofspeed.com/gzip-test/

·· 不显示 gzip 接受

答案1

· gzip 命令需要直接位于 http 块中,而不是位于服务器块中

user nginx; 
worker_processes auto; 
error_log /var/log/nginx/error.log; 
pid /run/nginx.pid; 

events { 
worker_connections 1024; 
} 

http { 
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; 
tcp_nopush on; 
tcp_nodelay on; 
keepalive_timeout 65; 
types_hash_max_size 2048; 

server_tokens off; 

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

limit_conn_zone $binary_remote_addr zone=addr:8m; 
limit_req_zone $binary_remote_addr zone=mylimit:16m rate=16r/s; 

gzip on; 
gzip_vary on; 
gzip_min_length 1024; 
gzip_types text/css text/plain; 

server { 
listen 80 default_server; 
listen [::]:80 default_server; 
server_name abcj.me; 
root /srv; 
index index-l1.html 

client_body_timeout 8; 
client_header_timeout 8; 

location ~* \.(css|ico|jpeg|jpg|js|mp3|mp4|pdf|png|txt|webp|zip)$ { 
expires 16d; 
} 

location ~* \.(html)$ { 
expires 1d; 
} 

error_page 404 /404-l1.html; 
location = /40x.html { 
} 

error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
} 
} 

server { 
listen 80; 
server_name www.abcj.me; 
return 301 http://abcj.me$request_uri; 
} 
} 

答案2

用户 www-data;worker_processes 4;pid /run/nginx.pid;包括 /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 4096; ## max clients = worker_processes * worker_connections
        multi_accept       on;
        use                epoll;
}

http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        client_body_timeout 12;
        client_header_timeout 12;
        send_timeout 10;
        types_hash_max_size 2048;
        server_tokens off;

        proxy_buffering on;
        proxy_buffer_size 16k;
        proxy_buffers 64 4k;
        proxy_busy_buffers_size 24k;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ## more tuning
        client_max_body_size 30M;
        client_body_buffer_size 12K;
        server_names_hash_max_size 1024;
        server_names_hash_bucket_size 112;

        client_header_buffer_size 2k;
        large_client_header_buffers 6 8k;

        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##
        #access_log     off;
        log_not_found  off;
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        ##
        # Gzip Settings
        ##
        gzip  on;
        gzip_vary on;
        gzip_min_length 1000;
        gzip_comp_level 9;
        gzip_proxied expired no-cache no-store private auth;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_disable "MSIE [1-6]\.";

        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

相关内容