Linux高并发Nginx配置优化

辰星2年前技术文章767

在日常的运维工作中,经常会用到nginx服务,也时常会碰到nginx因高并发导致的性能瓶颈问题。一般来说nginx配置文件中对优化比较有作用的主要有以下几项:
1)nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数。
worker_processes 8;

2)为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

3)下面这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
worker_rlimit_nofile 65535;

4)使用epoll的I/O模型,用这个模型来高效处理异步事件
use epoll;

5)每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。
worker_connections 65535;

6)http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃!
keepalive_timeout 60;

7)客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。
client_header_buffer_size 4k;

8)下面这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
open_file_cache max=102400 inactive=20s;

9)下面这个是指多长时间检查一次缓存的有效信息。
open_file_cache_valid 30s;

10)open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
open_file_cache_min_uses 1;

Nginx配置文件:
user   www www;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;
error_log   /www/log/nginx_error.log   crit;
pid         /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
 
events
{
  use epoll;
  worker_connections 65535;
}
 
http
{
  include       mime.types;
  default_type   application/octet-stream;
 
  charset   utf-8;
 
  server_names_hash_bucket_size 128;
  client_header_buffer_size 2k;
  large_client_header_buffers 4 4k;
  client_max_body_size 8m;
 
  sendfile on;
  tcp_nopush     on;
 
  keepalive_timeout 60;
 
  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2
                keys_zone=TEST:10m
                inactive=5m;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 16k;
  fastcgi_buffers 16 16k;
  fastcgi_busy_buffers_size 16k;
  fastcgi_temp_file_write_size 16k;
  fastcgi_cache TEST;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;
  fastcgi_cache_min_uses 1;
  fastcgi_cache_use_stale error timeout invalid_header http_500; 
  open_file_cache max=204800 inactive=20s;
  open_file_cache_min_uses 1;
  open_file_cache_valid 30s; 
 
  tcp_nodelay on;
  
  gzip on;
  gzip_min_length   1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
 
  server
  {
    listen       8080;
    server_name   huan.wangshibo.com;
    index index.php index.htm;
    root   /www/html/;
 
    location /status
    {
        stub_status on;
    }
 
    location ~ .*\.(php|php5)?$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fcgi.conf;
    }
 
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
    {
      expires       30d;
    }
 
    log_format   access   '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log   /www/log/access.log   access;
      }
}


相关文章

开源大数据集群部署(五)Freeipa客户端部署及配置(kerberos+ldap)

开源大数据集群部署(五)Freeipa客户端部署及配置(kerberos+ldap)

1、安装IPA-Client安装(每个节点均需安装)yum install ipa-client -y2、执行安装命令ipa-client-install3、执行完成后进入交互阶段4、安装完成后查看页...

MySQL运维实战之ProxySQL(9.6)SQL黑名单

利用mysql_query_rules表中的error_msg字段,可以实现SQL黑名单的功能。如果规则设置了error_msg,当SQL语句匹配这条规则时,proxysql会直接将error_msg...

Prometheus+Consul服务自动发现监控

Prometheus+Consul服务自动发现监控

为什么使用consulprometheus作为新一代的监控利器,有很多优点,部署起来也十分方便。部署prometheus后自然会需要使用prometheus去监控物理机或者虚拟机的资源,这里就需要使用...

大数据组件Apache NiFi

大数据组件Apache NiFi

概述NiFi是美国国家安全局开发并使用了8年的可视化数据集成产品,2014年NAS将其贡献给了Apache社区,2015年成为Apache顶级项目。是一个基于Web图形界面,通过拖拽、连接、配置完成基...

MySQL keepalived安装配置(二)

MySQL keepalived安装配置(二)

一、keepalived安装配置1.1、主备库安装keepalived服务:yum install -y keepalived yum install -y python MySQL-python1...

 大数据集群监控配置操作指导(二)node_exporter+mysql_exporter部署

大数据集群监控配置操作指导(二)node_exporter+mysql_exporter部署

2.node_exporter监控集群服务器(所有集群服务器)wget https://github.com/prometheus/node_exporter/releases/download/v1...

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。