|
1 | 1 | # Nginx 高并发系统内核优化
|
2 | 2 | ## socket 优化
|
3 |
| -#### Nginx 优化 |
| 3 | +#### Nginx |
4 | 4 | + 子进程允许打开的连接数:`worker_connections`
|
5 |
| -#### 系统内核 优化 |
| 5 | +#### 系统内核 |
6 | 6 | + [内核参数的优化](http://blog.csdn.net/moxiaomomo/article/details/19442737)
|
7 | 7 | + 实践优化配置
|
8 | 8 | + 编辑: `vim /etc/sysctl.conf`
|
|
22 | 22 | net.ipv4.tcp_keepalive_time = 30
|
23 | 23 | ```
|
24 | 24 | + 执行命令使之生效:`/sbin/sysctl -p`
|
25 |
| -## 文件 优化 |
26 |
| -#### Nginx 优化 |
| 25 | +## 文件优化 |
| 26 | +#### Nginx |
27 | 27 | + 指当一个nginx进程打开的最多文件描述符数目:`worker_rlimit_nofile 100000;`
|
28 |
| -#### 系统内核 优化 |
| 28 | +#### 系统内核 |
29 | 29 | + 系统限制其最大进程数:`ulimit -n`
|
30 | 30 | + 编辑文件:`/etc/security/limits.conf`
|
31 | 31 | ```conf
|
|
35 | 35 | * soft nofile 65535
|
36 | 36 | * hard nofile 65535
|
37 | 37 | ```
|
| 38 | +## PHP-FPM优化 |
| 39 | ++ rlimit_files |
| 40 | ++ rlimit_files |
| 41 | ++ rlimit_files |
38 | 42 | ## 优化配置文件
|
39 | 43 | + nginx优化配置文件
|
40 | 44 | ```lua
|
|
50 | 54 | use epoll;
|
51 | 55 | worker_connections 204800;
|
52 | 56 | }
|
| 57 | + |
| 58 | + http |
| 59 | + { |
| 60 | + include mime.types; |
| 61 | + default_type application/octet-stream; |
| 62 | + |
| 63 | + charset utf-8; |
| 64 | + |
| 65 | + server_names_hash_bucket_size 128; |
| 66 | + client_header_buffer_size 2k; |
| 67 | + large_client_header_buffers 4 4k; |
| 68 | + client_max_body_size 8m; |
| 69 | + |
| 70 | + sendfile on; |
| 71 | + tcp_nopush on; |
| 72 | + |
| 73 | + keepalive_timeout 60; |
| 74 | + |
| 75 | + fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 |
| 76 | + keys_zone=TEST:10m |
| 77 | + inactive=5m; |
| 78 | + fastcgi_connect_timeout 300; |
| 79 | + fastcgi_send_timeout 300; |
| 80 | + fastcgi_read_timeout 300; |
| 81 | + fastcgi_buffer_size 64k; |
| 82 | + fastcgi_buffers 8 64k; |
| 83 | + fastcgi_busy_buffers_size 128k; |
| 84 | + fastcgi_temp_file_write_size 128k; |
| 85 | + fastcgi_cache TEST; |
| 86 | + fastcgi_cache_valid 200 302 1h; |
| 87 | + fastcgi_cache_valid 301 1d; |
| 88 | + fastcgi_cache_valid any 1m; |
| 89 | + fastcgi_cache_min_uses 1; |
| 90 | + fastcgi_cache_use_stale error timeout invalid_header http_500; |
| 91 | + |
| 92 | + open_file_cache max=204800 inactive=20s; |
| 93 | + open_file_cache_min_uses 1; |
| 94 | + open_file_cache_valid 30s; |
| 95 | + tcp_nodelay on; |
| 96 | + |
| 97 | + gzip on; |
| 98 | + gzip_min_length 1k; |
| 99 | + gzip_buffers 4 16k; |
| 100 | + gzip_http_version 1.0; |
| 101 | + gzip_comp_level 2; |
| 102 | + gzip_types text/plain application/x-javascript text/css application/xml; |
| 103 | + gzip_vary on; |
| 104 | + |
| 105 | + server |
| 106 | + { |
| 107 | + listen 8080; |
| 108 | + server_name backup.aiju.com; |
| 109 | + index index.php index.htm; |
| 110 | + root /www/html/; #这里的位置很重要,不要写在其它指令里面,我曾经就调试了好久才发现这个问题的 |
| 111 | + |
| 112 | + location /status |
| 113 | + { |
| 114 | + stub_status on; |
| 115 | + } |
| 116 | + |
| 117 | + location ~ .*\.(php|php5)?$ |
| 118 | + { |
| 119 | + fastcgi_pass 127.0.0.1:9000; |
| 120 | + fastcgi_index index.php; |
| 121 | + include fcgi.conf; |
| 122 | + } |
| 123 | + |
| 124 | + location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ |
| 125 | + { |
| 126 | + expires 30d; |
| 127 | + } |
| 128 | + |
| 129 | + log_format access '$remote_addr - $remote_user [$time_local] "$request" ' |
| 130 | + '$status $body_bytes_sent "$http_referer" ' |
| 131 | + '"$http_user_agent" $http_x_forwarded_for'; |
| 132 | + access_log /www/log/access.log access; |
| 133 | + } |
| 134 | + } |
53 | 135 | ```
|
54 | 136 | + 完整的内核优化配置
|
55 | 137 | ```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 |
| 138 | + net.ipv4.ip_forward = 0 |
| 139 | + net.ipv4.conf.default.rp_filter = 1 |
| 140 | + net.ipv4.conf.default.accept_source_route = 0 |
| 141 | + kernel.sysrq = 0 |
| 142 | + kernel.core_uses_pid = 1 |
60 | 143 | net.ipv4.tcp_syncookies = 1
|
61 |
| - net.core.somaxconn = 262144 |
| 144 | + kernel.msgmnb = 65536 |
| 145 | + kernel.msgmax = 65536 |
| 146 | + kernel.shmmax = 68719476736 |
| 147 | + kernel.shmall = 4294967296 |
| 148 | + net.ipv4.tcp_max_tw_buckets = 6000 |
| 149 | + net.ipv4.tcp_sack = 1 |
| 150 | + net.ipv4.tcp_window_scaling = 1 |
| 151 | + net.ipv4.tcp_rmem = 4096 87380 4194304 |
| 152 | + net.ipv4.tcp_wmem = 4096 16384 4194304 |
| 153 | + net.core.wmem_default = 8388608 |
| 154 | + net.core.rmem_default = 8388608 |
| 155 | + net.core.rmem_max = 16777216 |
| 156 | + net.core.wmem_max = 16777216 |
62 | 157 | net.core.netdev_max_backlog = 262144
|
63 |
| - net.ipv4.tcp_max_orphans = 262144 |
| 158 | + net.core.somaxconn = 262144 |
| 159 | + net.ipv4.tcp_max_orphans = 3276800 |
64 | 160 | net.ipv4.tcp_max_syn_backlog = 262144
|
| 161 | + net.ipv4.tcp_timestamps = 0 |
| 162 | + net.ipv4.tcp_synack_retries = 1 |
65 | 163 | net.ipv4.tcp_syn_retries = 1
|
| 164 | + net.ipv4.tcp_tw_recycle = 1 |
| 165 | + net.ipv4.tcp_tw_reuse = 1 |
| 166 | + net.ipv4.tcp_mem = 94500000 915000000 927000000 |
66 | 167 | net.ipv4.tcp_fin_timeout = 1
|
67 | 168 | net.ipv4.tcp_keepalive_time = 30
|
| 169 | + net.ipv4.ip_local_port_range = 1024 65000 |
68 | 170 | ```
|
0 commit comments