Nginx高并发配置思路(轻松应对1万并发量)

测试机器为腾讯云服务器1核1G内存,swap分区2G,停用除SSH外的所有服务,仅保留nginx,优化思路主要包括两个层面:系统层面+nginx层面。

一、系统层面

1、调整同时打开文件数量

ulimit -n 20480

2、TCP最大连接数(somaxconn)

echo 10000 > /proc/sys/net/core/somaxconn

3、TCP连接立即回收、回用(recycle、reuse)

echo 1 > /proc/sys/net/ipv4/tcp_tw_reuseecho 1 > /proc/sys/net/ipv4/tcp_tw_recycle

4、不做TCP洪水抵御

echo 0 > /proc/sys/net/ipv4/tcp_syncookies

也可以直接使用优化后的配置,在/etc/sysctl.conf中加入:

net.core.somaxconn = 20480
net.core.rmem_default = 262144
net.core.wmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 4096 16777216
net.ipv4.tcp_wmem = 4096 4096 16777216
net.ipv4.tcp_mem = 786432 2097152 3145728
net.ipv4.tcp_max_syn_backlog = 16384
net.core.netdev_max_backlog = 20000
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_max_orphans = 131072
net.ipv4.tcp_syncookies = 0

使用:sysctl -p 生效

sysctl -p

二、nginx层面

修改nginx配置文件,nginx.conf

增加work_rlimit_nofile和worker_connections数量,并禁用keepalive_timeout。

worker_processes  1;
worker_rlimit_nofile 20000;

events {
    use epoll;
    worker_connections 20000;
    multi_accept on;
}

http {
  keepalive_timeout 0;
}

重启nginx

/usr/local/nginx/sbin/nginx -s reload

使用ab压力测试

ab -c 10000 -n 150000 http://127.0.0.1/index.html

测试结果:

This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 15000 requests
Completed 30000 requests
Completed 45000 requests
Completed 60000 requests
Completed 75000 requests
Completed 90000 requests
Completed 105000 requests
Completed 120000 requests
Completed 135000 requests
Completed 150000 requests
Finished 150000 requests


Server Software:        nginx/1.8.0
Server Hostname:        127.0.0.1
Server Port:            80

Document Path:          /index.html
Document Length:        612 bytes

Concurrency Level:      10000
Time taken for tests:   19.185 seconds
Complete requests:      150000
Failed requests:        0
Write errors:           0
Total transferred:      131180388 bytes
HTML transferred:       95121324 bytes
Requests per second:    7818.53 [#/sec] (mean)
Time per request:       1279.013 [ms] (mean)
Time per request:       0.128 [ms] (mean, across all concurrent requests)
Transfer rate:          6677.33 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  650 547.9    522    7427
Processing:   212  519 157.4    496     958
Waiting:        0  404 139.7    380     845
Total:        259 1168 572.1   1066    7961

Percentage of the requests served within a certain time (ms)
  50%   1066
  66%   1236
  75%   1295
  80%   1320
  90%   1855
  95%   2079
  98%   2264
  99%   2318
 100%   7961 (longest request)

原文发布于微信公众号 - nginx(nginx-study)

原文发表时间:2018-04-28

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Kubernetes

Kubernetes Nginx Ingress Controller源码分析之创建篇

main controllers/nginx/pkg/cmd/controller/main.go:29 func main() { // start a ...

8587
来自专栏IT探索

linux下源码安装mysql

问题:1. You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

922
来自专栏JAVA烂猪皮

Netty原理分析

Netty是一个高性能、异步事件驱动的NIO框架,基于JAVA NIO提供的API实现。它提供了对TCP、UDP和文件传输的支持,作为一个异步NIO框架,Net...

2122
来自专栏java闲聊

Netty入门(一)

在文章开始之前首先明确一个问题,为什么要使用Netty,Netty解决了什么问题,围绕着这个问题我们开始本篇文章的学习

1532
来自专栏IT进修之路

原 荐 SpringBoot整合mybati

2014
来自专栏haifeiWu与他朋友们的专栏

Netty实战之第一个应用

作为一个正在Java路上摸爬滚打的小菜鸡,之前在项目中也用过Netty,也因为Netty报名阿里的中间件大赛,但终究功力太浅,最终不了了之,最近工作中又遇到了N...

1852
来自专栏AI-vell

java轻量RESTful api服务搭建(jersey+jetty)

由于开始要搭建一个java + python的服务,java端提供数据库增删改查逻辑供python端调用,第一时间想到了用REST(Representation...

5227
来自专栏乐沙弥的世界

publickey,gssapi-with-mic,Unspecified GSS failure

        最近的MHA配置时碰到了Permission denied (publickey,gssapi-with-mic,password)这个错误提示...

1472
来自专栏编程思想之路

Android6.0锁屏源码分析之界面布局分析

大致先介绍一下锁屏界面 Android的锁屏界面可以分为两级, 一级锁屏界面暂且称之为锁屏界面LockScreen,即平常用到的无需任何输入和验证,只需滑动解锁...

3958
来自专栏Ryan Miao

SpringCloud学习5-如何创建一个服务提供者provider

1353

扫码关注云+社区

领取腾讯云代金券