【nginx】配置文件的优化

王朝学院·作者佚名  2016-08-27  
宽屏版  字体: |||超大  

1、编译安装过程优化在编译Nginx时,默认以debug模式进行,而在debug模式下会插入很多跟踪和ASSERT之类的信息,编译完成后,一个Nginx要有好几兆字节。在编译前取消Nginx的debug模式,编译完成后Nginx只有几百千字节,因此可以在编译之前,修改相关源码,取消debug模式,具体方法如下:

在Nginx源码文件被解压后,找到源码目录下的auto/cc/gcc文件,修改如下几行

sed -i 's@CFLAGS="$CFLAGS -g"@#CFLAGS="$CFLAGS -g"@' auto/cc/gcc

为特定的CPU指定CPU类型编译优化在编译Nginx时,默认的GCC编译参数是“-O”,要优化GCC编译,可以使用以下两个参数:

--with-cc-opt='-O3'

--with-cpu-opt=CPU #为特定的 CPU 编译,有效的值包括:pentium, pentiumPRo, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

要确定CPU类型,可以通过如下命令:

cat /proc/cpuinfo | grep "model name"

nginx配置参数

--prefix=/usr/local/nginx \

--error-log-path=/data/logs/error/error.log \

--http-log-path=/data/logs/access/access.log \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \ \

--conf-path=/etc/nginx/nginx.conf \

--sbin-path=/usr/sbin/nginx \

--user=www \

--group=webgrp \

--vhost-domain=example.com \

--pro-language=php\

--with-http_ssl_module \

--with-http_realip_module \

--with-http_addition_module \

--with-http_image_filter_module \

--with-http_sub_module \

--with-http_dav_module \

--with-http_flv_module \

--with-http_mp4_module \

--with-http_gzip_static_module \

--with-http_concat_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_degradation_module \

--with-http_sysguard_module \

--with-backtrace_module \

--with-http_stub_status_module \

--with-http_upstream_check_module \

--with-google_perftools_module \

--with-http_geoip_module\

--with-pcre=/usr/local/src/pcre-8.35 \

--with-http_image_filter_module\

2、隐藏版本号在nginx配置文件的http标签内加入“server_tokens off; ”参数,也可以放大server标签和location标签中

或者在源代码中更改

src/core/nginx.h

#define NGINX_VERSION "1.6.2" // 修改为想要的版本号如2.4.3

#define NGINX_VER "nginx/" 改为 Apache

src/http/ngx_http_header_filter_module.c

static char ngx_http_server_string[] ="Server:nginx" //改为apache

src/http/ngx_http_special_response.c

static u_char ngx_http_error_full_tail[] =

"<hr><center>" NGINX_VER "</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

static u_char ngx_http_error_tail[] =

"<hr><center>nginx</center>" CRLF

"</body>" CRLF

"</html>" CRLF

;

//改为apache

3、利用TCMalloc优化Nginx的性能TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具“google-perftools”中的一个成员。与标准的glibc库的malloc相比,TCMalloc库在内存分配效率和速度上要高很多,这在很大程度上提高了服务器在高并发情况下的性能,从而降低系统负载。下面简单介绍如何为Nginx添加TCMalloc库支持。

要安装TCMalloc库,需要安装libunwind(32位操作系统不需要安装)和google-perftools两个软件包,libunwind库为基于64位CPU和操作系统的程序提供了基本函数调用链和函数调用寄存器功能。下面介绍利用TCMalloc优化Nginx的具体操作过程:

centos 》 nginx 》gperftools》libunwind 依赖顺序, 先安装libunwind

cd /usr/local/src

wget -c http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz >libunwind-1.1.tar.gz

tar zxf libunwind-1.1.tar.gz

cd libunwind-1.1

CFLAGS=-fPIC ./configure --prefix=/usr --enable-shared

make CFLAGS=-fPIC

make CFLAGS=-fPIC install

安装google-perftools

cd /usr/local/src

wget -c ftp://ftp.tw.freebsd.org/pub/ports/distfiles/gperftools-2.1.tar.gz

#wget -c http://gperftools.googlecode.com/files/google-perftools-2.1.tar.gz(现在googlecode.com被封了)

tar -vxzf gperftools-2.1.tar.gz

cd gperftools-2.1

./configure \

--prefix=/usr/local/gperftools \

--disable-cpu-profiler \

--enable-shared \

--disable-heap-profiler \

--disable-heap-checker \

--disable-dependency-tracking \

--enable-frame-pointers

or

./configure --prefix=/usr/local/gperftools --enable-frame-pointers

make && make install

到这里安装google-perftools完成了但未生效,接下来需要使google-perftools生效

/sbin/ldconfig

echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf

ldconfig

or

ln -s /usr/local/gperftools/lib/* /usr/local/lib

ldconfig

详见:http://www.cnblogs.com/chenpingzhao/p/4603437.html

nginx配置文件修改

google_perftools_profiles /data/www/tmp/tcmalloc;

验证运行状态

[root@dev http]# lsof -n | grep tcmalloc

nginx 18292 www 13w REG 8,2 0 660107 /data/www/tmp/tcmalloc.18292

nginx 18293 www 15w REG 8,2 0 660109 /data/www/tmp/tcmalloc.18293

nginx 18294 www 23w REG 8,2 0 660108 /data/www/tmp/tcmalloc.18294

nginx 18295 www 25w REG 8,2 0 660110 /data/www/tmp/tcmalloc.18295

nginx 18296 www 30w REG 8,2 0 660106 /data/www/tmp/tcmalloc.18296

4、配置文件优化nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu计为8)

worker_processes 8;

为每个进程分配cpu,上例中将8 个进程分配到8 个cpu,当然可以写多个,或者将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;//8颗cpu

worker_cpu_affinity 0001 0010 0100 1000; //4颗cpu

这个指令是指当一个nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx 进程数相除,但是nginx 分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致,可以限制为操作系统最大的限制65535

worker_rlimit_nofile 65535;

客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小,但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数

[root@dev http]# getconf PAGESIZE

4096

client_header_buffer_size 4k;

这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。

open_file_cache max=65535 inactive=60s;

这个是指多长时间检查一次缓存的有效信息

open_file_cache_valid 80s;

open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive 时间内一次没被使用,它将被移除。

open_file_cache_min_uses 1;

开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载

sendfile on

tcp_nopush on //只有在sendfile开启模式下有效

长连接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操作系统的设置,可以另外起一片文章了。长连接请求大量小文件的时候,可以减少重建连接的开销,但假如有大文件上传,65s内没上传完成会导致失败。如果设置时间过长,用户又多,长时间保持连接会占用大量资源。

keepalive_timeout 60;#设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接。

tcp_nodelay on;#打开tcp_nodelay,在包含了keepalive参数才有效

client_header_timeout 15;#设置客户端请求头读取超时时间,如果超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out(408)”错误

client_body_timeout 15;#设置客户端请求主体读取超时时间,如果超过这个时间,客户端还没有发送任何数据,Nginx将返回“Request time out(408)”错误

send_timeout 15;#指定响应客户端的超时时间。这个超过仅限于两个连接活动之间的时间,如果超过这个时间,客户端没有任何活动,Nginx将会关闭连接。

允许客户端请求的最大单文件字节数。如果有上传较大文件,请设置它的限制值

client_max_body_size 10m

缓冲区代理缓冲用户端请求的最大字节数

client_body_buffer_size 128k

事件处理模型优化,根据系统类型不同选择不同的事务处理模型,选择有“use [ kqueue | rtsig |epool |dev/pool |select |pllo ];

events {

use epoll;

worker_connections 65536;

}

Max_client=worker_processes*worker_connections

fastcgi配置

fastcgi_connect_timeout 300;#指定链接到后端FastCGI的超时时间。

fastcgi_send_timeout 300;#向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。

fastcgi_read_timeout 300;#指定接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。

fastcgi_buffer_size 64k;#指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为gastcgi_buffers选项指定的缓冲区大小。

fastcgi_buffers 4 64k;#指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求,如果一个php脚本所产生的页面大小为256KB,那么会分配4个64KB的缓冲区来缓存,如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于磁盘。一般这个值应该为站点中php脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“8 16K”、“4 64k”等。

fastcgi_busy_buffers_size 128k;#建议设置为fastcgi_buffer的两倍,繁忙时候的buffer

fastcgi_temp_file_write_size 128k;#在写入fastcgi_temp_path时将用多大的数据库,默认值是fastcgi_buffers的两倍,设置上述数值设置小时若负载上来时可能报502 Bad Gateway

fastcgi_cache oldboy_ngnix;#表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502的错误放生,但是开启缓存也可能会引起其他问题,要很据具体情况选择

fastcgi_cache_valid 200 302 1h;#用来指定应答代码的缓存时间,实例中的值表示将2000和302应答缓存一小时,要和fastcgi_cache配合使用

fastcgi_cache_valid 301 1d;#将301应答缓存一天

fastcgi_cache_valid any 1m;#将其他应答缓存为1分钟

fastcgi_cache_min_uses 1;#请求的数量

fastcgi_cache_path /data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m;#定义缓存的路径

gzip配置

gzip on;#开启压缩功能

gzip_min_length 1k;#设置允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取,默认值是0,不管页面多大都进行压缩,建议设置成大于1K,如果小与1K可能会越压越大。

gzip_buffers 4 32k;#压缩缓冲区大小,表示申请4个单位为32K的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果。

gzip_http_version 1.1;#压缩版本(默认1.1,前端为squid2.5时使用1.0)用于设置识别HTTP协议版本,默认是1.1,目前大部分浏览器已经支持GZIP解压,使用默认即可

gzip_comp_level 9;#压缩比例,用来指定GZIP压缩比,1压缩比最小,处理速度最快,9压缩比最大,传输速度快,但是处理慢,也比较消耗CPU资源。

gzip_types text/CSStext/xmlapplication/javascript;#用来指定压缩的类型,‘text/html’类型总是会被压缩。

gzip_vary on;#vary header支持,改选项可以让前端的缓存服务器缓存经过GZIP压缩的页面,例如用Squid缓存经过nginx压缩的数据。

expires配置

对于图片,CSS,JS等元素更改的机会较少,特别是图片,这时可以将图片设置在浏览器本地缓存365天或更长,CSS,JS,html等代码缓存10天,这样用户第一次打开页面后,会在本地缓存上述内容,提高了以后打开的页面加载速度,节省服务端大量贷款,此功能同apache的expires。这里通过location,将需要缓存的扩展名列出来,然后指定缓存时间

location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)(\?[0-9]+)?$ {

expires 30d;

log_not_found off;

access_log off;

}

location ~* \.(js|css)$ {

expires 7d;

log_not_found off;

access_log off;

}

location = /(favicon.ico|roboots.txt) {

access_log off;

log_not_found off;

}

URL访问控制

来就应该只是资源文件,禁止指定扩展名程序被执行,例如:.php,.sh,.pl,nginx下禁止访问资源目录下的php程序文件

location ~ ^/images/.*\.(php|php5|.sh|.pl|.py)$

{

deny all;

}

if ($request_method !~ ^(GET|HEAD|POST)$ ) {

return 444;

}

location ~ ^/static/.*\.(php|php5|.sh|.pl|.py)$

{

deny all;

}

location ~* ^/data/(attachment|avatar)/.*\.(php|php5)$

{

deny all;

}

限制使用网站ip访问网站

server {

listen 80 default_server;

server_name _;

return 444;

}

图片及目录防盗链

location ~* \.(jpg|gif|png|swf|flv|wma|wmv|asf|mp3|mmf|zip|rar)$ {

valid_referers none blocked *.etiantian.org etiantian.org;

if ($invalid_referer) {

return 302 http://www.explam.com/img/nolink.jpg;

}

}

优雅的错误提示

error_page 500 501 502 503 504 http://www.example.com/error2.html;

error_page 400 403 404 405 408 410 411 412 413 414 415 http://www.example.com/error1.html;

爬虫优化,可以进行适当限速

使用tmpfs文件系统给/tmp

提高效率,部分程序切图片操作临时放到/tmp下,可以把tmp设置成内存文件系统,占用内存空间的,就是从内存里拿出一块来当磁盘用

mount -t tmpfs -o size=16m tmpfs /tmp

防DOS攻击

限制单个ip的 req/s , conn

map $remote_addr $rt_filtered_ip {

default $binary_remote_addr;

1.2.3.4 "";

4.4.4.4 "";

}

or

geo $rt_filtered_ip {

default $binary_remote_addr;

127.0.0.1 "";

192.168.1.0/24 "";

10.1.0.0/16 "";

::1 "";

2001:0db8::/32 "";

1.2.3.4 ""

}

limit_conn_zone $binary_remote_addr zone=perip:10m;

limit_conn_zone $server_name zone=perserver:10m;

limit_conn_zone $host$uri zone=peruri:10m;

limit_req_zone $rt_filtered_ip zone=qps:10m rate=1r/s;

server {

location = /wp-login.php {

limit_req zone=qps burst=5 nodelay;

limit_conn perip 10;

limit_conn perserver 100;

limit_rate 500k;

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

}

}

ab -n 100 -c 10 example.com/wp-login.php

$binary_remote_addr是限制同一客户端ip地址;

$server_name是限制同一server最大并发数;

limit_conn为限制并发连接数;

limit_rate为限制下载速度;

访问控制 allow/deny

location /nginx-status {

stub_status on;

access_log off;

auth_basic "NginxStatus";

auth_basic_user_file /usr/local/nginx/htpasswd;

allow 192.168.10.100;

allow 172.29.73.0/24;

deny all;

} //htpasswd -c htpasswd admin

5、一个标准的配置文件nginx.conf配置

user www webgrp;

worker_processes 4;

worker_cpu_affinity 0001 0010 0100 1000;

worker_rlimit_nofile 65536;

pid/var/run/nginx.pid;

#error_log/data/logs/error/nginx_error.log info;

google_perftools_profiles /data/www/tmp/tcmalloc;

events

{

useepoll;

worker_connections65536;

}

http

{

includemime.types;

default_typeapplication/octet-stream;

charset utf-8;

server_names_hash_bucket_size 128;

client_header_buffer_size 4k;

large_client_header_buffers 4 32k;

client_max_body_size 32m;

open_file_cache max=65536 inactive=20s;

open_file_cache_valid 30s;

open_file_cache_min_uses 1;

sendfile on;

tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

server_tokens off;

port_in_redirect off;

fastcgi_connect_timeout600;

fastcgi_send_timeout 600;

fastcgi_read_timeout 600;

fastcgi_buffer_size 64k;

fastcgi_buffers4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

fastcgi_temp_path /dev/shm/nginx_tmp;

fastcgi_intercept_errors on;

#fastcgi_cache_path /data/www/tmp/_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=10g;

#open gzip

gzip on;

gzip_vary on;

gzip_min_length1k;

gzip_buffers 4 16k;

gzip_http_version 1.0;

gzip_comp_level4;

gzip_proxied any;

gzip_typestext/plain application/x-Javascripttext/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;

gzip_disable "MSIE [1-6]\.(?!.*SV1)";

#Proxy

proxy_connect_timeout600;

proxy_read_timeout600;

proxy_send_timeout600;

proxy_buffer_size16k;

proxy_buffers432k;

proxy_busy_buffers_size64k;

proxy_temp_file_write_size64k;

proxy_cache_path /data/www/tmp/cache levels=1:2 keys_zone=Z:100m inactive=7d max_size=30g;

#Limit

limit_req_zone $binary_remote_addr zone=xxx:10m rate=5r/s;

limit_req_zone $binary_remote_addr zone=qps1:1m rate=1r/s;

#Log format

log_format access '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for $request_body'

'upstream_response_time $upstream_response_time request_time $request_time';

#502_next

upstream php_fpm_sock{

server unix:/dev/shm/php-fpm.socket;

server unix:/dev/shm/php-fpm-b.socket;

server unix:/dev/shm/php-fpm-c.socket;

}

fastcgi_next_upstream error timeout invalid_header http_503 http_500;

#cros

add_header Access-Control-Allow-Origin *;

#add_header X-Cache-CFC "$upstream_cache_status - $upstream_response_time";

server

{

listen 80 default;

server_name _;

return 444;

}

include vhosts/*.conf;

}

vhost配置

server {

listen 80;

server_name www.example.com example.com;

root /data/www/www.example.com/;

index index.php index.html index.htm;

access_log /data/logs/access/www.example.com_access.log access;

error_log /data/logs/error/www.example.com_error.log crit;

location @fobidden{

include fastcgi_params;

fastcgi_pass unix:php_fpm_sock;

fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

allow 124.112.113.242;

deny all;

}

location /nginx_status {

stub_status on;

access_log off;

allow 124.112.113.242;

deny all;

}

location = /status {

try_files $uri $uri/ @fobidden;

}

if ($host = example.com ) {

return 301 $scheme://www.example.com$request_uri;

}

location ~ /bbs/ {

if ($host = www.example.com ) {

rewrite ^/bbs/(.*)$ $scheme://bbs.example.com/$1 permanent;

break;

}

}

location = /application/ {

return 302 /application/uploads/other/404-3.jpg;

}

location / {

try_files $uri $uri/ /application.php?s=$uri&$args;

}

location ~* \.php($|/){

set $script $uri;

set $path_info "";

if ($uri ~ "^(.+?\.php)(/.+)$") {

set $script $1;

set $path_info $2;

}

fastcgi_pass unix:php_fpm_sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$script;

fastcgi_param SCRIPT_NAME $script;

fastcgi_param PATH_INFO $path_info;

include fastcgi.conf;

include fastcgi_params;

#fastcgi_cache ngx_fcgi_cache;

#fastcgi_cache_valid 200 302 1h;

#fastcgi_cache_valid 301 1d;

#fastcgi_cache_valid any 1m;

#fastcgi_cache_min_uses 1;

#fastcgi_cache_use_stale error timeout invalid_header http_500;

#fastcgi_cache_key $scheme://$host$request_uri;

}

location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)(\?[0-9]+)?$ {

expires 30d;

log_not_found off;

access_log off;

}

location ~* \.(js|css)$ {

expires 7d;

log_not_found off;

access_log off;

}

location = /(favicon.ico|roboots.txt) {

access_log off;

log_not_found off;

}

location ~* \.(htacess|svn|tar.gz|tar|zip|sql) {

return 404;

}

#location ~* \.(gif|jpg|png|swf|flv)$ {

#valid_referers none blocked www.example.com example.com

#if ($invalid_referer) {

#return 404;

#}

#}

error_page 404 500 502 /Common/tpl/404.html;

}

6、系统内核的优化sysctl.conf 中一部分配置

net.ipv4.tcp_max_tw_buckets = 6000

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_syncookies = 1

net.core.somaxconn = 262144

net.core.netdev_max_backlog = 262144

net.ipv4.tcp_max_orphans = 262144

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

下面是对实例中选项的含义进行介绍:

net.ipv4.tcp_max_tw_buckets参数用来设定timewait的数量,默认是180000,这里设为6000。

net.ipv4.ip_local_port_range选项用来设定允许系统打开的端口范围。

net.ipv4.tcp_tw_recycle选项用于设置启用timewait快速回收。

net.ipv4.tcp_tw_reuse选项用于设置开启重用,允许将TIME-WAIT sockets重新用于新的TCP连接。

net.ipv4.tcp_syncookies选项用于设置开启SYN Cookies,当出现SYN等待队列溢出时,启用cookies进行处理。

net.core.somaxconn选项默认值是128, 这个参数用于调节系统同时发起的tcp连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此,需要结合并发请求数来调节此值。

net.core.netdev_max_backlog选项表示当每个网络接口接收数据包的速率比内核处理这些包的速率快时,允许发送到队列的数据包的最大数目。

net.ipv4.tcp_max_orphans选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立连接将立即被复位并打印出警告信息。这个限制只是为了防止简单的DoS攻击。不能过分依靠这个限制甚至人为减小这个值,更多的情况是增加这个值。

net.ipv4.tcp_max_syn_backlog选项用于记录那些尚未收到客户端确认信息的连接请求的最大值。对于有128MB内存的系统而言,此参数的默认值是1024,对小内存的系统则是128。

net.ipv4.tcp_synack_retries参数的值决定了内核放弃连接之前发送SYN+ACK包的数量。

net.ipv4.tcp_syn_retries选项表示在内核放弃建立连接之前发送SYN包的数量。

net.ipv4.tcp_fin_timeout选项决定了套接字保持在FIN-WAIT-2状态的时间。默认值是60秒。正确设置这个值非常重要,有时候即使一个负载很小的Web服务器,也会出现因为大量的死套接字而产生内存溢出的风险。

net.ipv4.tcp_keepalive_time选项表示当keepalive启用的时候,TCP发送keepalive消息的频度。默认值是2(单位是小时)。

一个标准的配置文件

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_tw_buckets = 6000

net.ipv4.tcp_sack = 1

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_rmem = 4096 87380 4194304

net.ipv4.tcp_wmem = 4096 16384 4194304

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.core.somaxconn = 262144

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

net.ipv4.ip_local_port_range = 1024 65000

修改系统最大连接数 /etc/security/limits.conf

* soft nofile 65535

* hard nofile 65535

* soft nproc 65535

* hard nproc 65535 //ulimit -SHn 65535

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有