nginx开启gzip及缓存

nginx实现资源压缩的原理是通过ngx_http_gzip_module模块拦截请求,并对需要做gzip的类型做gzip压缩,该模块是默认基础的,不需要重新编译,直接开启即可

gzip参数配置

    #开启和关闭gzip模式
    gzip on|off;
    
    #gizp压缩起点,文件大于1k才进行压缩
    gzip_min_length 1k;
    
    # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间
    gzip_comp_level 1;
    
    # 进行压缩的文件类型。根据网页需要自行调整
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript ;
    
    #nginx对于静态文件的处理模块,开启后会寻找以.gz结尾的文件,直接返回,不会占用cpu进行压缩,如果找不到则不进行压缩
    gzip_static on|off
    
    # 是否在http header中添加Vary: Accept-Encoding,建议开启
    gzip_vary on;

    # 设置压缩所需要的缓冲区大小,以4k为单位,如果文件为7k则申请2*4k的缓冲区,可以不填写,一般默认即可
    gzip_buffers 2 4k;

    # 设置gzip压缩针对的HTTP协议版本
    gzip_http_version 1.1;

可以使用站长之家的gzip工具检测压缩效果: https://tool.chinaz.com/gzips/

配置缓存

# 类型与时间自行控制

location ~* ^.+\.(ico|gif|jpg|jpeg|png)$ { 
        access_log   off; 
        expires      30d;
}

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
    access_log   off;
    expires      24h;
}

# 以文件结尾匹配
location ~* \.(html|htm)$ {
        expires      1h;
}

注意事项,如果服务器静态资源是以alias, try_files形式匹配的,以上写法可能导致资源访问不到,nginx的location有优先级之分,

配置demo

server{
    listen 80;
    server_name location;
    rewrite ^(.*)$ https://${server_name}$1 permanent; 
    # return 301 https://$host$request_uri;
}
server {
        listen   443 ssl;
        server_name location;
        ssl_certificate "/path/x.pem";
        ssl_certificate_key "/path/x.key";
        ssl_session_cache shared:le_nginx_SSL:1m;
        ssl_session_timeout 1440m;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        ssl_ciphers "ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS";

        index index.html index.htm index.php;
        add_header X-Frame-Options SAMEORIGIN;
        add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;


        location / {
            # root /path/;                 # root 会将 location 匹配的路径缀在 root 参数的后面
            alias /path/;                  # alias 则是将 location 匹配的路径的目录部分,替换为 alias 的参数
            # index index.html index.htm;     # index 是处理请求路径结尾是 / 的情况
            try_files $uri $uri/ /index.html;  # try_files强制请求重新指向 index.html
            
          
            # 以文件名匹配,加缓存
            if ($request_filename ~* \.(ico|eot|ttf|otf|woff|svg|mp3)$) {
                access_log off;
                expires max;
            }

            if ($request_filename ~* \.(css|js|txt|xml|swf)$) {
                access_log off;
                expires 24h;
            }

            if ($request_filename ~* \.(html|htm)$) {
                access_log off;
                expires 1h;
            }

        }


        location /api {
            proxy_pass http://127.0.0.1:5001;
            proxy_http_version 1.1;
            # proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_set_header Host  $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            # proxy_redirect http://$host https://$host:$server_port;
            proxy_redirect off;

        }


}

proxy_cache缓存: 参考 《Nginx缓存开启》


参考链接

加速nginx
nginx开启gzip

添加新评论