nginx 做缓存
原已经安装好的nginx,现在需要添加一个未被编译安装的模块:
nginx -V 可以查看原来编译时都带了哪些参数
原来的参数:
--prefix=/app/nginx
添加的参数:
--with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
本机 192.168.1.7
nginx.conf
##cache##
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path /home/temp_dir;
proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
proxy_headers_hash_bucket_size 1024;
proxy_headers_hash_max_size 512;
##end##
upstream backend_server {
server 192.168.1.5:80 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
listen 80;
server_name www.lrm.com 192.168.1.7;
index index.php index.html index.htm;
root /usr/www/lrm;
location /
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
location ~ /purge(/.*)
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
location ~ .*\.(php|jsp|cgi)?$
}
缓存 静态文件
更高级的缓存
Nginx高性能优化插件与缓存的安装和配置
2012-01-03 / Linux安全 / Linux, Nginx, 性能优化, 缓存 /
1.1下载模块:
memc-nginx-module
下载地址: https://github.com/agentzh/memc-nginx-module/downloads
srcache-nginx-module
下载地址: https://github.com/agentzh/srcache-nginx-module/downloads
ngx_http_upstream_keepalive
下载地址: http://mdounin.ru/hg/ngx_http_upstream_keepalive/
解压在Nginx安装目录的同级目录下。
接着添加google-perftools-module模块,用来优化高并发时的内存管理性能
先安装 libunwind
1
2
3
4
<code># wget http://download.savannah.gnu.org/releases/libunwind/libunwind-1.0.1.tar.gz</code>
<code># CFLAGS=-fPIC ./configure --prefix=/usr</code>
<code># make CFLAGS=-fPIC</code>
<code># make CFLAGS=-fPIC install</code>
再安装 google-perftools
5
6
<code>#wget http://google-perftools.googlecode.com/files/google-perftools-1.9.1.tar.gz</code>
<code># ./configure --prefix=/usr --enable-frame-pointers (32位可以不添加--enable-frame-pointers)</code>
<code># make && make install</code>
<code># echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf</code>
<code># /sbin/ldconfig</code>
1.2安装编译:
1.2.1.进入Nginx安装目录。先去掉Nginx的Debug功能,编辑auto/cc/gcc,找到
<code># debug</code>
<code>CFLAGS="$CFLAGS -g"</code>
然后注释掉CFLAGS行,如下
<code>#CFLAGS="$CFLAGS -g"</code>
1.2.2.查看CPU型号
<code># cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c</code>
如果是Intel,一般PC机是显示 Pentium(R) Dual-Core CPU E6500 @ 2.93GHz , 前面是Pentium.
如果是AMD, 一般就是opteron.
如果是服务器,一般是Core
具体的要在生产服务器中查找对应的CPU弄号参数
这个型号在接下来编译的时候指定给–with-cpu-opt参数
1.2.3.生成配置
7
8
9
10
11
<code># ./configure --prefix=/usr/local/nginx</code>
<code>--with-http_stub_status_module</code>
<code>--with-http_ssl_module</code>
<code>--add-module=..</code><code>/memc-nginx-module</code>
<code>--add-module=..</code><code>/srcache-nginx-module</code>
<code>--add-module=..</code><code>/ngx_http_upstream_keepalive</code>
<code>--user=nginx</code>
<code>--group=nginx</code>
<code>--with-cc-opt=</code><code>'-O3'</code>
<code>--with-cpu-opt=Pentium</code>
<code>--with-google_perftools_module</code>
1.2.4.编译
1.3 配置:
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<code>worker_processes 2; # 多少个CPU内核就开多少个进程,处理客户连接由进程开启线程来处理。</code>
<code>worker_cpu_affinity 0001 0010; #每一位代表一个进程,按顺序,0001代表第一个CPU,0010代表第二个CPU.这里把2个进程分配给两个CPU处理。</code>
<code>pid logs/nginx.pid;</code>
<code>google_perftools_profiles /tmp/tcmalloc; #使用google_preftools模块</code>
<code>worker_rlimit_nofile 65535; #每一个进程能打开的最大文件数, ulimit -n 得到。设置一样大小,因为Nginx不是平均给多个进程分配,所以最好设置一样大小。</code>
<code>events {</code>
<code></code><code>use epoll; #使用 epoll处理Socket,是异步事件的通知机制,不是传统的轮询,增加性能。与windows的IOCP相拟.</code>
<code></code><code>worker_connections 15000; #每个进程最大连接数 ,客户端连接数max_clients = worker_processes * worker_connections,2个进程,要开到3W并发,所以,设置1.5W</code>
<code>}</code>
<code>http {</code>
<code></code><code>include mime.types;</code>
<code></code><code>default_type application/octet-stream;</code>
<code></code>
<code></code><code>log_format main '$remote_addr - $remote_user [$time_local] "$request" '</code>
<code></code><code>'$status $body_bytes_sent "$http_referer" '</code>
<code></code><code>'"$http_user_agent" "$http_x_forwarded_for"';</code>
<code></code><code>charset utf-8;</code>
<code></code><code>server_names_hash_bucket_size 128;</code>
<code></code><code>client_header_buffer_size 2k;</code>
<code></code><code>large_client_header_buffers 4 4k;</code>
<code></code><code>client_max_body_size 8m;</code>
<code></code><code>sendfile on;</code>
<code></code><code>tcp_nopush on; #如果使用了keepalive,那就很有必要打开这个选项,因为keepalive不会关闭连接,所以需要显式的调用tcp_push来将数据push出去,这个选项在sendfile on时生效。</code>
<code></code><code>tcp_nodelay on; #tcp_nodelay和tcp_nopush是互斥的。不过如果你同时设置了两个值的话,将会在第一个buf发送的时候,强制push数据,而第二个buf时,将会调用tcp_cork来打开nagle算法,也就是后面的都会应用tcp_nopush.</code>
<code></code><code>server_tokens off; #nginx版本号</code>
<code></code><code>keepalive_timeout 65;</code>
<code></code><code>gzip on; #开启或者关闭gzip模块</code>
<code></code><code>gzip_min_length 1k; #默认值是0,不管页面多大都压缩。建议设置成大于1k的字节数,小于1k可能会越压越大。</code>
<code></code><code>gzip_buffers 4 8k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 8k 代表以8k为单位,按照原始数据大小以8k为单位的4倍申请内存。如果没有设置,默认值是申请跟原始数据相同大小的内存空间去存储gzip压缩结果。</code>
<code></code><code>gzip_comp_level 2; #gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢(传输快但比较消耗cpu)</code>
<code></code><code>gzip_types text/plain application/x-javascript text/css application/xml; # 匹配MIME类型进行压缩,(无论是否指定)"text/html"类型总是会被压缩的。</code>
<code></code><code>gzip_vary on; #启用 "Vary: Accept-Encoding"</code>
<code></code><code>#Load config files from the ../conf.d directory</code>
<code></code><code>include ../conf.d/*.conf;</code>
<code></code><code>#Memcache Service upstream </code>
<code></code><code>upstream memcache{</code>
<code></code><code>server 10.66.6.59:11211; #memcached服务器地址</code>
<code></code><code>keepalive 512 single; #保持512个不立即关闭的连接</code>
<code></code><code>}</code>
<code></code><code># Web Service upstream</code>
<code></code><code>upstream rubyd{ #ruby服务器端地址列表</code>
<code></code><code>server 10.66.1.226:3000 down; #down表示关闭,不参于负载</code>
<code></code><code>server 10.66.1.227:3000 weight=5; #权重值,</code>
<code></code><code>server 10.66.1.228:3000 weight=1 max_fails=3 fail_timeout 30s; #如果连接3次并且都超时30s,这个服务器将被视为失效,不再参于负载 </code>
<code></code><code>server 10.66.1.229:3000 weight=1; #weight默认是1</code>
<code></code><code># 上面的配置表示每7个用户连接,.227将处理5个请求,228,229都分配1个请求。</code>
<code></code><code>server {</code>
<code></code><code>listen 80;</code>
<code></code><code>server_name localhost;</code>
<code></code><code>#memc-nginx-module #memc模块配置, 我们配置为/memc,所有请求都通过请求这个location来操作 memcache</code>
<code></code><code>location /memc {</code>
<code></code><code>internal; #只接受内部访问,不接收外部http请求</code>
<code></code><code>memc_connect_timeout 100ms; #memc连接超时时长。</code>
<code></code><code>memc_send_timeout 100ms; #memc发送超时时长。</code>
<code></code><code>memc_read_timeout 100ms; #memc读取超时时长。</code>
<code></code><code>set $memc_key $query_string; #使用Nginx内置的$query_string来作为 key</code>
<code></code><code>set $memc_exptime 300; #缓存失效时间</code>
<code></code><code>memc_pass memcache; #memc访问的upstream为memcache</code>
<code></code><code>location / { #所有访问</code>
<code></code><code>#root html;</code>
<code></code><code>index index.html index.htm;</code>
<code></code><code>#srcache-nginx-module #srcache模块配置</code>
<code></code><code>set $key $uri$args; #设置key为网址加参数的值。</code>
<code></code><code>#下面两步是以location的逻辑条件来执行,GET与PUT的优先级来决定的。</code>
<code></code><code>srcache_fetch GET /memc $key; #从/memc中查询$key, 以$uri$args为key的数据,如果有则直接返回.</code>
<code></code><code>srcache_store PUT /memc $key; #如果上一步没有直接返回,则跳到这一步,如果返回的http状态码为200,则在输出前 以$uri$args为key,将输入结果存入memcache。</code>
<code></code><code>#proxy</code>
<code></code><code>proxy_pass http://rubyd; #代理访问,指向名为 rubyd 的 upstream</code>
<code></code><code>proxy_set_header Host $host; #设置http头,转发端口域名给后端</code>
<code></code><code>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #设置http头,转发X-Forwarded-For给后端</code>
<code></code><code>error_page 500 502 503 504 /50x.html;</code>
<code></code><code>location = /50x.html {</code>
<code></code><code>root html;</code>
逻辑要点解释:
memc-nginx是一个标准的upstream模块,因此首先需要定义memcache的upstream。
这里我在本机上启动了一 个memcache服务,端口为默认的11211,keepalive指令是http-upsteram-keepalive-module提供的功能, 这里我们最大保持512个不立即关闭的连接用于提升性能。
下面是为memc-nginx-module配置location,我们配置为/memc,所有请求都通过请求这个location来操作 memcache.
memc-nginx-module存取memcache是基于http method语义的,使用http的GET方法表示get、PUT方法表示set、DELETE方法表示delete。
这里我们将/memc设为internal表示只接受内部访问,不接收外部http请求,这是为了安全考虑,当然如果需要通过http协议开放外部访问,可以去掉internal然后使用deny和allow指 令控制权限。比较重要的是$memc_key这个变量,它表示以什么作为key,这里我们直接使用Nginx内置的$query_string来作为 key,$memc_exptime表示缓存失效时间,以秒记。这里统一设为300(5分钟),在实际应用中可以根据具体情况为不同的内容设置不同的过期时间。
最后我们为“/”这个location配置了缓存,这表示所有的请求都会结果被缓存,当然这里只是示例需要,实际中一般不会这么配,而是为特定需要缓存的location配置缓存。 比如只缓存图片,js,css等资源文件。
srcache_fetch表示注册一个输入拦截处理器到location,这个配置将在location进入时被执行;
而 srcache_store表示注册一个输出拦截器到location,当location执行完成并输出时会被执行。
注意srcache模块实际可以与任何缓存模块进行配合使用,而不必一定是memc。这里我们以$uri$args作为缓存的key。
经过上述配置后,相当于对Nginx增加了如下逻辑:当所请求的uri以“.php”结尾时,首先到memcache中查询有没有 以$uri$args为key的数据,如果有则直接返回;否则,执行location的逻辑,如果返回的http状态码为200,则在输出前 以$uri$args为key,将输入结果存入memcache。