这篇文章是《打造3百万次请求/秒的高性能服务器集群》系列的第2部分,在这个部分中你可以使用任何一种 WEB 服务器,不过我决定使用 Nginx,因其轻量级、高可靠及高性能的优点。

通常来说,一个优化良好的 Nginx Linux 服务器可以达到 500,000 – 600,000 次/秒 的请求处理性能,然而我的 Nginx 服务器可以稳定地达到 904,000 次/秒 的处理性能,并且我以此高负载测试超过 12 小时,服务器工作稳定。

这里需要特别说明的是,本文中所有列出来的配置都是在我的测试环境验证的,而你需要根据你服务器的情况进行配置:

EPEL 源安装 Nginx:

yum -y install nginx

备份配置文件,然后根据你的需要进行配置:

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
vim /etc/nginx/nginx.conf
# This number should be, at maximum, the number of CPU cores on your system.
# (since nginx doesn't benefit from more than one worker per CPU.)
# 这里的数值不能超过 CPU 的总核数,因为在单个核上部署超过 个 Nginx 服务进程并不起到提高性能的作用。
worker_processes ; # Number of file descriptors used for Nginx. This is set in the OS with 'ulimit -n 200000'
# or using /etc/security/limits.conf
# Nginx 最大可用文件描述符数量,同时需要配置操作系统的 "ulimit -n 200000",或者在 /etc/security/limits.conf 中配置。
worker_rlimit_nofile ; # only log critical errors
# 只记录 critical 级别的错误日志
error_log /var/log/nginx/error.log crit # Determines how many clients will be served by each worker process.
# (Max clients = worker_connections * worker_processes)
# "Max clients" is also limited by the number of socket connections available on the system (~64k)
# 配置单个 Nginx 单个进程可服务的客户端数量,(最大值客户端数 = 单进程连接数 * 进程数 )
# 最大客户端数同时也受操作系统 socket 连接数的影响(最大 64K )
worker_connections ; # essential for linux, optmized to serve many clients with each thread
# Linux 关键配置,允许单个线程处理多个客户端请求。
use epoll; # Accept as many connections as possible, after nginx gets notification about a new connection.
# May flood worker_connections, if that option is set too low.
# 允许尽可能地处理更多的连接数,如果 worker_connections 配置太低,会产生大量的无效连接请求。
multi_accept on; # Caches information about open FDs, freqently accessed files.
# Changing this setting, in my environment, brought performance up from 560k req/sec, to 904k req/sec.
# I recommend using some varient of these options, though not the specific values listed below.
# 缓存高频操作文件的FDs(文件描述符/文件句柄)
# 在我的设备环境中,通过修改以下配置,性能从 560k 请求/秒 提升到 904k 请求/秒。
# 我建议你对以下配置尝试不同的组合,而不是直接使用这几个数据。
open_file_cache max= inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses ;
open_file_cache_errors on; # Buffer log writes to speed up IO, or disable them altogether
# 将日志写入高速 IO 存储设备,或者直接关闭日志。
# access_log /var/log/nginx/access.log main buffer=16k;
access_log off; # Sendfile copies data between one FD and other from within the kernel.
# More efficient than read() + write(), since the requires transferring data to and from the user space.
# 开启 sendfile 选项,使用内核的 FD 文件传输功能,这个比在用户态用 read() + write() 的方式更加高效。
sendfile on; # Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet,
# instead of using partial frames. This is useful for prepending headers before calling sendfile,
# or for throughput optimization.
# 打开 tcp_nopush 选项,Nginux 允许将 HTTP 应答首部与数据内容在同一个报文中发出。
# 这个选项使服务器在 sendfile 时可以提前准备 HTTP 首部,能够达到优化吞吐的效果。
tcp_nopush on; # don't buffer data-sends (disable Nagle algorithm). Good for sending frequent small bursts of data in real time.
# 不要缓存 data-sends (关闭 Nagle 算法),这个能够提高高频发送小数据报文的实时性。
tcp_nodelay on; # Timeout for keep-alive connections. Server will close connections after this time.
# 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接。
keepalive_timeout ; # Number of requests a client can make over the keep-alive connection. This is set high for testing.
# 单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。
keepalive_requests ; # allow the server to close the connection after a client stops responding. Frees up socket-associated memory.
# 允许服务器在客户端停止发送应答之后关闭连接,以便释放连接相应的 socket 内存开销。
reset_timedout_connection on; # send the client a "request timed out" if the body is not loaded by this time. Default .
# 配置客户端数据请求超时时间,默认是 秒。
client_body_timeout ; # If the client stops reading data, free up the stale client connection after this much time. Default .
# 客户端数据读超时配置,客户端停止读取数据,超时时间后断开相应连接,默认是 秒。
send_timeout ; # Compression. Reduces the amount of data that needs to be transferred over the network
# 压缩参数配置,减少在网络上所传输的数据量。
gzip on;
gzip_min_length ;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6].";

启动 Nginx 并配置起机自动加载。

service nginx start
chkconfig nginx on

配置 Tsung 并启动测试,测试差不多 10 分钟左右就能测试到服务器的峰值能力,具体的时间与你的 Tsung 配置相关。

tsung start

你觉得测试结果已经够了的情况下,通过 ctrl+c 退出,之后使用我们之前配置的别名命令 treport 查看测试报告。

WEB 服务器调优,第二部分:TCP 协议栈调优

这个部分不只是对 Ngiinx 适用,还可以在任何 WEB 服务器上使用。通过对内核 TCP 配置的优化可以提高服务器网络带宽。

以下配置在我的 10-Gbase-T 服务器上工作得非常完美,服务器从默认配置下的 8Gbps 带宽提升到 9.3Gbps。

当然,你的服务器上的结论可能不尽相同。

下面的配置项,我建议每次只修订其中一项,之后用网络性能测试工具 netperf、iperf 或是用我类似的测试脚本 cluster-netbench.pl 对服务器进行多次测试。

yum -y install netperf iperf
vim /etc/sysctl.conf
# Increase system IP port limits to allow for more connections
# 调高系统的 IP 以及端口数据限制,从可以接受更多的连接
net.ipv4.ip_local_port_range = net.ipv4.tcp_window_scaling = # number of packets to keep in backlog before the kernel starts dropping them
# 设置协议栈可以缓存的报文数阀值,超过阀值的报文将被内核丢弃
net.ipv4.tcp_max_syn_backlog = # increase socket listen backlog
# 调高 socket 侦听数阀值
net.core.somaxconn =
net.ipv4.tcp_max_tw_buckets = # Increase TCP buffer sizes
# 调大 TCP 存储大小
net.core.rmem_default =
net.core.rmem_max =
net.core.wmem_max =
net.ipv4.tcp_rmem =
net.ipv4.tcp_wmem =
net.ipv4.tcp_congestion_control = cubic

每次修订配置之后都需要执行以下命令使之生效.

sysctl -p /etc/sysctl.conf

别忘了在配置修订之后务必要进行网络 benchmark 测试,这样可以观测到具体是哪个配置修订的优化效果最明显。通过这种有效测试方法可以为你节省大量时间。

转自:http://blog.jobbole.com/87531/

每秒处理3百万请求的Web集群搭建-为最佳性能调优 Nginx的更多相关文章

  1. 每秒处理3百万请求的Web集群搭建-用 LVS 搭建一个负载均衡集群

    这篇文章是<打造3百万次请求/秒的高性能服务器集群>系列的第3部分,有关于性能测试工具以及优化WEB服务器部分的内容请参看以前的文章. 本文基于你已经优化好服务器以及网络协议栈的基础之上, ...

  2. 每秒处理3百万请求的Web集群搭建-如何生成每秒百万级别的 HTTP 请求?

    本文是构建能够每秒处理 3 百万请求的高性能 Web 集群系列文章的第一篇.它记录了我使用负载生成器工具的一些经历,希望它能帮助每一个像我一样不得不使用这些工具的人节省时间. 负载生成器是一些生成用于 ...

  3. Apache tomcat高可用web集群搭建过程配置记录

    说明,本文仅作为个人搭建配置保存,问题处理没有一一列出,过程也未见详尽,有问题的朋友可以直接留言给我,会一一回复,谢谢. 小目标: 支持故障转移(或主备,扩展性不佳),保证故障转移后,对前端用户透明, ...

  4. 使用ARM和VMSS创建自动扩展的web集群

    在很多的商业场景中,用户的访问,峰值时间都是很难预测的,尤其是做一些市场推广活动和促销的时候,到底部署什么规模的web集群合适,这一直是个问题,部署过量会造成高成本和资源不必要的浪费,部署过少,如果到 ...

  5. haproxy+keepalived实现web集群高可用性[转]

    负载均衡集群的概念 负载均衡是设计分布式系统架构必须要考虑的因素之一,它指的是通过调度分发的方式尽可能将“请求”.“访问”的压力负载平均分摊到集群中的各个节点,避免有些节点负载太高导致访问延迟,而有些 ...

  6. Keepalived+Nginx+Tomcat 实现高可用Web集群

    https://www.jianshu.com/p/bc34f9101c5e Keepalived+Nginx+Tomcat 实现高可用Web集群 0.3912018.01.08 20:28:59字数 ...

  7. WEB 集群与负载均衡(一)基本概念-上

    Web集群是由多个同时运行同一个web应用的服务器组成,在外界看来就像一个服务器一样,这多台服务器共同来为客户提供更高性能的服务.集群更标准的定义是:一组相互独立的服务器在网络中表现为单一的系统,并以 ...

  8. 阿里云小规模web集群分享(电商)

    计算基础资源使用阿里云ECS.OSS.RDS.mysql中间件.CDN 原则是尽量少改动代码来实现web集群 1.负载均衡器: a)负责处理所有请求 b)http动态请求分配到后端web服务器 c)维 ...

  9. 基于RHCS的web双机热备集群搭建

    基于RHCS的web双机热备集群搭建 RHCS集群执行原理及功能介绍 1. 分布式集群管理器(CMAN)  Cluster Manager.简称CMAN.是一个分布式集群管理工具.它执行在集群的各个节 ...

随机推荐

  1. 人脸对齐SDM原理----Supervised Descent Method and its Applications to Face Alignment

    最近组里研究了SDM算法在人脸对齐中的应用,是CMU的论文<Supervised Descent Method and its Applications to Face Alignment> ...

  2. hibernate 结合servlet及 jsp 的使用

    Hibernate结合JSP使用 前面几章详细的介绍了Hibernate的相关知识,这一章介绍Hibernate结合JSP和Servlet的使用,通过这一章的学习,可以加深对Hibernate的理解. ...

  3. Codeforces Round #247 (Div. 2) ABC

    Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431  代码均已投放:https://github.com/illuz/Wa ...

  4. 按字母顺序排列的IDC函数列表

    http://www.2cto.com/shouce/ida/162.htm 按字母顺序排列的IDC函数列表 下面是函数描述信息中的约定: 'ea' 线性地址 'success' 0表示函数失败:反之 ...

  5. 用css让一个容器水平垂直居中

    阅读目录 方法一:position加margin 方法二: diaplay:table-cell 方法三:position加 transform 方法四:flex;align-items: cente ...

  6. dao层知识点总结

    1.dao层要有connection 2.dao层进行分页,mysql limit关键字 3.dao层进行结果集转换为java bean 4.dao层queryforlist

  7. crm使用soap分配记录

    //样例 function demo() {     //操作记录的id     var targetId = "A8A46444-BA10-E411-8A04-00155D002F02&q ...

  8. u3d移动游戏优化规范

    1.顶点性能一般来说,如果您想在iPhone 3GS或更新的设备上每帧渲染不超过40,000可见点,那么对于一些配备 MBX GPU的旧设备(比如,原始的 iPhone,如 iPhone 3g和 iP ...

  9. 深度学习Deep learning

    In the last chapter we learned that deep neural networks are often much harder to train than shallow ...

  10. react组件引用时的default常见错误

    1.下面使用方法是正确的: export class StepLoad extends React.Component {} 引用上面插件的方法,重命名 import {StepLoad as Ste ...