众所周知nginx有很强的分发静态文件的能力,很多时候nginx对静态资源分发能力的瓶颈和redis一样在主机的网卡上。

(一般虚拟机的网卡只有500mbps,如果你使用的是万兆的物理网卡就当我没说)

和redis对比,nginx有另外一个瓶颈在服务器的硬盘IO上,SSD硬盘情况会好一些, 所以很多情况下,我们会把 nginx的cache 做在系统的ssd硬盘上,

其实还可以直接把cache放到内存文件系统里,进一步提升磁盘io吞吐。

tmpfs

differences between ramfs and tmpfs

1
2
3
4
5
#!/bin/bash
mkdir /mnt/ramdisk
mount -t tmpfs -o size=512m tmpfs /mnt/ramdisk

echo 'tmpfs       /mnt/ramdisk tmpfs   nodev,nosuid,noexec,nodiratime,size=1024M   0 0' >> /etc/fstab

nginx

http cache config

1
2
3
4
5
6
http {
    more_set_headers 'Server: CachedLOL';

    proxy_cache_path /var/cache/nginx levels=1:2 use_temp_path=on keys_zone=one:500m max_size=5g inactive=120m;
    proxy_temp_path /var/cache/nginx/tmp 1 2;
}

location conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
upstream upV1 {
  server 172.26.2.5:9090 fail_timeout=0;
  server 172.26.2.6:9090 fail_timeout=0;
}

server {
  listen 80 default backlog=16384;
  server_name tab.deoops.com;

  location ~* ( /static.*|/list.+|/ )$ {
    proxy_redirect off;
    proxy_cache one;
    proxy_ignore_headers "Set-Cookie";
    proxy_hide_header "Set-Cookie";
    add_header X-Cache $upstream_cache_status;
    proxy_cache_key $uri$is_args$args$mobile;
    proxy_cache_min_uses 1;
    proxy_cache_valid 120m;
    proxy_cache_use_stale error timeout;
    proxy_buffering on;
    proxy_pass http://upV1;
  }