如何优化Nginx 如何优化Nginx

如何优化Nginx

1.worker_processes的进程数,数量要与CPU数量一致,通过lscpu查看

worker_processes 1; 
      
      

2.worker process打开文件数的优化

worker_rlimit_nofile 65535;
      
      

2.1 优化了nginx的worker进程最多打开数量的参数之后,还需要优化系统内核参数(允许打开最多文件的参数)

            临时配置:           


      
      
  1. ulimit -Hn 100000
  2. ulimit -Sn 100000

            永久配置:


      
      
  1. vim /etc/security/limits.conf
  2. * soft nofile 100000
  3. * hard nofile 100000

3.单个进程最大连接数的优化


      
      
  1. events {
  2. worker_connections 2048;
  3. multi_accept on;
  4. use epoll;
  5. }

4.隐藏版本信息的优化

server_tokens off;
      
      

5.高效文件传输模式的   


      
      
  1. sendfile on;
  2. tcp_nopush on;
  3. tcp_nodelay on;

6.访问日志关闭的优化

access_log off;

      
      

7.超时时间的优化


      
      
  1. keepalive_timeout 10; //设置客户端保持活动状态的超时时间
  2. client_header_timeout 10; //客户端请求头读取超时时间
  3. client_body_timeout 10; //客户端请求体读取超时时间
  4. reset_timedout_connection on; //在客户端停止响应之后,允许服务器关闭连接,释放socket关联的内存
  5. send_timeout 10; //指定客户端的超时时间,如果在10s内客户端没有任何响应,nginx会自动断开连接

8.gzip的优化


      
      
  1. gzip on; //开启压缩
  2. gzip_min_length 1000; //小文件不压缩
  3. gzip_comp_level 6; //压缩比例
  4. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; //对指定文件类型进行压缩

9.缓存静态页面的优化 (文件句柄是打开文件的唯一标示)


      
      
  1. open_file_cache max= 100000 inactive= 20s; //设置服务器最大缓存10万个文件句柄,关闭20s内无请求的句柄
  2. open_file_cache_valid 30s; //文件句柄的有效期为30s
  3. open_file_cache_min_uses 2; //最少打开2次才会被缓存
  4. open_file_cache_errors on;

附:nginx主配置文件

    点击此处下载nginx.conf

1.worker_processes的进程数,数量要与CPU数量一致,通过lscpu查看

worker_processes 1; 
  
  

2.worker process打开文件数的优化

worker_rlimit_nofile 65535;
  
  

2.1 优化了nginx的worker进程最多打开数量的参数之后,还需要优化系统内核参数(允许打开最多文件的参数)

            临时配置:           


  
  
  1. ulimit -Hn 100000
  2. ulimit -Sn 100000

            永久配置:


  
  
  1. vim /etc/security/limits.conf
  2. * soft nofile 100000
  3. * hard nofile 100000

3.单个进程最大连接数的优化


  
  
  1. events {
  2. worker_connections 2048;
  3. multi_accept on;
  4. use epoll;
  5. }

4.隐藏版本信息的优化

server_tokens off;
  
  

5.高效文件传输模式的   


  
  
  1. sendfile on;
  2. tcp_nopush on;
  3. tcp_nodelay on;

6.访问日志关闭的优化

access_log off;

  
  

7.超时时间的优化


  
  
  1. keepalive_timeout 10; //设置客户端保持活动状态的超时时间
  2. client_header_timeout 10; //客户端请求头读取超时时间
  3. client_body_timeout 10; //客户端请求体读取超时时间
  4. reset_timedout_connection on; //在客户端停止响应之后,允许服务器关闭连接,释放socket关联的内存
  5. send_timeout 10; //指定客户端的超时时间,如果在10s内客户端没有任何响应,nginx会自动断开连接

8.gzip的优化


  
  
  1. gzip on; //开启压缩
  2. gzip_min_length 1000; //小文件不压缩
  3. gzip_comp_level 6; //压缩比例
  4. gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; //对指定文件类型进行压缩

9.缓存静态页面的优化 (文件句柄是打开文件的唯一标示)


  
  
  1. open_file_cache max= 100000 inactive= 20s; //设置服务器最大缓存10万个文件句柄,关闭20s内无请求的句柄
  2. open_file_cache_valid 30s; //文件句柄的有效期为30s
  3. open_file_cache_min_uses 2; //最少打开2次才会被缓存
  4. open_file_cache_errors on;

附:nginx主配置文件

    点击此处下载nginx.conf

猜你喜欢

转载自blog.csdn.net/guhong888/article/details/82454468