nginx高并发下配置参数

时间:2023-01-19 19:17:05
今天下午,测试组同事模拟800个用户同时发起请求,nginx开始报错,
"Too Many Open Files"
 我们使用的是Dell R430服务器,2个物理CPU,每个CPU包含6个内核;
解决方法:
1. 修改系统最大连接数配置:
echo "fs.file-max = 70000" >> /etc/sysctl.conf  &&  echo "nginx soft nofile 10000" >>  /etc/security/limits.conf && echo "nginx hard nofile 30000" >> /etc/security/limits.conf && sysctl -p && sed -i '3a\worker_rlimit_nofile 30000;' /etc/nginx/nginx.conf
 
2. 修改nginx配置文件/etc/nginx/nginx.conf
 

user nginx;
worker_processes 6;
worker_rlimit_nofile 30000;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
  worker_connections 10240;
}

http {
  include /etc/nginx/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 /var/log/nginx/access.log main;

          sendfile on;
          #tcp_nopush on;
          keepalive_timeout 65;
          #gzip on;
          upstream epgservers{
                server 192.168.89.211:81 max_fails=3 fail_timeout=5s;
                server 192.168.89.212:81 max_fails=3 fail_timeout=5s;
                server 192.168.89.213:81 max_fails=3 fail_timeout=5s;
          }

          include /etc/nginx/conf.d/*.conf;

}

/etc/nginx/conf.d/default.conf

server {
  listen 80;
  server_name localhost;

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location / {
    client_max_body_size 300m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 300;
    proxy_read_timeout 300;
    proxy_send_timeout 300;
    proxy_buffer_size 64k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;

    proxy_pass http://epgservers;
  }
}

/etc/nginx/conf.d/http.conf

server {
  listen 81;
  server_name localhost;

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location / {
    root /var/www/html;
    index index.html index.htm index.php;
  }

  #error_page 404 /404.html;

  # redirect server error pages to the static page /50x.html
  #
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  # proxy_pass http://127.0.0.1;
  #}

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #

  location ^~ /image/ {
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    if ($request_uri ~ /image/(\d+).(\d+).(\d+).(\d+)/(\d+)/(.+))
    {
      set $ip $1.$2.$3.$4;
      set $port $5;
      set $path $6;
    }
    proxy_pass http://$ip:$port/$path;
  }

  location ~ \.php$ {

    root /var/www/html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_connect_timeout 150;
    fastcgi_read_timeout 150;
    fastcgi_send_timeout 150;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 32k;
    fastcgi_busy_buffers_size 64k;
    fastcgi_temp_file_write_size 64k;
    include fastcgi_params;
    client_max_body_size 10240m;
    client_body_buffer_size 10m;

  }
  

      location /vod{

    alias /usr/local/content  ;

  }


  
#image server rewrite
  rewrite /imgfs/weed/(.*) /app/Imgfs.php?fid=$1 last;
  rewrite ^/sunboss/(\d+)/(.+)  /sunboss/$2?hostid=$1 last;

  
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  # deny all;
  #}
  }

3. 重启nginx
service nginx restart
 
4. 检查确认:
 
ps -ef|grep nginx
root      58651      1  0 18:23 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     58652  58651  0 18:23 ?        00:00:06 nginx: worker process
nginx     58653  58651  0 18:23 ?        00:00:03 nginx: worker process
nginx     58654  58651  0 18:23 ?        00:00:05 nginx: worker process
nginx     58655  58651  0 18:23 ?        00:00:05 nginx: worker process
root      68259  34657  0 18:26 pts/0    00:00:00 tailf /var/log/nginx/error.log
root      83634  82399  0 18:27 pts/2    00:00:02 tailf /var/log/nginx/access.log
root     146535  55465  0 18:54 pts/1    00:00:00 grep --color=auto nginx
 
 
cat /proc/58655/limits 
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             127387               127387               processes 
Max open files            30000                30000                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       127387               127387               signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us    
 
 
第二台应用节点绑定epgservice虚拟IP, 三台应用节点都要修改;