[nginx 配置]Nginx写IO占用高故障处理方法

时间:2019-10-22  来源:nginx  阅读:

故障现象
 
突然收到一台服务器负载过高告警,紧接着网站打开缓慢。
 
故障分析
 
1、登录服务器,使用top命令看到Cpu行的iowait达到了70%以上,所以断定是IO负载过高的原因;
2、接着使用iotop -o命令发现,Nginx的写IO特别大,并且在上一步的top命令看到Nginx的进程状态为D,表示Nginx在等待IO已经为僵死状态;
3、这时候是清楚知道是Nginx在对文件系统进行大量的写操作导致的系统负载过高了,但还是不能知道具体Nginx在写什么文件导致的负载压力,所以我们还需要继续追查下去;
4、我们找到其中一个nginx worker进程的pid,使用lsof -p pid列出来的文件发现除了一些系统库文件及日志文件,还有相当多的fastcgi_temp/xxx文件,有可能与这些文件有关联;
5、再次使用strace -p pid追踪,发现nginx进程对某个fd进行大量的写操作,与lsof命令列出来的文件刚好符合;
6、使用iostat 1输出的大量写io的分区也与fastcgi_temp所在分区相符合;
7、猜测可能是外部正在上传大量的大文件给php-fpm,于是通过EZHTTP的小工具来查看实时流量,发现入站流量其实不大。
 
分析结果
 
根据以上的故障分析,非常有可能是本机的某些程序通过http上传大量大文件。因为对程序逻辑不熟悉,也只是猜测。为了尽快恢复服务,决定实施以下解决方案。
 
解决方案
 
既然清楚知道了fastcgi_temp io压力大,目前也无法短时间从根本上解决问题,所以决定把fastcgi_temp指向/dev/shm,也就是映射到了内存,重启nginx之后服务恢复了正常。最终原因还需要开发配合解决。

后来小编又找到一些相关的解决办法供各位参考。

看了一下nginx wiki proxy章发现以下选项:

proxy_buffering
syntax: proxy_buffering on|off

default: proxy_buffering on

context: http, server, location

This directive activate response buffering of the proxied server.

If buffering is activated, then nginx assumes the answer of the proxied server as fast as possible, preserving it in the buffer, assigned by directive
proxy_buffer_size
and
proxy_buffers
.

If the response can not all be placed in memory, then parts of it will be written to disk.

If buffering is switched off, then the response is synchronously transferred to client immediately as it is received.

nginx do not attempt to count entire answer of the proxied server, maximum size of data, which nginx can accept from the server it is assigned by directive
proxy_buffer_size
.

For Comet applications based on long-polling it is important to set proxy_buffering to off, otherwise the asynchronous response is buffered and the Comet does not work.

总算发现问题所在了,设置off重启, 一切正常。

再补充两种解决办法


有两种办法,一种是换ssd硬盘的服务器,

一种是把网站的一些经常访问的网页加载到内存中。

第一种,我查了,ssd的服务器基本没有,有也只是有些vps,配置都不咋滴。

第二种,不会弄啊,有什么其他的处理方法没?

我最大并发才1万左右,我就纳闷了,我看到很多人拿nginx测试,随随便便一个低端的服务器都可以抗起三四万并发,我才一万左右,而且服务器还是买的比较好的了,都抗不住。表示不解!!!

贴上我的配置文件:

user  www www;
worker_processes 8;
#worker_cpu_affinity auto;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
worker_rlimit_nofile 65535;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  20480;
}

http {
    server_names_hash_bucket_size 512; 
    server_names_hash_max_size 512;
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  "$remote_addr - $remote_user [$time_local] "$request" "
    #                  "$status $body_bytes_sent "$http_referer" "
    #                  ""$http_user_agent" "$http_x_forwarded_for"";


    #access_log  logs/access.log  main;


     sendfile        on;
    tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay on;
    server_tokens off;
    gzip  on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types    text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
   
    include vhost/*.conf;
vhost 里面的网站的配置

server {

listen       80;
server_name www.xxx.com *.xxx.com;
  location / {
  root /home/wwwroot/xxx.com;
error_page  404  /404.html;
error_page  500 502 503 504  /50x.html;
}
}

[nginx 配置]Nginx写IO占用高故障处理方法

http://m.bbyears.com/caozuoxitong/74832.html

推荐访问:nginx负载均衡配置
相关阅读 猜你喜欢
本类排行 本类最新