nginx优化

[root@localhost  soft]# wget http://nginx.org/download/nginx-1.0.15.tar.gz
[root@localhost  nginx-1.0.15]# ./configure --prefix=/usr/local/nginx

[root@localhost  soft]# yum  install httpd-tools
[root@localhost  soft]# ab -V
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/
[root@localhost  nginx-1.0.15]# ulimit -n
1024
root@localhost nginx-1.0.15]# cd /usr/local/nginx
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# ./sbin/nginx

localhost nginx-1.0.15]# ab -c 1000 -n 50000 http://127.0.0.1/index.html         1000并发50000此
ompleted 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


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

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

Concurrency Level:      1000
Time taken for tests:   12.241 seconds
Complete requests:      50000
Failed requests:        2621
   (Connect: 0, Receive: 0, Length: 2621, Exceptions: 0)
Write errors:           0
Non-2xx responses:      2621
Total transferred:      18139935 bytes
HTML transferred:       7684393 bytes
Requests per second:    4084.68 [#/sec] (mean)
Time per request:       244.817 [ms] (mean)
Time per request:       0.245 [ms] (mean, across all concurrent requests)
Transfer rate:          1447.19 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  175 493.2     37    3095
Processing:    10   47  31.6     43     666
Waiting:        0   37  29.9     31     659
Total:         17  222 503.7     82    3218

Percentage of the requests served within a certain time (ms)
  50%     82
  66%     97
  75%    118
  80%    126
  90%    189
  95%   1129
  98%   3040
  99%   3123
 100%   3218 (longest request)
ot@localhost nginx]# ulimit -n 20000
安装统计模块
ot@localhost nginx-1.0.15]#  ./configure --prefix=/usr/local/nginx --add-module=/root/soft/ngx_http_consistent_hash-master/ --with-http_stub_status_module
[root@localhost nginx-1.0.15]# vi /usr/local/nginx/conf/nginx.conf
 location /status {
            stub_status on;
            access_log off;
            allow ip;
        }
[root@localhost nginx]# ./sbin/nginx -s reload
 ab -c 2000 -n 50000 http://127.0.0.1/index.html

 [root@localhost nginx]# vi conf/nginx.conf
events {
    worker_connections  10240;
}
1:判断nginx的瓶颈

1.1: 首先把ab测试端的性能提高,使之能高并发的请求.
易出问题: too many open files
原因 :  ab在压力测试时,打开的socket过多
解决: ulimit -n 30000 (重启失效)
观察结果: nginx 不需要特殊优化的情况下, 5000个连接,1秒内响应.
满足要求,但 wating状态的连接过多.

1.2: 解决waiting进程过多的问题.
解决办法: keepalive_timeout = 0;  
即: 请求结果后,不保留tcp连接.
在高并发的情况下, keepalive会占据大量的socket连接.
结果: waiting状态的连接明显减少.

1.3: 解决服务端 too many open files
分析: nginx要响应,
1是要建立socket连接,
2 是要读本地文件
这两个者限制.

root@localhost soft]# more /proc/sys/net/core/somaxconn
128
echo  50000 /proc/sys/net/core/somaxconn #系统抻面socket 优化
系统内核层面:
net.core.somaxconn = 4096 允许等待中的监听
net.ipv4.tcp_tw_recycle = 1  tcp连接快速回收
net.ipv4.tcp_tw_reuse = 1    tcp连接重用   
net.ipv4.tcp_syncookies = 0  不抵御洪水攻击
ulimit -n 30000
Nginx层面:
解决: nginx.conf 下面: work_connection 加大
worker_connections  10240;
Worker_rlimit_nofiles 10000;
Keepalive_timeout 0;

Nginx---->php-fpm之间的优化

如上图,在很多个nginx来访问fpm时, fpm的进程要是不够用, 会生成子进程.

生成子进程需要内核来调度,比较耗时,
如果网站并发比较大,
我们可以用静态方式一次性生成若干子进程,保持在内存中.

方法 -- 修改php-fpm.conf
Pm = static  让fpm进程始终保持,不要动态生成
Pm.max_children= 32  始终保持的子进程数量
 
Php-mysql的优化

Linux机器下 ,php 通过IP连接其他mysql服务器时,容易出的问题
能ping能,但connect不到.
 

一般是由:mysql服务器的防火墙影响的.

并发1万连接,响应时间过长.

优化思路: 同上的nginx
1: 内核层面,加大连接数,并加快tcp回收
2: mysql层面,增大连接数
3: php层面,用长连接,节省连接数
4: 用memcached缓存,减轻mysql负担

具体:
1.1  , PHP服务器增大 ulimint -n选项
1.2 mysql服务器内核配置
添加或修改如下选项
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 0

# syscttl -p 使修改立即生效

2.1  修改mysql.cnf
Vi  /etc/my.conf
# service mysqld restart 重启mysql

3.1 PHP层面 ,用长连接
Mysql_connect ---> mysql_pconnect
注: pconnect 在PHP以apache模块的形式存在时,无效果.
 
Nginx+phjp+mysql+nginx
在引入memcached后,性能提升不明显,甚至还略有下降
memcached使50%的请求变快了,但是一部分,反倒慢了.
原因在于--PHP->memcached也要建立tcp连接,代价挺高,
但缓存了数据之后,就省去了mysql的查询时间.
总结: memcached适合存复杂的sql,尤其是连接查询/模糊查询的sql结果
Memcached服务器的优化(集中在内核的ipv4设置上,不再重复)



你可能感兴趣的:(nginx优化)