Young's blog Young's blog
首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Young

首页
Spring
  • 前端文章1

    • JavaScript
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • HTML
  • CSS
  • 技术文档
  • GitHub技巧
  • Nodejs
  • 博客搭建
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Nginx基础
  • Nginx配置
  • Nginx proxy_pass 配置转发 / 路径
  • Nginx 配置密码认证
  • Nginx 配置 CPU 亲和性
  • Nginx 配置 UDP/TCP/WebSocket 反向代理
  • Nginx 配置目录文件列表显示功能
  • Nginx 配置 gzip 压缩、缓存功能
    • 开启 gzip 方法
    • 开启缓存
  • Nginx 配置端口转发
  • Nginx底层进程机制剖析
  • nginx配置location匹配顺序总结
  • nginx
andanyang
2023-03-29
目录

Nginx 配置 gzip 压缩、缓存功能

默认情况下,Nginx 的 gzip 压缩是关闭的,gzip 压缩功能就是可以让你节省不少带宽,但是会增加服务器 CPU 的开销哦,Nginx 默认只对 text html 进行压缩 ,如果要对 html 之外的内容进行压缩传输,我们需要手动来调。

# 开启 gzip 方法

开启 gzip 的相关指令位于 http{ … }两个大括号之间,代码如下:

  http {
    ...
    gzip on;
    gzip_http_version 1.1;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain text/xml application/json application/javascript application/x-javascript text/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    gzip_buffers 4 8k;
    ...
  }
1
2
3
4
5
6
7
8
9
10
11
12

# 开启缓存

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

location ~* ^.+\.(css|js|txt|xml|swf|wav)$ {
    access_log off;
    expires 30d;
    add_header Cache-Control private;
}

location ~* ^.+\.(html|htm)$ {
     expires 1h;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

其中的缓存时间可以自己根据需要修改。

编辑 (opens new window)
上次更新: 2024/04/19, 08:52:45
Nginx 配置目录文件列表显示功能
Nginx 配置端口转发

← Nginx 配置目录文件列表显示功能 Nginx 配置端口转发→

最近更新
01
idea 热部署插件 JRebel 安装及破解,不生效问题解决
04-10
02
spark中代码的执行位置(Driver or Executer)
12-12
03
大数据技术之 SparkStreaming
12-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Young | MIT License
浙ICP备20002744号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式