I like ubuntu
最近迁移一台服务器,合并了业务,服务器是nginx+php-fmp。TCP TIME_WAIT套接字数量经常达到两万,服务器被拖死。于是查找资料,通过修改Linux内核参数来减少这种情况
这是最开始的情况
于是尝试进行修改
增加以下几行:
说明:
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time = 1200 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 12000 65000 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为12000到65000。
net.ipv4.tcp_max_syn_backlog = 8192 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
net.ipv4.tcp_max_tw_buckets = 5000 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。
执行以下命令使配置生效:
另外,检查代码里是否有file_get_contents,如果有,尽量用curl进行替换,保证不卡死php进程
这是最开始的情况
TIME_WAIT 18939
FIN_WAIT1 2
ESTABLISHED 32
SYN_RECV 3
LAST_ACK 5
FIN_WAIT1 2
ESTABLISHED 32
SYN_RECV 3
LAST_ACK 5
于是尝试进行修改
vi /etc/sysctl.conf
增加以下几行:
引用
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 12000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 12000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
说明:
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout = 30 表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time = 1200 表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 12000 65000 表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为12000到65000。
net.ipv4.tcp_max_syn_backlog = 8192 表示SYN队列的长度,默认为1024,加大队列长度为8192,可以容纳更多等待连接的网络连接数。
net.ipv4.tcp_max_tw_buckets = 5000 表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。
执行以下命令使配置生效:
/sbin/sysctl -p
另外,检查代码里是否有file_get_contents,如果有,尽量用curl进行替换,保证不卡死php进程
在nginx.conf里面加入如下配置即可。。经firebug测试,jquery1.3.2原本55k大小,现在只需要传输19k
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
squid的
与之配合的nginx的
cache_effective_user www-data
cache_effective_group www-data
http_port 127.0.0.1:88 accel vhost vport
cache_peer 60.28.216.8 parent 80 0 no-query originserver forceddomain=www.iciba.com name=webServer1
cache_peer_domain webServer1 iciba.local
acl localhost src 127.0.0.1/255.255.255.255
http_access allow localhost
http_access deny all
cache_mgr luochong@kingsoft.com
cache_dir ufs /data/squid 1000 16 256
cache_mem 128 MB
maximum_object_size_in_memory 512 KB
max_open_disk_fds 0 KB
minimum_object_size 0 KB
maximum_object_size 4096 KB
cache_swap_low 90
cache_swap_high 95
cache_access_log /data/logs/squid/access.log
cache_log /data/logs/squid/cache.log
cache_store_log /data/logs/squid/store.log
visible_hostname No1.proxy
refresh_pattern -i . 1440 50% 2880 ignore-reload
cache_effective_group www-data
http_port 127.0.0.1:88 accel vhost vport
cache_peer 60.28.216.8 parent 80 0 no-query originserver forceddomain=www.iciba.com name=webServer1
cache_peer_domain webServer1 iciba.local
acl localhost src 127.0.0.1/255.255.255.255
http_access allow localhost
http_access deny all
cache_mgr luochong@kingsoft.com
cache_dir ufs /data/squid 1000 16 256
cache_mem 128 MB
maximum_object_size_in_memory 512 KB
max_open_disk_fds 0 KB
minimum_object_size 0 KB
maximum_object_size 4096 KB
cache_swap_low 90
cache_swap_high 95
cache_access_log /data/logs/squid/access.log
cache_log /data/logs/squid/cache.log
cache_store_log /data/logs/squid/store.log
visible_hostname No1.proxy
refresh_pattern -i . 1440 50% 2880 ignore-reload
与之配合的nginx的
upstream iciba_local {
ip_hash;
server 127.0.0.1:88;
}
server {
listen 80;
server_name iciba.local;
#access_log /var/log/nginx/localhost.access.log;
location / {
proxy_pass http://iciba_local;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
ip_hash;
server 127.0.0.1:88;
}
server {
listen 80;
server_name iciba.local;
#access_log /var/log/nginx/localhost.access.log;
location / {
proxy_pass http://iciba_local;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
查找指定文件夹内指定文件类型的文件内容
查看系统连接数
查看所有连接到本机的ip和每ip的连接数量
find . -type f -name '*.php' -exec grep '1950' -l {} \;
查看系统连接数
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
查看所有连接到本机的ip和每ip的连接数量
netstat -na|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -r +0n
本文参照了
http://rashost.com/blog/centos5-vps-nginx-solution2
http://blog.s135.com/nginx_php_v5/
首先还是换源,换源方法参照http://anerg.cn/read.php?33
先下载nginx和已经编译好的php
安装nginx,并加入/etc/init.d/
默认nginx的目录是/usr/share/nginx/html/这个我们一会儿再编辑
现在安装mysql
修改你的mysql密码
安装相关库
修改mysql的库文件,否则在64位系统下老去找32位的mysql库
http://rashost.com/blog/centos5-vps-nginx-solution2
http://blog.s135.com/nginx_php_v5/
首先还是换源,换源方法参照http://anerg.cn/read.php?33
先下载nginx和已经编译好的php
wget http://rashost.com/download/centos5-x86_64/nginx-0.7.61-1.x86_64.rpm
wget http://rashost.com/download/centos5-x86_64/php-fpm-5.2.10-x86_64.tar.gz
wget http://rashost.com/download/centos5-x86_64/php-fpm-5.2.10-x86_64.tar.gz
安装nginx,并加入/etc/init.d/
rpm -ivh nginx-0.7.61-1.x86_64.rpm
chkconfig --list nginx
chkconfig nginx on
/etc/init.d/nginx start
chkconfig --list nginx
chkconfig nginx on
/etc/init.d/nginx start
默认nginx的目录是/usr/share/nginx/html/这个我们一会儿再编辑
现在安装mysql
yum install -y mysql-server
chkconfig --list mysqld
chkconfig mysqld on
/etc/init.d/mysqld start
chkconfig --list mysqld
chkconfig mysqld on
/etc/init.d/mysqld start
修改你的mysql密码
mysqladmin -u root password 'newpassword'
安装相关库
yum install libxml2-devel libmcrypt-devel openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel openldap-devel libmhash-devel mysql-devel libtool-ltdl-devel
修改mysql的库文件,否则在64位系统下老去找32位的mysql库
mv /usr/lib/mysql /usr/lib/mysql.i386
ln -sf /usr/lib64/mysql /usr/lib/mysql
ln -sf /usr/lib64/mysql /usr/lib/mysql
» 阅读全文







