当前位置首页 > Nginx知识

nginx配置优化的几个参数(转)-web21

阅读次数:268 次  来源:admin  发布时间:

ginx 配置优化的几个参数(转)

ginx配置文件里面需要注意的一些参数

worker_processes 8

ginx要开启的进程数 一般等于cpu的总核数 其实一般情况下开4个或8个就可 我开2个

以了 多了没有太多用

每个nginx进程消耗的内存10兆的模样

worker_cpu_affinity

仅适用于Linux,使用该选项可以绑定worker进程和CPU(2.4内核的机器用不

了)

假如是8 cpu 分配如下:

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000

00100000 01000000 10000000

ginx可以使用多个worker进程,原因如下:

to use SMP

to decrease latency when workers blockend on disk I/O

to limit number of connections per process when select()/poll() i

used

The worker_processes and worker_connections from the event sectio

allows you to calculate maxclients value: k

max_clients = worker_processes * worker_connectio

worker_rlimit_nofile 102400

每个nginx进程打开文件描述符最大数目 配置要和系统的单进程打开文件数一

致,linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应

应该填写65535

ginx调度时分配请求到进程并不是那么的均衡,假如超过会返回502错误。我

这里写的大一点

use epoll

Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模

型,而Apache则使用的是传统的select模型。

处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。

在高并发服务器中,轮询I/O是最耗时间的操作 目前Linux下能够承受高并发

访问的Squid、Memcached都采用的是epoll网络I/O模型。

worker_connections 65535

每个工作进程允许最大的同时连接数  (Maxclient = work_processes * worker_connections)

keepalive_timeout 75

keepalive超时时间

这里需要注意官方的一句话:

The parameters can differ from each other. Line Keep-Alive:

timeout=time understands Mozilla and Konqueror. MSIE itself shut

keep-alive connection approximately after 60 seconds.

client_header_buffer_size 16k

large_client_header_buffers 4 32k

客户请求头缓冲大小

ginx默认会用client_header_buffer_size这个buffer来读取header值,如果

header过大,它会使用large_client_header_buffers来读取

如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request

求行如果超过buffer,就会报HTTP 414错误(URI Too Long)

ginx接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400的

HTTP错误(Bad Request)。

open_file_cache max 102400

使用字段:http, server, location 这个指令指定缓存是否启用,如果启用,将记录文件以下信息: ·打开的文件描述符,大小信息和修改时间. ·存在的目录信息. ·在搜索文件过程中的错误信息 -- 没有这个文件,无法正确读取,参考open_file_cache_errors 指令选项:

·max - 指定缓存的最大数目,如果缓存溢出,最长使用过的文件(LRU)将被移除

例: open_file_cache max=1000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors o

open_file_cache_error

语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.

open_file_cache_min_use

语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如 果使用更大的值,文件描述符在cache中总是打开状态.

open_file_cache_valid

语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.

开启gzi

gzip o

gzip_min_length  1k

gzip_buffers     4 16k

gzip_http_version 1.0

gzip_comp_level 2

gzip_types       text/plain application/x-JavaScript text/c

application/xml

gzip_vary o

缓存静态文件:

location ~* ^.+\.(swf|gif|png|jpg|js|css)$ {

root   /usr/local/ku6/ktv/show.ku6.com/

expires 1m

}

优化Linux内核参数

vi /etc/sysctl.conf

# Add

et.ipv4.tcp_max_syn_backlog = 65536

et.core.netdev_max_backlog =  32768

et.core.somaxconn = 32768

et.core.wmem_default = 8388608

et.core.rmem_default = 8388608

et.core.rmem_max = 16777216

et.core.wmem_max = 16777216

et.ipv4.tcp_timestamps = 0

et.ipv4.tcp_synack_retries = 2

et.ipv4.tcp_syn_retries = 2

et.ipv4.tcp_tw_recycle = 1

#net.ipv4.tcp_tw_len = 1

et.ipv4.tcp_tw_reuse = 1

et.ipv4.tcp_mem = 94500000 915000000 927000000

et.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30

#net.ipv4.tcp_keepalive_time = 120

et.ipv4.ip_local_port_range = 1024  65535

附录:一些错误排查

PHP-cgi进程数不够用、php执行时间长(MySQL慢)、或者是php-cgi进程死掉

,都会出现502错误

一般来说Nginx 502 Bad Gateway和php-fpm.conf的设置有关,而Nginx 504 Gateway Time-out则是与nginx.conf的设置有关

1、查看当前的PHP FastCGI进程数是否够用:

etstat -anpo | grep "php-cgi" | wc -l

如果实际使用的“FastCGI进程数”接近预设的“FastCGI进程数”,那么

,说明“FastCGI进程数”不够用,需要增大。

2、部分PHP程序的执行时间超过了Nginx的等待时间,可以适当增加

ginx.conf配置文件中FastCGI的timeout时间,例如:

htt

{

......

fastcgi_connect_timeout 300

fastcgi_send_timeout 300

fastcgi_read_timeout 300

......

}

413 Request Entity Too Large

增大client_max_body_size

client_max_body_size:指令指定允许客户端连接的最大请求实体大小,它出现在请求头部的Content-Length字段. 如果请求大于指定的值,客户端将收到一个"Request Entity Too Large" (413)错误. 记住,浏览器并不知道怎样显示这个错误.

hp.ini中增大

ost_max_size 和upload_max_filesize

发表于 2016-11-18 17:10  web21  阅读(585)  评论(0)  编辑  收藏  举报

nginx配置优化的几个参数(转)-web21

上一篇:Debian系统常用配置
下一篇:Apachemod_fcgidfcgid_header_bucket_read函数缓冲区溢出漏洞