• Linux系统--------内核参数调优、一键安装nginx、tomcat调优


    一、内核参数调优

    默认的Linux内核参数考虑的是最通用场景,不符合用于支持高并发访问的Web服务器的定义,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者提供压缩服务器的服务器时,内核参数的调整都是不同的,此处针对最通用的、使Nginx支持更多并发请求的TCP网络参数做简单的配置

    修改配置文件/etc/sysctl.conf

    1. fs.file-max = 1000000
    2. #表示单个进程较大可以打开的句柄数
    3. net.ipv4.tcp_tw_reuse = 1
    4. #参数设置为 1 ,表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,这对于服务器来说意义重大,因为总有大量TIME_WAIT状态的链接存在
    5. net.ipv4.tcp_keepalive_time = 600
    6. #当keepalive启动时,TCP发送keepalive消息的频度;默认是2小时,将其设置为10分钟,可更快的清理无效链接
    7. net.ipv4.tcp_fin_timeout = 30
    8. #当服务器主动关闭链接时,socket保持在FIN_WAIT_2状态的较大时间
    9. net.ipv4.tcp_max_tw_buckets = 5000
    10. #表示操作系统允许TIME_WAIT套接字数量的较大值,如超过此值,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为8000,过多的TIME_WAIT套接字会使Web服务器变慢
    11. net.ipv4.ip_local_port_range = 1024 65000
    12. #定义UDP和TCP链接的本地端口的取值范围
    13. net.ipv4.tcp_rmem = 10240 87380 12582912
    14. #定义了TCP接受缓存的最小值、默认值、较大值
    15. net.ipv4.tcp_wmem = 10240 87380 12582912
    16. #定义TCP发送缓存的最小值、默认值、较大值
    17. net.core.netdev_max_backlog = 8096
    18. #当网卡接收数据包的速度大于内核处理速度时,会有一个列队保存这些数据包。这个参数表示该列队的较大值
    19. net.core.rmem_default = 6291456
    20. #表示内核套接字接受缓存区默认大小
    21. net.core.wmem_default = 6291456
    22. #表示内核套接字发送缓存区默认大小
    23. net.core.rmem_max = 12582912
    24. #表示内核套接字接受缓存区较大大小
    25. net.core.wmem_max = 12582912
    26. #表示内核套接字发送缓存区较大大小
    27. 注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑
    28. net.ipv4.tcp_syncookies = 1
    29. #与性能无关。用于解决TCP的SYN攻击
    30. net.ipv4.tcp_max_syn_backlog = 8192
    31. #这个参数表示TCP三次握手建立阶段接受SYN请求列队的较大长度,默认1024,将其设置的大一些可使出现Nginx繁忙来不及accept新连接时,Linux不至于丢失客户端发起的链接请求
    32. net.ipv4.tcp_tw_recycle = 1
    33. #这个参数用于设置启用timewait快速回收
    34. net.core.somaxconn=262114
    35. #选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此需要结合高并发请求数来调节此值。
    36. net.ipv4.tcp_max_orphans=262114
    37. #选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立链接将立即被复位并输出警告信息。这个限制指示为了防止简单的DOS攻击,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加这个值

    二、一键安装Nginx

    1. #! /bin/bash
    2. version=1.15.4
    3. #判断函数是否执行成功
    4. function show_result(){
    5. if [ "$1" -eq 0 ]
    6. then
    7. echo -e "\e[32m$2 is Success . [ OK ] \e[0m"
    8. else
    9. echo -e "\e[31m$2 is Fail . [ FAIL ] \e[0m"
    10. fi
    11. }
    12. #创建 nginx 用户和用户组
    13. function user_create(){
    14. local item="Create User and Group"
    15. if [ `cat /etc/{passwd,group} | grep nginx | wc -l ` -ge 2 ];
    16. then
    17. echo -e "\e[31mUser and Group exist! \e[0m"
    18. else
    19. groupadd -g 1004 nginx && \
    20. useradd -u 1004 -g 1004 -M -s /sbin/nologin nginx
    21. show_result $? "${item}"
    22. fi
    23. }
    24. #下载一些拓展包
    25. function nginx_pkg(){
    26. local item="Packages Install"
    27. yum -y install gcc openssl-devel pcre-devel zlib-devel > /dev/null 2>&1
    28. show_result $? "${item}"
    29. }
    30. #下载nginx
    31. function nginx_download(){
    32. local item="Nginx Download"
    33. cd /usr/local/src && \
    34. wget http://nginx.org/download/nginx-${version}.tar.gz > /dev/null 2>&1
    35. test -e /usr/local/src/nginx-${version} || tar zxf nginx-${version}.tar.gz
    36. rm -rf /usr/local/src/nginx-${version}.tar.gz
    37. show_result $? "${item}"
    38. }
    39. #编译安装
    40. function nginx_compile(){
    41. local item="Nginx Compile"
    42. cd /usr/local/src/nginx-${version}
    43. if [ `ls -l /usr/local/ | grep 'nginx' | wc -l` -ge 1 ];
    44. then
    45. echo -e "\e[31mNginx exist! \e[0m"
    46. else
    47. ./configure --prefix=/usr/local/nginx > /dev/null 2>&1 && make > /dev/null 2>&1 && make install > /dev/null 2>&1
    48. fi
    49. show_result $? "${item}"
    50. }
    51. #软连接建立
    52. function nginx_softlink(){
    53. local item="Nginx Softlink"
    54. test -d /etc/nginx/ || ln -s /usr/local/nginx/conf/ /etc/nginx
    55. test -e /usr/sbin/nginx || ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
    56. show_result $? "${item}"
    57. }
    58. #注册服务
    59. function nginx_service(){
    60. local item="Nginx Service"
    61. test -e /usr/lib/systemd/system/nginx.service || \
    62. echo '
    63. [Unit]
    64. Description=The nginx HTTP and reverse proxy server
    65. After=network-online.target remote-fs.target nss-lookup.target
    66. Wants=network-online.target
    67. [Service]
    68. Type=forking
    69. PIDFile=/usr/local/nginx/logs/nginx.pid
    70. # Nginx will fail to start if /run/nginx.pid already exists but has the wrong
    71. # SELinux context. This might happen when running `nginx -t` from the cmdline.
    72. # https://bugzilla.redhat.com/show_bug.cgi?id=1268621ExecStartPre=/usr/bin/rm-f /usr/local/nginx/logs/nginx.pid
    73. ExecStartPre=/usr/local/nginx/sbin/nginx -t
    74. ExecStart=/usr/local/nginx/sbin/nginx
    75. ExecReload=/usr/local/nginx/sbin/nginx -s reload
    76. KillSignal=SIGQUIT
    77. TimeoutStopSec=5
    78. KillMode=process
    79. PrivateTmp=true
    80. ' > /usr/lib/systemd/system/nginx.service
    81. systemctl daemon-reload
    82. show_result $? "${item}"
    83. }
    84. #内核优化
    85. function nginx_kernel(){
    86. local item="Optimize Kernel Arguments"
    87. cp /etc/sysctl.conf /etc/sysctl.conf.${current_time} > /dev/null 2>&1
    88. arch_ratio=$([[ ! -z $(uname -a | grep x86_64) ]] && expr 64 / 32 || expr 32 / 32)
    89. memory_size=$(free -b| awk 'NR==2{print $2}')
    90. nf_conntrack_size=$(expr ${memory_size} / 16384 / ${arch_ratio})
    91. #开启反向路径过滤
    92. add_config_tofile "net.ipv4.conf.default.rp_filter = 1" /etc/sysctl.conf
    93. add_config_tofile "net.ipv4.conf.all.rp_filter = 1" /etc/sysctl.conf
    94. #处理无源路由包
    95. add_config_tofile "net.ipv4.conf.all.accept_source_route = 0" /etc/sysctl.conf
    96. add_config_tofile "net.ipv4.conf.default.accept_source_route = 0" /etc/sysctl.conf
    97. #core文件名中添加pid作为扩展名
    98. add_config_tofile "kernel.core_uses_pid = 1" /etc/sysctl.conf
    99. #开启syn洪水攻击保护
    100. add_config_tofile "net.ipv4.tcp_syncookies = 1" /etc/sysctl.conf
    101. #修改消息队列长度
    102. add_config_tofile "kernel.msgmnb = 65536" /etc/sysctl.conf
    103. add_config_tofile "kernel.msgmax = 65536" /etc/sysctl.conf
    104. #修改最大内存共享段大小bytes
    105. add_config_tofile "kernel.shmmax = 68719476736" /etc/sysctl.conf
    106. add_config_tofile "kernel.shmall = 4294967296" /etc/sysctl.conf
    107. #timewait数量默认18000
    108. add_config_tofile "net.ipv4.tcp_max_tw_buckets = 600" /etc/sysctl.conf
    109. add_config_tofile "net.ipv4.tcp_sack = 1" /etc/sysctl.conf
    110. add_config_tofile "net.ipv4.tcp_window_scaling = 1" /etc/sysctl.conf
    111. add_config_tofile "net.ipv4.tcp_rmem = 4096 87380 16777216" /etc/sysctl.conf
    112. add_config_tofile "net.ipv4.tcp_wmem = 4096 65536 16777216" /etc/sysctl.conf
    113. add_config_tofile "net.core.rmem_default = 8388608" /etc/sysctl.conf
    114. add_config_tofile "net.core.wmem_max = 16777216" /etc/sysctl.conf
    115. #未收到客户端确认信息连接请求的最大值
    116. add_config_tofile "net.ipv4.tcp_max_syn_backlog = 262144" /etc/sysctl.conf
    117. #放弃建立连接之前发送的synack包
    118. add_config_tofile "net.ipv4.tcp_syn_retries = 2" /etc/sysctl.conf
    119. #开启重用,允许time—wait socket 重新用语新的tcp连接
    120. add_config_tofile "net.ipv4.tcp_tw_reuse = 1" /etc/sysctl.conf
    121. add_config_tofile "net.ipv4.tcp_fin_timeout = 1" /etc/sysctl.conf
    122. #防止简单的ddos攻击
    123. add_config_tofile "net.ipv4.tcp_max_orphans = 3276800" /etc/sysctl.conf
    124. #启用timewait快速收回
    125. add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
    126. #keeptime启用时tcp发送keepalive消息的频度,默认2h
    127. add_config_tofile "net.ipv4.tcp_keepalive_time = 600" /etc/sysctl.conf
    128. #允许系统打开的端口范围
    129. add_config_tofile "net.ipv4.ip_local_port_range = 1024 65535" /etc/sysctl.conf
    130. #资源回收
    131. add_config_tofile "net.ipv4.tcp_tw_recycle = 0" /etc/sysctl.conf
    132. #路由转发
    133. add_config_tofile "net.ipv4.ip_forward = 1" /etc/sysctl.conf
    134. #修改防火墙连接跟踪表大小,默认65535
    135. add_config_tofile "net.netfilter.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
    136. add_config_tofile "net.nf_conntrack_max = ${nf_conntrack_size}" /etc/sysctl.conf
    137. #解禁ping
    138. add_config_tofile "net.ipv4.icmp_echo_ignore_all = 0" /etc/sysctl.conf
    139. modprobe bridge
    140. sysctl -p > /dev/null 2>&1
    141. show_result $? "${item}"
    142. }
    143. #启动 nginx
    144. function nginx_start(){
    145. local item="Nginx start"
    146. systemctl enable nginx --now > /dev/null 2>&1
    147. show_result $? "${item}"
    148. }
    149. #负责写入配置的函数
    150. function add_config_tofile(){
    151. local keywords=`echo $1| awk -F "[= ]+" '{print $1}'`
    152. local SearchResult=`grep "^${keywords}" "$2"`
    153. if [ -z "${SearchResult}" ]
    154. then
    155. echo $1 >> $2
    156. else
    157. sed -i "s/^${keywords}.*/$1/" $2
    158. fi
    159. }
    160. #主函数
    161. function main(){
    162. user_create
    163. nginx_pkg
    164. nginx_download
    165. nginx_compile
    166. nginx_softlink
    167. nginx_service
    168. nginx_kernel
    169. nginx_start
    170. }
    171. main

    三、Tomcat应用程序调优


    关于 Tomcat 主配置文件 server.xml 里面很多默认的配置项,但并不能满足业务需求, 常用的优化相关参数如下

    1. 【maxThreads】Tomcat 使用线程来处理接收的每个请求,这个值表示 Tomcat 可创建的最 大的线程数,默认值是 200
    2. 【minSpareThreads】最小空闲线程数,Tomcat 启动时的初始化的线程数,表示即使没有 人使用也开这么多空线程等待,默认值是 10
    3. 【maxSpareThreads】最大备用线程数,一旦创建的线程超过这个值,Tomcat 就会关闭不 再需要的 socket 线程。默认值是-1(无限制)。一般不需要指定
    4. 【URIEncoding】指定 Tomcat 容器的 URL 编码格式,语言编码格式这块倒不如其它 Web 服务器软件配置方便,需要分别指定utf-8
    5. 【connnectionTimeout】网络连接超时,单位:毫秒,设置为 0 表示永不超时,这样设置 有隐患的。通常默认 20000 毫秒(20秒)就可以
    6. 【enableLookups】是否反查域名,以返回远程主机的主机名,取值为:truefalse, 如果设置为 false,则直接返回 IP 地址,为了提高处理能力,应设置为 false
    7. 【disableUploadTimeout】上传时是否使用超时机制。应设置为 true
    8. 【connectionUploadTimeout】上传超时时间,毕竟文件上传可能需要消耗更多的时间, 这个根据你自己的业务需要自己调,以使 Servlet 有较长的时间来完成它的执行,需要 与上一个参数一起配合使用才会生效
    9. 【acceptCount】指定当所有可以使用的处理请求的线程数都被使用时,可传入连接请求 的最大队列长度,超过这个数的请求将不予处理,默认为 100 个。
    10. 【compression】是否对响应的数据进行 GZIP 压缩,off:表示禁止压缩;on:表示允许 压缩(文本将被压缩)、force:表示所有情况下都进行压缩,默认值为 off,压缩数据 后可以有效的减少页面的大小,一般可以减小 1/3 左右,节省带宽。
    11. 【compressionMinSize】表示压缩响应的最小值,只有当响应报文大小大于这个值的时候 才会对报文进行压缩,如果开启了压缩功能,默认值就是 2048
    12. 【compressableMimeType】压缩类型,指定对哪些类型的文件进行数据压缩。
    13. 【noCompressionUserAgents="gozilla, traviata"】对于以下的浏览器,不启用压缩

    如果已经对代码进行了动静分离,静态页面和图片等数据就不需要 Tomcat 处理了,那 么也就不需要在 Tomcat 中配置压缩了。因为这里只有一台 Tomcat 服务器,而且压测的是 Tomcat 首页,会有图片和静态资源文件,所以这里启用压缩。

    以上是一些常用的配置参数,还有好多其它的参数设置,还可以继续深入的优化

    HTTP Connector 与 AJP Connector 的参数属性值,可以参考官方文档的详细说明进行学习。链 接 地 址 Apache Tomcat 9 Configuration Reference (9.0.86) - The HTTP Connector 

    下 面 开 始 对 Tomcat 配置文件优化进行前后的对比

    1. <Connector port="8080" protocol="HTTP/11.1"
    2. connectionTimeout="20000"
    3. redirectPort="8443"
    4. minSpareThreads="50"
    5. enableLookups="false"
    6. disableUploadTimeout="true"
    7. acceptCount="300"
    8. maxThreads="500"
    9. processorCache="500"
    10. URIEncoding="UTF-8"
    11. compression="on"
    12. compressionMinSize="2048"
    13. compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain,image/gif,image /jpg,image/png"/>

  • 相关阅读:
    “现代”“修饰”卷积神经网络,何谓现代
    云架构师学习------云存储白皮书深入理解
    143、锐捷交换机恢复出厂和各种基本配置
    字符驱动开发
    使用CMakeLists.txt简化项目构建过程
    差分代码模板
    selenium-语法
    计算机毕业设计JavaWeb商铺租赁管理系统(源码+系统+mysql数据库+lw文档)
    【LeetCode】LeetCode 106.从中序与后序遍历序列构造二叉树
    测试技能提升篇——了解Tomcat架构需要知道的事儿!
  • 原文地址:https://blog.csdn.net/zzzxxx520369/article/details/136414892