nginx 优化

压缩优化
  • 注意:对已经压缩过了的图片如 gif,jpg,png,ico格式等是没有效果的,甚至会适得其反,具体情况调试查看下
  • 对html,js,css之类的文本有效果
gzip on;
gzip_min_length 2k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_static on;
gzip_proxied any;
gzip_types text/css text/xml application/javascript application/json text/plain text/javascript;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;   
传输优化
  • 指定是否使用sendfile系统调用来传输文件。
    sendfile系统调用在两个文件描述符之间直接传递数据(完全在内核中操作),从而避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。
  • tcp_nopush 配置和 tcp_nodelay “互斥”。它可以配置一次发送数据的包大小。也就是说,它不是按时间累计 0.2 秒后发送包,而是当包累计到一定大小后就发送。在 nginx 中,tcp_nopush 必须和 sendfile 搭配使用。
sendfile on;  #开启高效文件传输模式 
tcp_nopush on;  #减少网络报文段数量
tcp_nodelay on; #提高I/O性能
静态资源缓存域
http{
   # 设置缓存的目录,并且内存中缓存区名为cache,大小为128m,
   # 三天未被访问过的缓存自动清楚,磁盘中缓存的最大容量为2GB。
   proxy_cache_path /soft/nginx/cache levels=1:2 keys_zone=hot_cache:128m inactive=3d max_size=2g;
   server{
       location / {
           # 使用名为nginx_cache的缓存空间
           proxy_cache cache;
           # 对于200、206、304、301、302状态码的数据缓存1天
           proxy_cache_valid 200 206 304 301 302 1d;
           # 对于其他状态的数据缓存30分钟
           proxy_cache_valid any 30m;
           # 定义生成缓存键的规则(请求的url+参数作为key)
           proxy_cache_key $host$uri$is_args$args;
           # 资源至少被重复访问三次后再加入缓存
           proxy_cache_min_uses 3;
           # 出现重复请求时,只让一个去后端读数据,其他的从缓存中读取
           proxy_cache_lock on;
           # 上面的锁超时时间为3s,超过3s未获取数据,其他请求直接去后端
           proxy_cache_lock_timeout 3s;
           # 对于请求参数或cookie中声明了不缓存的数据,不再加入缓存
           proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
           # 在响应头中添加一个缓存是否命中的状态(便于调试)
           add_header Cache-status $upstream_cache_status;
       }
   }
}
参考链接:
  • https://haicoder.net/nginx/nginx-cache.html
  • https://www.w3schools.cn/nginx/nginx_proxy_cache.html
  • https://help.aliyun.com/document_detail/40077.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值