欢迎光临散文网 会员登陆 & 注册

nginx性能优化的6种方向

2023-02-16 09:46 作者:学酥两支笔  | 我要投稿

思路:

多进程 multiprocessing

输入输出 IO (DIO、AIO Asynchronous)

缓冲区 buffer 针对时间,分批,write-buffer

快速缓冲贮存区 cache 针对存储,分块,read-cache,大多数时候用cache代替buffer可以

通用网关接口 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;

        }


nginx性能优化的6种方向的评论 (共 条)

分享到微博请遵守国家法律