Nginx高并发简单配置

摘要:
https:worker_connections 20000;多重接受;

https://www.cnblogs.com/sunjianguo/p/8298283.html

停用除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_reuse

echo 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高并发简单配置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇基于Windows系统搭建一个MQTT Brokerwin10安装elementary os双系统下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

编译安装openresty

选择版本 Openresty的版本号比较简单,形式为:Openresty 1.2.3.x 例如Openresty 1.17.8.2表示是基于Nginx 1.17.8开发的第二个版本 那么选择的标准就是根据自己的需要,对应nginx的版本来下载 预编译包安装(可跳过) 这里以centos为例,简单带过一下,根据官方的要求,Red Hat类的系统支持的Open...

code-server nginx 反向代理

这里根据官网所给出的配置项进行了简单修改,仅作参考 关于nginx配置项有不了解的可以看这里 反向代理配置内容 location / { proxy_pass http://127.0.0.1:8082; proxy_set_header Host $host; proxy_set_header Upgrade $http_upgrade;...

在windows上使用nginx重定向目录访问远程服务器文件详细实例

为了在开发环境保持于生产环境相同的访问远程服务器文件资源的目录配置,需要在开发环境(windows)在远程文件服务器使用nginx重定向文件目录,因为网上的资料大都是copy的,解释比较笼统,也没有具体的例子,就花了蛮长时间才实现,所以自己写一篇记录一下,之后忘了可以更方便的温故,也分享给大家,让大家即使没有使用过nginx也能更加简单方便的快速学会使用。...

__attribute__的一些相关属性

__attribute__((format()))  这个format有3个参数。 int my(NSString *str,NSString *str1,NSArray*str2,...) __attribute__((format(__NSString__,2,4)));   三个参数告诉编译器,第二个参数必须是NSString类型,且可变参数从第4位...

Nginx中文手冊

下载 : Nginx 中文手冊Nginx 常见应用技术指南[Nginx Tips] 第二版 作者:NetSeek http://www.linuxtone.org (IT运维专家网|集群架构|性能调优) 目 录 一、 Nginx 基础知识 二、 Nginx 安装及调试 三、 Nginx Rewrite 四、 Nginx Redirect 五、 Nginx...

Nginx超时配置

Nginx超时配置1.client_header_timeout语法client_header_timeout time 默认值60s 上下文http server 说明指定等待client发送一个请求头的超时时间(例如:GET/HTTP/1.1).仅当在一次read中,没有收到请求头,才会算超时。如果在超时时间内,client没发送任何东西,nginx返...