性能优化

  1. file-max 控制系统能打开的句柄数
  2. ulimit 控制进程能打开的句柄数

    修改linux单进程最大文件连接数

[root@ebs-41181 nginx]# vim /etc/security/limits.conf
##### 在文件最后加入如下内容
* soft nofile 102400
* hard nofile 102400

配置Nginx进程数量

worker_processes  auto;

配置系统最大句柄数量

#当前会话生效
systemctl -w fs.file-max=2000000
#永久生效
echo 'fs.file-max=2000000' >> /etc/sysctl.conf

打开日志buffer缓冲,避免磁盘的频繁写入(以下配置表示日志达到32K后才写入磁盘,如果3秒内日志没有达到32K强制写入磁盘) access_log logs/access.log json buffer=32k flush=3s;

开启后端502重试,upstream池里某个节点502时会转发请求重试

proxy_next_upstream error http_502 timeout;
proxy_next_upstream_tries 2;

长链接、打开文件缓存、客户端相关配置

open_file_cache           max=10240 inactive=20s;
open_file_cache_valid     30s;
open_file_cache_min_uses  1;
reset_timedout_connection on;

keepalive_timeout       45s;
keepalive_requests      2000;
client_header_timeout   15s;
client_body_timeout     15s;
proxy_connect_timeout   5s;
variables_hash_max_size 1024;

server_tokens                 off;
proxy_intercept_errors        on;
fastcgi_intercept_errors      on;
client_header_buffer_size     16k;
large_client_header_buffers   4 32k;
client_max_body_size          160m;
server_names_hash_max_size    1024;
server_names_hash_bucket_size 256;

打开gzip压缩,节约传输流量 gzip on; gzip_min_length 1k; gzip_buffers 16 8k; gzip_comp_level 6; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json application/javascript; gzip_vary on; gzip_proxied any;

去除客户顿啊header头中的Connection,指定为http1.1的长链接 proxy_redirect off ; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host;

代理到后端时添加header头,获取客户端真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Request-Id $request_id; proxy_set_header x-b3-traceid $request_id;

扩展

查看当前系统打开句柄最大数量(默认为内存的10%,单位KB)

`more /proc/sys/fs/file-max`

>查看当前已经打开句柄总数

`lsof|awk '{print $2}'|wc -l`


>根据打开文件句柄的数量降序排列,其中第二列为进程ID:

`lsof|awk '{print $2}'|sort|uniq -c|sort -nr|more`
Copyright © 运维知识库 all right reserved,powered by Gitbook文件修订时间: 2023-09-19 10:45:38

results matching ""

    No results matching ""