nginx性能優(yōu)化的6種方向
思路:
多進(jìn)程 multiprocessing
輸入輸出 IO (DIO、AIO Asynchronous)
緩沖區(qū) buffer 針對時間,分批,write-buffer
快速緩沖貯存區(qū) cache 針對存儲,分塊,read-cache,大多數(shù)時候用cache代替buffer可以
通用網(wǎng)關(guān)接口 fastcgi(Common Gateway Interface)
1 multiprocessing
grep ^processor /proc/cpuinfo | wc -l
vim /etc/nginx/nginx.conf
vim /etc/nginx/conf.d/default.conf
worker_processes auto
systemctl restart nginx
ps -aux | grep nginx |grep -v grep
2 IO
事件處理模型
events {
use epoll;
}
windows中是kqueue
3 buffer
vim /etc/nginx/nginx.conf
client_body_buffer_size 512k;
4 cache?
為1,000個元素定義了一個緩存,到期時間為60秒
http
{
? ?open_file_cache max=1000 inactive=60s;
}
5 fastcgi
mkdir /home/www/cache
vim /etc/nginx/conf.d/default.conf
server外添加
fastcgi_cache_path /home/www/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "schemerequest_methodhostrequest_uri";
php中添加
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
systemctl reload nginx
6 expire
可以在http段中或者server段中或者location段中加入
root? ?/home/www/wuye/public;
? ? ? ? location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
? ? ? ? {
? ? ? ? ? ? expires? ? ? 1d;
? ? ? ? }
? ? ? ? location ~ .*\.(js|css)?$
? ? ? ? {
? ? ? ? ? ? expires? ? ? 1h;
? ? ? ? }