|
| 1 | +# Nginx 高并发系统内核优化 |
| 2 | +## socket 优化 |
| 3 | +#### Nginx 优化 |
| 4 | ++ 子进程允许打开的连接数:`worker_connections` |
| 5 | +#### 系统内核 优化 |
| 6 | ++ [内核参数的优化](http://blog.csdn.net/moxiaomomo/article/details/19442737) |
| 7 | ++ 实践优化配置 |
| 8 | + + 编辑: `vim /etc/sysctl.conf` |
| 9 | + + 配置结果 |
| 10 | + ```bash |
| 11 | + net.ipv4.tcp_max_tw_buckets = 6000 |
| 12 | + net.ipv4.ip_local_port_range = 1024 65000 |
| 13 | + net.ipv4.tcp_tw_recycle = 1 |
| 14 | + net.ipv4.tcp_tw_reuse = 1 |
| 15 | + net.ipv4.tcp_syncookies = 1 |
| 16 | + net.core.somaxconn = 262144 |
| 17 | + net.core.netdev_max_backlog = 262144 |
| 18 | + net.ipv4.tcp_max_orphans = 262144 |
| 19 | + net.ipv4.tcp_max_syn_backlog = 262144 |
| 20 | + net.ipv4.tcp_syn_retries = 1 |
| 21 | + net.ipv4.tcp_fin_timeout = 1 |
| 22 | + net.ipv4.tcp_keepalive_time = 30 |
| 23 | + ``` |
| 24 | + + 执行命令使之生效:`/sbin/sysctl -p` |
| 25 | +## 文件 优化 |
| 26 | +#### Nginx 优化 |
| 27 | ++ 指当一个nginx进程打开的最多文件描述符数目:`worker_rlimit_nofile 100000;` |
| 28 | +#### 系统内核 优化 |
| 29 | ++ 系统限制其最大进程数:`ulimit -n` |
| 30 | ++ 编辑文件:`/etc/security/limits.conf` |
| 31 | + ```conf |
| 32 | + # End of file |
| 33 | + root soft nofile 65535 |
| 34 | + root hard nofile 65535 |
| 35 | + * soft nofile 65535 |
| 36 | + * hard nofile 65535 |
| 37 | + ``` |
| 38 | +## 优化配置文件 |
| 39 | ++ nginx优化配置文件 |
| 40 | + ```lua |
| 41 | + user www www; |
| 42 | + worker_processes 8; |
| 43 | + worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000; |
| 44 | + error_log /www/log/nginx_error.log crit; |
| 45 | + pid /usr/local/nginx/nginx.pid; |
| 46 | + worker_rlimit_nofile 204800; |
| 47 | + |
| 48 | + events |
| 49 | + { |
| 50 | + use epoll; |
| 51 | + worker_connections 204800; |
| 52 | + } |
| 53 | + ``` |
| 54 | ++ 完整的内核优化配置 |
| 55 | + ```lua |
| 56 | + net.ipv4.tcp_max_tw_buckets = 6000 |
| 57 | + net.ipv4.ip_local_port_range = 1024 65000 |
| 58 | + net.ipv4.tcp_tw_recycle = 1 |
| 59 | + net.ipv4.tcp_tw_reuse = 1 |
| 60 | + net.ipv4.tcp_syncookies = 1 |
| 61 | + net.core.somaxconn = 262144 |
| 62 | + net.core.netdev_max_backlog = 262144 |
| 63 | + net.ipv4.tcp_max_orphans = 262144 |
| 64 | + net.ipv4.tcp_max_syn_backlog = 262144 |
| 65 | + net.ipv4.tcp_syn_retries = 1 |
| 66 | + net.ipv4.tcp_fin_timeout = 1 |
| 67 | + net.ipv4.tcp_keepalive_time = 30 |
| 68 | + ``` |
0 commit comments