Skip to content


freebsd 7.2 内核优化提高负载

uname -a FreeBSD web_1 7.2-RELEASE FreeBSD 7.2-RELEASE #0: Fri May 1 07:18:07 UTC 2009 [email protected]:/usr/obj/usr/src/sys/GENERIC amd64

nginx 0.7.63作反向代理

worker_processes 16; worker_rlimit_nofile 51200; events { use kqueue; worker_connections 51200; }

#tail /var/log/messages

Nov 25 21:47:11 web_1 kernel: kern.maxfiles limit exceeded by uid 88, please see tuning(7). Nov 25 21:47:12 web_1 kernel: kern.maxfiles limit exceeded by uid 80, please see tuning(7). Nov 25 21:47:44 web_1 last message repeated 27 times Nov 25 21:48:16 web_1 last message repeated 17 times Nov 26 09:04:01 web_1 kernel: Limiting open port RST response from 341 to 200 packets/sec

查看当前内核参数 #sysctl -a

kern.maxfiles: 12328 kern.maxfilesperproc: 11095 net.inet.icmp.icmplim: 200 net.inet.icmp.icmplim_output: 1 kern.ipc.somaxconn:128 net.inet.tcp.blackhole: 0 net.inet.udp.blackhole: 0 net.inet.udp.maxdgram: 9216 net.inet.tcp.sendspace: 32768 net.inet.tcp.recvspace: 65536 kern.ipc.nmbclusters: 25600 vfs.vmiodirenable: 1 net.inet.tcp.msl: 30000 net.inet.tcp.finwait2_timeout: 60000 net.inet.tcp.fast_finwait2_recycle: 0

优化内核,立即生效

#sysctl -w kern.maxfiles=65536 #sysctl -w kern.maxfilesperproc=65536 #sysctl -w net.inet.icmp.icmplim=500 #sysctl -w net.inet.icmp.icmplim_output=0 #sysctl -w kern.ipc.somaxconn=4096 #sysctl -w net.inet.tcp.blackhole=2 #sysctl -w net.inet.udp.blackhole=1 #sysctl -w net.inet.udp.maxdgram=65536 #sysctl -w net.inet.tcp.sendspace=65536 #sysctl -w net.inet.tcp.recvspace=65536 #sysctl -w kern.ipc.nmbclusters=32768 #sysctl -w net.inet.tcp.msl=2500 #sysctl -w net.inet.tcp.finwait2_timeout=600 #sysctl -w net.inet.tcp.fast_finwait2_recycle=1

以下三个参数可以在几分钟后大幅减少FIN_WAIT_2 等待数 net.inet.tcp.msl net.inet.tcp.finwait2_timeout net.inet.tcp.fast_finwait2_recycle

保存参数 #vi /etc/sysctl.conf

kern.maxfiles=65536 kern.maxfilesperproc=65536 net.inet.icmp.icmplim=500 net.inet.icmp.icmplim_output=0 kern.ipc.somaxconn=4096 net.inet.tcp.blackhole=2 net.inet.udp.blackhole=1 net.inet.udp.maxdgram=65536 net.inet.tcp.sendspace=65536 net.inet.tcp.recvspace=65536 kern.ipc.nmbclusters=32768 net.inet.tcp.msl=2500 net.inet.tcp.finwait2_timeout=600 net.inet.tcp.fast_finwait2_recycle=1

#vi /boot/loader.conf

kern.ipc.nmbclusters=”102400″ kern.ipc.nmbufs=”409600″

这2个参数须重启服务器才能生效

查看tcp状态 #netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}’

FIN_WAIT_1 1610 FIN_WAIT_2 19 SYN_SENT 54 LAST_ACK 286 CLOSING 7 CLOSE_WAIT 2 SYN_RCVD 55 TIME_WAIT 213 ESTABLISHED 10928

#netstat -m

2284/25241/27525 mbufs in use (current/cache/total) 664/14398/15062/32768 mbuf clusters in use (current/cache/total/max) 664/14312 mbuf+clusters out of packet secondary zone in use (current/cache) 998/8109/9107/12800 4k (page size) jumbo clusters in use (current/cache/total/max) 0/0/0/6400 9k jumbo clusters in use (current/cache/total/max) 0/0/0/3200 16k jumbo clusters in use (current/cache/total/max) 5891K/67542K/73433K bytes allocated to network (current/cache/total) 0/0/0 requests for mbufs denied (mbufs/clusters/mbuf+clusters) 0/0/0 requests for jumbo clusters denied (4k/9k/16k) 0/0/0 sfbufs in use (current/peak/max) 0 requests for sfbufs denied 0 requests for sfbufs delayed 160 requests for I/O initiated by sendfile 0 calls to protocol drain routines

参考 http://wiki.freebsdchina.org/doc/s/sysctl/tuning http://blog.csdn.net/hjue/archive/2005/12/16/553703.aspx

Posted in *bsd.

Tagged with , , .


VMware下安装ubuntu7

原先硬盘里装的是xp和suse双系统,现想在xp下用vm虚拟ubuntu

一。删除suse 因为硬盘空间不够,先把suse的空间回收 分区魔术师一直报错,改用acronis disk director删除linux分区,格式化成ntfs。

二。修复xp启动 重启后会进入grub grub>root (hd0,2) grub>setup (hd0) 如果出现successed,就表示成功了。 grub>quit 然后再重启可进入xp。

修复mbr 1 下载 MbrFix 工具 http://www.linuxidc.com/upload/2007_11/07110411258868.rar 2 MBRFix工具修复系统启动项很方便,先进入cmd命令窗口,然后进入mbrfix工具所在的目录(用cd命令),然后输入命令 MbrFix /drive 0 fixmbr ,再确认一下即可。如果默认系统在D盘,则为MbrFix /drive 1 fixmbr ,依此类推 。

以后可以直接进入xp了

三。准备安装源 vmware版本为6.52 build-156735 ubuntu7.10的光盘

四。安装ubuntu

用VMware安装Ubuntu傻瓜书 http://soft.zdnet.com.cn/software_zone/2009/0219/1346542.shtml

我装了三次每次至82%时就卡着不动 据说是官方的镜像更新网速太慢,会卡个几小时 解决方法 !安装前先把网线拨了 !在vm settings中network Adapter的Connetc at power on 去掉勾 !在进入安装界面后,打开右上角的Network,把网络给关了 !语言不要选中文,用默认的英文

五。安装后设置 ubuntu 的root权限需用sudo来操作 1.修改ll命令

sudo vim ~/.bashrc

在命令模式下输入:set nu 显示行号,再输入:65跳到65行

#alias ll=’ls -l’

去掉前面的#注释

保存后用source生效

source ~/.bashrc

2.修改hostname

sudo hostname ubuntu sudo vi /etc/hostname

改成ubuntu

sudo vi /etc/hosts

改成ubuntu

3.关闭ipv6

sudo vim /etc/modprobe.d/aliases

将这一行 alias net-pf-10 ipv6 替换成下面二行 alias net-pf-10 off alias ipv6 off 重启

Posted in LINUX, 技术.

Tagged with , .


CentOs5.2安装tomcat

查看当前版本

java -version java version “1.4.2”

卸载1.4

rpm -qa |grep java java-1.4.2-gcj-compat-1.4.2.0-26jpp …… rpm -e java-1.4.2-gcj-compat-1.4.2.0-26jpp
====================== 2010-11-5更新 jdk6 6u2364位版 http://www.java.net/download/jdk6/6u23/promoted/b03/binaries/jdk-6u23-ea-bin-b03-linux-amd64-18_oct_2010-rpm.bin ======================

安装jdk1.6

wget http://www.java.net/download/jdk6/6u18/promoted/b04/binaries/jdk-6u18-ea-bin-b04-linux-i586-28_oct_2009-rpm.bin chmod 755 jdk-6u18-ea-bin-b04-linux-i586-28_oct_2009-rpm.bin\?e\=1257754475\&h\=106947ca348d71bef632eb95d717de05 ./jdk-6u18-ea-bin-b04-linux-i586-28_oct_2009-rpm.bin\?e\=1257754475\&h\=106947ca348d71bef632eb95d717de05

一路空格后健入yes

查看当前版本

java -version java version “1.6.0” OpenJDK Runtime Environment (build 1.6.0-b09) OpenJDK 64-Bit Server VM (build 1.6.0-b09, mixed mode)
======================= 2010-11-5更新 tomcat 新的版本及下载地址 http://labs.renren.com/apache-mirror/tomcat/tomcat-6/v6.0.29/bin/apache-tomcat-6.0.29.tar.gz =======================

安装tomcat

wget http://labs.xiaonei.com/apache-mirror/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz tar zxvf apache-tomcat-6.0.20.tar.gz mv apache-tomcat-6.0.20 /opt/tomcat

设置环境变量

vi /etc/profile JAVA_HOME=/usr/java/jdk1.6.0_20 export JAVA_HOME PATH=$PATH:$JAVA_HOME/bin export PATH CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export CLASSPATH export JDK_HOME=${JAVA_HOME} export CATALINA_BASE=/opt/tomcat export CATALINA_HOME=/opt/tomcat

启动tomcat

source /etc/profile /opt/tomcat/bin/startup.sh

打开8080防火墙

iptables -A INPUT -p tcp -m tcp –dport 8080 -j ACCEPT service iptables save service iptables restart

http://localhost:8080

参考: https://blog.c1gstudio.com/archives/142 tomcat安全设置 https://blog.c1gstudio.com/archives/865 优化tomcat 内存 https://blog.c1gstudio.com/archives/276

Posted in Tomcat, 技术.

Tagged with .


导出索爱机型里面的通讯录至GPhone(G1/G2)

1备份 通讯录->备份 会在内存卡中生成一个contacts.vcf 路径为other\Backup\Contacts\contacts.vcf

2.将文件移至电脑 通过读卡器或usb

3.转换格式 vcf文件不能直接导入gmail。需要用到这个网站:http://labs.brotherli.ch/vcfconvert/ 选择Vcf文件地址,然后选择Gmail(CSV),然后点击下面的Convert就可以了。 Filter那个选项,第一个是只保留E-mail地址,第二个是只保留电话号码。看着勾吧。不勾也行.

如果导入的联系人没有名字或号码格式错误请按下列步骤操作 这次选择CSV格式,导出为contacts2.csv 用editplus打开contacts2.csv,用alt+c (块选择)复制第一行的名字 用excel打开gmail格式的csv文件contact.csv,粘贴刚才的数据至第一行name 将Section 1 – Mobile列的单元格格式设为文本 复制contacts2.csv中的手机号粘贴到Section 1 – Mobile列。 下一步导入gmail

4.导入gmail 现在打开Gmail,进入Contacts,在右上角的import. 上传并导入

5.gphone中同步 打开wifi或gprs 使用gmail账号登入 设置->数据同步->同步联系人 联系人->menu->显示组->所有联系人

参考:http://www.ooophone.com/viewthread.php?tid=1570

Posted in Android, 其它.

Tagged with .


在手机上(android)用SSH

新入了G1可以无时无刻使用学习linux了

它本身就带了个“超级终端”,但时输入ssh提示permission denied,应该是没root权限。 我通过电子市场下了个Connectbot 配合G1的硬键盘,使用起来和电脑差不多。

连接格式: username@hostip:port

键位: 右shift(向上键)为TAB ctrl为按一下轨迹球

Posted in Android.

Tagged with , , .


[转]WEB架构 – v.2008.163.com对新架构的尝试

先看看架构图: 163_nginx

实际上是app_nginx_squid_nginx的结构,nginx顶在最前面,具体有多少台我就不通告了,然后后面有比nginx更多的squid。

这个架构的特点是扩展了squid的功能,并且可以将很多请求绕过squid,实际应用时仍然出现一点问题,并在此系统的超级负荷下收获了不少经验。 为保商业秘密,只能告诉一个大概数字,此系统带图片的负荷每日在2亿-5亿之间,nginx代理全部机器加起来每秒处理的并发数日常总共是10000左右。 另说说每秒并发如何计算,跟据测试和对比,在web最前端每秒并发=系统的established/web server的keep alive。如果不是前端,那就不那么好算了,在这个系统中squid的established极低,不到10个,所以铁定不能从这个数字去判断了,可以使用squid的mgr:info来查看。 在这样的负荷下工作,平常运行时是没有很大的问题,就是在爆发地震性新闻那一小段时间难于招架。 于是我检查了系统,发现几个问题,并相应对系统进行了优化: 1、调整nginx的超时设置 我一般都会把nginx的超时时间设得稍微长一些,免得会抛出504错误,所以一般设为5分钟,但是在高负荷下,nginx总是能探测到很多的超时,经过测试,因为nginx前端堆积过多这些超时的请求,所以nginx的cpu占用率会上涨,最后nginx前端负荷过高,但后面一级的squid应该也负荷同样多的超时请求,但它却好得很。这样看来,nginx代理在处理这些超时请求上还有待提高。所以我调整了超时设置,将超时限定为5秒,这下负载就下来了不少。因为nginx超时后会重新选择一个后端请求,所以对访问影响不大,客户端同时也不会在一个超时请求下等待过长时间,这样就更为友好。 2、将图片用nginx缓存 鉴于nginx代理会出现问题,干脆把修改率不大的图片用nginx缓存起来,直接对外访问好了。于是使用proxy_store配置了一下,图片放到 /dev/shm内存里,然后写一个shell每小时把这些图片全删掉重取。放在内存里删除文件那是快啊,那么多图片一闪就删完了。 这样做下来,nginx的负载就很低了,处理同样的并发量,load average从2直降到了0.2,这时我觉得甚至只用单台nginx都可以工作。 ps: 在实际应用中也尝试了nginx的url hash模块,发现这个模块在有squid当机时,它并不会自动切换到另一台,就这个问题看来这个模块暂时还是不好使。

Posted in 技术, 网站架构.

Tagged with , .


如何在JPEG里隐藏文件

如何在一个JPEG文件里隐藏其他文件 具体做法总结如下:

将你要隐藏的文件用WinRAR打包。比如说打包成a.rar。 运行cmd进入命令行状态。 使用copy /b my.jpg + a.rar new.jpg将图片文件my.jpg和文件包a.rar合并成new.jpg。 这样,在通常状态下,这个文件就和一个图片文件一样,但是如果你用WinRAR来打开这个文件,就可以看到隐藏的那些文件。

Posted in Tools, 其它.

Tagged with .


万人在线论坛,系统优化

原先有tcp超时现像(WGET 十几次后会有超时),插拨网线并重启后已恢复正常。

目前一小时近2W人在线,留下些操作记录

系统信息

dell R410 5504(4核2G) 2 4G2 sas15K 146*2 Intel(R) Xeon(R) CPU E5504 @ 2.00GHz

centos 5.2 64bit nginx+php+mysql

nginx 6个进程 php 96个进程

Linux bora 2.6.18-128.el5 #1 SMP Wed Jan 21 10:41:14 EST 2009 x86_64 x86_64 x86_64 GNU/Linux

sysctl内核 net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768

net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1 #net.ipv4.tcp_tw_len = 1 net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30 #net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535

优化文件句柄 vi /etc/security/limits.conf

  • soft nofile 51200
  • hard nofile 51200

vi /etc/rc.local ulimit -SHn 51200

=================== Active connections: 2419 server accepts handled requests 73668795 73668795 232420556 Reading: 11 Writing: 28 Waiting: 2380

在线会员 – 总计 13433 人在线

top – 13:29:01 up 33 days, 22:53, 2 users, load average: 1.22, 1.60, Tasks: 265 total, 1 running, 264 sleeping, 0 stopped, 0 zombie Cpu(s): 9.3%us, 1.4%sy, 0.0%ni, 88.2%id, 0.5%wa, 0.0%hi, 0.6%si, Mem: 8168412k total, 6691148k used, 1477264k free, 917728k buffe Swap: 4096532k total, 228k used, 4096304k free, 3841696k cache

netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’ TIME_WAIT 4845 SYN_SENT 1 FIN_WAIT1 185 ESTABLISHED 2698 FIN_WAIT2 381 SYN_RECV 162 CLOSING 5 LAST_ACK 137

netstat -n |wc -l 8441

修改tcp_no_metrics_save 默认情况下一个tcp连接关闭后,把这个连接曾经有的参数比如慢启动门限snd_sthresh,拥塞窗口snd_cwnd 还有srtt等信息保存到dst_entry中, 只要dst_entry 没有失效,下次新建立相同连接的时候就可以使用保存的参数来初始化这个连接.通常情况下是关闭的。

echo ‘1’ > /proc/sys/net/ipv4/tcp_no_metrics_save

依然超时

vi /etc/sysctl.conf 增加一行 net.ipv4.tcp_no_metrics_save =1

sysctl -p

开启5分钟后系统负载有小额上升

Active connections: 2520 server accepts handled requests 73715479 73715479 232593589 Reading: 11 Writing: 14 Waiting: 2495

top – 13:46:30 up 33 days, 23:10, 2 users, load average: 1.49, 1.74, 1.64 Tasks: 265 total, 3 running, 262 sleeping, 0 stopped, 0 zombie Cpu(s): 13.8%us, 2.4%sy, 0.0%ni, 82.9%id, 0.1%wa, 0.1%hi, 0.7%si, 0.0 Mem: 8168412k total, 7225928k used, 942484k free, 925236k buffers Swap: 4096532k total, 228k used, 4096304k free, 3893016k cached

vi /etc/sysctl.conf 修改 net.ipv4.tcp_no_metrics_save =0

sysctl -p

5分钟后负载下载,net.ipv4.tcp_no_metrics_save 不是很管用

top – 13:52:18 up 33 days, 23:16, 2 users, load average: 1.18, 1.46, 1.56 Tasks: 265 total, 1 running, 264 sleeping, 0 stopped, 0 zombie Cpu(s): 12.9%us, 2.1%sy, 0.0%ni, 83.5%id, 0.8%wa, 0.1%hi, 0.6%si, 0.0 Mem: 8168412k total, 7274212k used, 894200k free, 927504k buffers Swap: 4096532k total, 228k used, 4096304k free, 3911832k cached

=======================================

cat /proc/sys/net/ipv4/tcp_fin_timeout 60 cat /proc/sys/net/ipv4/tcp_keepalive_time 7200 vi /etc/sysctl.conf

net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 120

sysctl -p

======================== ifconfig eth0 txqueuelen 1000

看了下已经是1000了 ifconfig

TX packets:1924158031 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:339974963014 (316.6 GiB) TX bytes:2165156209254 (1.9 TiB)

================================ cat /proc/sys/net/ipv4/netfilter/ip_conntrack_max 65536

========================= cat /proc/sys/fs/file-nr 4590 0 765985

======================================= cat /proc/sys/net/ipv4/route/gc_interval 60 cat /proc/sys/net/ipv4/route/gc_timeout 300 cat /proc/sys/net/ipv4/route/gc_elasticity 8

eaccelerator

[eaccelerator] zend_extension=”/opt/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so” eaccelerator.shm_size=”32″ eaccelerator.cache_dir=”/opt/php/eaccelerator_cache” eaccelerator.enable=”1″ eaccelerator.optimizer=”1″ eaccelerator.check_mtime=”1″ eaccelerator.debug=”0″ eaccelerator.filter=”” eaccelerator.shm_max=”0″ eaccelerator.shm_ttl=”3600″ eaccelerator.shm_prune_period=”3600″ eaccelerator.shm_only=”0″ eaccelerator.compress=”1″ eaccelerator.compress_level=”9″

nginx

user www website; worker_processes 6; error_log /var/log/nginx/nginx_error.log crit; pid /dev/shm/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; log_format access ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” $http_x_forwarded_for’; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_body_timeout 60; client_max_body_size 8m; #linux 2.4+ sendfile on; tcp_nopush on; tcp_nodelay on; server_name_in_redirect off; keepalive_timeout 60; fastcgi_intercept_errors on; fastcgi_hide_header X-Powered-By; fastcgi_connect_timeout 180; fastcgi_send_timeout 180; fastcgi_read_timeout 180; fastcgi_buffer_size 128k; fastcgi_buffers 4 128K; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_temp_path /dev/shm; gzip on; gzip_min_length 1k; gzip_comp_level 5; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css application/xml; limit_zone one $binary_remote_addr 10m; server { listen 80; server_name bbs.xxx.com *.bbs.xxx.com; index index.html index.htm index.php; root /opt/lampp/htdocs/bbs; error_page 404 403 /404.html; location ~/\.ht { deny all; } location ~ /bbs/attachment\.php?$ { include fcgi.conf; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; limit_conn one 1; limit_rate 30k; } location ~ .*\.php?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; } rewrite ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2 last; rewrite ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3 last; rewrite ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3 last; rewrite ^(.*)/profile-(username|uid)-(.+)\.html$ $1/viewpro.php?$2=$3 last; rewrite ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3 last; location ~(favicon.ico) { log_not_found off; expires 99d; break; } location ~(robots.txt) { log_not_found off; expires 7d; break; } location ~* ^.+\.(jpg|jpeg|gif|png|swf|rar|zip|css|js)$ { valid_referers none blocked *.xxx.com *.xxx.net localhost; if ($invalid_referer) { rewrite ^/ ; return 412; } access_log off; root /opt/lampp/htdocs/bbs; expires 7d; break; } access_log /var/log/nginx/bbs.xxx.com.log access; } }

================== 总计 18518 人在线

top – 17:40:04 up 5:49, 2 users, load average: 1.81, 2.08, 2.20 Tasks: 265 total, 6 running, 259 sleeping, 0 stopped, 0 zombie Cpu(s): 26.0%us, 3.4%sy, 0.0%ni, 69.0%id, 0.7%wa, 0.0%hi, 0.9%si, Mem: 8168412k total, 7173156k used, 995256k free, 486980k buffe Swap: 4096532k total, 0k used, 4096532k free, 3559140k cache

Active connections: 2306 server accepts handled requests 297904 297904 980053 Reading: 7 Writing: 10 Waiting: 2289

netstat -n|wc -l 8157

sysctl

net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 net.ipv4.tcp_max_syn_backlog = 65536 net.core.netdev_max_backlog = 32768 net.core.somaxconn = 32768 net.ipv4.tcp_max_tw_buckets = 10000 net.core.wmem_default = 8388608 net.core.rmem_default = 8388608 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 196608 262144 393216 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 120 net.ipv4.ip_local_port_range = 1024 65535 net.ipv4.tcp_syncookies = 1

=================================== 2009/11/2更新 近日网络有些不稳定,系统日志中有下列信息 tail /var/log/messages

Nov 1 19:37:32 bora kernel: ip_conntrack: table full, dropping packet. Nov 1 19:38:31 bora kernel: ip_conntrack: table full, dropping packet. Nov 1 19:43:15 bora kernel: ip_conntrack: table full, dropping packet. Nov 1 20:38:31 bora kernel: ip_conntrack: table full, dropping packet. Nov 1 20:42:16 bora last message repeated 2 times Nov 1 20:51:42 bora last message repeated 2 times Nov 1 21:23:38 bora last message repeated 3 times Nov 1 21:28:14 bora last message repeated 2 times cat /var/log/messages |grep ‘ip_conntrack: table full’ cat /var/log/messages |grep ‘syslogd 1.4.1: restart’

发现个规律 ip_conntrack: table full信息一般隔5天出现,一天后系统就出现假死,然后需手工重启。 应该就是ip_conntrack的问题

查看当前的参数

cat /proc/sys/net/ipv4/ip_conntrack_max 65536

65536为系统默认1G内存的数值 ip_conntrack_max 计算公式 参考:http://www.wallfire.org/misc/netfilter_conntrack_perf.txt

CONNTRACK_MAX = RAMSIZE (in bytes) / 16384 / (x / 32) #where x is the number of bits in a pointer (for example, 32 or 64 bits)

1G内存的话:102410241024/16384/(32/32)=65536 我的配值为8G 64bit 819210241024/16384/(64/32)=262144

查看RAMSIZE

cat /proc/sys/net/ipv4/netfilter/ip_conntrack_buckets 8192

查看ip_conntrack timeout

cat /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_established 432000

#432000(5天)改成36000 (10小时)

vi /etc/sysctl.conf 修改内核在尾部增加两行

net.ipv4.ip_conntrack_max = 262144 net.ipv4.netfilter.ip_conntrack_tcp_timeout_established = 36000

立即生效 sysctl -p

Posted in linux 维护优化, 技术.

Tagged with .


Nginx 修补bug,平滑升级至0.8.16

A patch to fix VU#180065 vulnerability in 0.1.0-0.8.14. The patch is not required for versions 0.8.16+, 0.7.62+, 0.6.39+, 0.5.38+.

nginx有一安全漏洞影响0.1.0-0.8.14的版本。 除0.8.16+, 0.7.62+, 0.6.39+, 0.5.38+.

wget http://sysoev.ru/nginx/nginx-0.7.62.tar.gz tar zxvf nginx-0.7.62.tar.gz cd nginx-0.7.62

关闭debug模式来减少nginx大小

vi auto/cc/gcc # 最后几行sheft+g #注释这行 #CFLAGS=”$CFLAGS -g”

伪装header

vi src/core/nginx.h #define NGINX_VERSION “1.2” #define NGINX_VER “C1GWS/” NGINX_VERSION

编译

./configure –user=www –group=website –prefix=/opt/nginx –with-http_stub_status_module –with-http_ssl_module make

#不需做make install哈

备份原始文件

mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.old

复制新文件

cp objs/nginx /opt/nginx/sbin/nginx

检查配置文件

/opt/nginx/sbin/nginx -t

如果你的配置文件是0.6X的话会有2个错误

[warn]: the “optimize_server_names” directive is deprecated, use the “server_name_in_redirect” directive instead in /opt/nginx/conf/nginx.conf:36 [emerg]: “server_name_in_redirect” directive is duplicate in /opt/nginx/conf/nginx.conf:37 configuration file /opt/nginx/conf/nginx.conf test failed [warn]: duplicate MIME type “text/html” in /opt/nginx/conf/nginx.conf:63 the configuration file /opt/nginx/conf/nginx.conf syntax is ok configuration file /opt/nginx/conf/nginx.conf test is successful

nginx.conf中去掉server_name_in_redirect及text/html

optimize_server_names off; server_name_in_redirect off; gzip_types text/plain application/x-javascript text/css application/xml;

重命名pid,并启用新的pid #”`“在键盘左上角

kill -USR2 `cat /dev/shm/nginx.pid`

退出旧的nginx

kill -QUIT `cat /dev/shm/nginx.pid.oldbin`

升级完成!

curl -I localhost HTTP/1.1 200 OK Server: C1GWS/1.2

curl -I localhost

Posted in Nginx, 安全, 技术.

Tagged with , .


tomcat安全设置

1.关闭服务器端口: server.xml默认有下面一行:

这样允许任何人只要telnet到服务器的8005端口,输入”SHUTDOWN”,然后回车,服务器立即就被关掉了。 从安全的角度上考虑,我们需要把这个shutdown指令改成一个别人不容易猜测的字符串,可以同时把端口也改了。 例如修改如下:

这样就只有在telnet到8005,并且输入”c1gstudio”才能够关闭Tomcat. 注意:这个修改不影响shutdown.bat的执行。运行shutdown.bat一样可以关闭服务器。

2.增加防火墙 更安全的方式是同时增加防火墙,来限制访问Tomcat的控制与连接器端口 你可以通过运行netstat -a来查看网络服务器socket及其他现有连接的清单 插入规则 iptables -A INPUT -p tcp -m tcp –dport 8005 -j DROP 保存规则 service iptables save 重载规则 service iptables restart

3.处理好Tomcat管理台的安全 Tomcat管理台的应用文件,默认在{Tomcat安装目录}\server\webapps下,有admin和manager两个应用。 其用户密码,在{Tomcat安装目录}\conf/tomcat-users.xml中定义。在{Tomcat安装目录}\webapps下 admin.xml和manager.xml文件定义了可以通过访问/admin和/manager进入。 默认情况下,完全可以登录tomcat管理台,造成严重安全问题 检测办法:用IE打开链接http://[IP]:[Port]/admin,以用户名admin,密码为空登录,如果成功, 说明存在问题。 解决办法:可以删除{Tomcat安装目录}\webapps下admin.xml和manager.xml文件,或者去掉用户密 码,也可以删除应用文件。

4.运行错误网页

如果找不到网页即出现404错误,会显示服务器版本号,服务器配置也一目了然, 为了避免这种情况,希望自定义设置错误页面。 设置如下: 用记事本打开\conf\web.xml文件,在文件的倒数第二行(一行之前)加入以下内容:

404 /404.jsp 500 /500.jsp

在根目录下创建404.jsp和500.jsp文件

5.多重服务器的安全防护模式 当在同一台主机(或同一网络文件系统)上的 Apache httpd Web 服务器与 Tomcat 之间共享网页的实际目录时, 请留意其个别安全防护模式间的相互作用。当你有“受保护的目录”时,这会特别重要。 服务器将具有能读取彼此文件的权限。 在这些状况下,请注意 Tomcat 并不会保护如 .htaccess 的文件,而Apache也不会保护Web应用程序的 WEB-INF 或 META-INF 目录。 这些情形都有可能导致重大的安全漏洞,所以,我们建议你在使用这些特别的目录时,要格外小心。

若要让 Apache httpd 保护 WEB-INF 及 META-INF 目录,请在 httpd.conf 中加入下列内容

AllowOverride None deny from all AllowOverride None deny from all

6.屏蔽目录文件自动列出的方法 conf/web.xml文件

default org.apache.catalina.servlets.DefaultServlet debug 0 listings false 1 false

这里false为不列出,true为充许列出

7.以非root用户运行

8.关闭8009端口 8009/tcp open ajp13

8009端口是tomcat和apache的mod_proxy_ajp,mod_jk沟通的端口,没有用到就关了。

tomcatpath/conf/server.xml 中的这段注释掉

<!– –>

Posted in Tomcat, 技术.

Tagged with , .