2014-04-11 10:53:01
来 源
IT技术网
Nginx
本篇分享了Nginx服务器优化时需要调整的几个参数,希望对于初学Nginx服务器相关的朋友有帮助,更多Nginx安装、配置、报错处理等资源请本站内搜索。。
-- 最近在服务器上搞了一些nginx  研究了一下 总结总结

worker_processes 8

nginx要开启的进程数 一般等于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

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

to use SMP

to decrease latency when workers blockend on disk I/O

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

used

The worker_processes and worker_connections from the event sections

allows you to calculate maxclients value: k

max_clients = worker_processes * worker_connections

worker_rlimit_nofile 102400;

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

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

应该填写65535 

nginx调度时分配请求到进程并不是那么的均衡,假如超过会返回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 shuts

keep-alive connection approximately after 60 seconds.

client_header_buffer_size 16k

large_client_header_buffers 4 32k

客户请求头缓冲大小

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

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

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

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

nginx接受最长的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 on;

open_file_cache_errors

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

open_file_cache_min_uses

语法: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中缓存项目的有效信息.

开启gzip

gzip on;

  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/css application/xml;

  gzip_vary on;

缓存静态文件:

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

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

expires 1m;

}

优化Linux内核参数 vi /etc/sysctl.conf

  

# Add

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

 

附录:一些错误排查

php-cgi进程数不够用、php执行时间长(mysql慢)、或者是php-cgi进程死掉 ,都会出现502错误

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

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

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

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

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

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

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

http

{

......

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)错误. 记住,浏览器并不知道怎样显示这个错误.

php.ini中增大

post_max_size 和upload_max_filesize

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。