Nginx 配置详解

    # This number should be, at maximum, the number of CPU cores on your system.
    # (since nginx doesn't benefit from more than one worker per CPU.)
    # 这里的数值不能超过 CPU 的总核数,因为在单个核上部署超过 1 个 Nginx 服务进程并不起到提高性能的作用。
    worker_processes 24;

    # Number of file descriptors used for Nginx. This is set in the OS with 'ulimit -n 200000'
    # or using /etc/security/limits.conf
    # Nginx 最大可用文件描述符数量,同时需要配置操作系统的 "ulimit -n 200000",或者在 /etc/security/limits.conf 中配置。
    worker_rlimit_nofile 200000;

    # only log critical errors
    # 只记录 critical 级别的错误日志
    error_log /var/log/nginx/error.log crit

    # Determines how many clients will be served by each worker process.
    # (Max clients = worker_connections * worker_processes)
    # "Max clients" is also limited by the number of socket connections available on the system (~64k)
    # 配置单个 Nginx 单个进程可服务的客户端数量,(最大值客户端数 = 单进程连接数 * 进程数 )
    # 最大客户端数同时也受操作系统 socket 连接数的影响(最大 64K )
    worker_connections 4000;

    # essential for linux, optmized to serve many clients with each thread
    # Linux 关键配置,允许单个线程处理多个客户端请求。
    use epoll;

    # Accept as many connections as possible, after nginx gets notification about a new connection.
    # May flood worker_connections, if that option is set too low.
    # 允许尽可能地处理更多的连接数,如果 worker_connections 配置太低,会产生大量的无效连接请求。
    multi_accept on;

    # Caches information about open FDs, freqently accessed files.
    # Changing this setting, in my environment, brought performance up from 560k req/sec, to 904k req/sec.
    # I recommend using some varient of these options, though not the specific values listed below.
    # 缓存高频操作文件的FDs(文件描述符/文件句柄)
    # 在我的设备环境中,通过修改以下配置,性能从 560k 请求/秒 提升到 904k 请求/秒。
    # 我建议你对以下配置尝试不同的组合,而不是直接使用这几个数据。
    open_file_cache max=200000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

    # Buffer log writes to speed up IO, or disable them altogether
    # 将日志写入高速 IO 存储设备,或者直接关闭日志。
    # access_log /var/log/nginx/access.log main buffer=16k;
    access_log off;

    # Sendfile copies data between one FD and other from within the kernel.
    # More efficient than read() + write(), since the requires transferring data to and from the user space.
    # 开启 sendfile 选项,使用内核的 FD 文件传输功能,这个比在用户态用 read() + write() 的方式更加高效。
    sendfile on;

    # Tcp_nopush causes nginx to attempt to send its HTTP response head in one packet,
    # instead of using partial frames. This is useful for prepending headers before calling sendfile,
    # or for throughput optimization.
    # 打开 tcp_nopush 选项,Nginux 允许将 HTTP 应答首部与数据内容在同一个报文中发出。
    # 这个选项使服务器在 sendfile 时可以提前准备 HTTP 首部,能够达到优化吞吐的效果。
    tcp_nopush on;

    # don't buffer data-sends (disable Nagle algorithm). Good for sending frequent small bursts of data in real time.
    # 不要缓存 data-sends (关闭 Nagle 算法),这个能够提高高频发送小数据报文的实时性。
    tcp_nodelay on;

    # Timeout for keep-alive connections. Server will close connections after this time.
    # 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接。
    keepalive_timeout 30;

    # Number of requests a client can make over the keep-alive connection. This is set high for testing.
    # 单个客户端在 keep-alive 连接上可以发送的请求数量,在测试环境中,需要配置个比较大的值。
    keepalive_requests 100000;

    # allow the server to close the connection after a client stops responding. Frees up socket-associated memory.
    # 允许服务器在客户端停止发送应答之后关闭连接,以便释放连接相应的 socket 内存开销。
    reset_timedout_connection on;

    # send the client a "request timed out" if the body is not loaded by this time. Default 60.
    # 配置客户端数据请求超时时间,默认是 60 秒。
    client_body_timeout 10;

    # If the client stops reading data, free up the stale client connection after this much time. Default 60.
    # 客户端数据读超时配置,客户端停止读取数据,超时时间后断开相应连接,默认是 60 秒。
    send_timeout 2;

    # Compression. Reduces the amount of data that needs to be transferred over the network
    # 压缩参数配置,减少在网络上所传输的数据量。
    gzip on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6].";

################################################################################################
   fastcgi_connect_timeout 300;
   fastcgi_send_timeout 300;
   fastcgi_read_timeout 300;
   fastcgi_buffer_size 16k;
   fastcgi_buffers 16 16k;
   fastcgi_busy_buffers_size 16k;
   fastcgi_temp_file_write_size 16k;
   fastcgi_cache TEST;
   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的几个指令:
 

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;

这个指令为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。

fastcgi_connect_timeout 300;

指定连接到后端FastCGI的超时时间。

fastcgi_send_timeout 300;

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

fastcgi_read_timeout 300;

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

fastcgi_buffer_size 16k;

指定读取FastCGI应答第一部分 需要用多大的缓冲区,这里可以设置为fastcgi_buffers指令指定的缓冲区大小,上面的指令指定它将使用1个 16k的缓冲区去读取应答的第一部分,即应答头,其实这个应答头一般情况下都很小(不会超过1k),但是你如果在fastcgi_buffers指令中指 定了缓冲区的大小,那么它也会分配一个fastcgi_buffers指定的缓冲区大小去缓存。

fastcgi_buffers 16 16k;

指定本地需要用多少和多大的缓冲区来 缓冲FastCGI的应答,如上所示,如果一个php脚本所产生的页面大小为256k,则会为其分配16个16k的缓冲区来缓存,如果大于256k,增大 于256k的部分会缓存到fastcgi_temp指定的路径中, 当然这对服务器负载来说是不明智的方案,因为内存中处理数据速度要快于硬盘,通常这个值 的设置应该选择一个你的站点中的php脚本所产生的页面大小的中间值,比如你的站点大部分脚本所产生的页面大小为 256k就可以把这个值设置为16 16k,或者4 64k 或者64 4k,但很显然,后两种并不是好的设置方法,因为如果产生的页面只有32k,如果用4 64k它会分配1个64k的缓冲区去缓存,而如果使用64 4k它会分配8个4k的缓冲区去缓存,而如果使用16 16k则它会分配2个16k去缓存页面,这样看起来似乎更加合理。

fastcgi_busy_buffers_size 32k;

这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。

fastcgi_temp_file_write_size 32k;

在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。

fastcgi_cache TEST

开启FastCGI缓存并且为其制定一个名称。个人感觉开启缓存非常有用,可以有效降低CPU负载,并且防止502错误。但是这个缓存会引起很多问题,因为它缓存的是动态页面。具体使用还需根据自己的需求。

fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;

为指定的应答代码指定缓存时间,如上例中将200,302应答缓存一小时,301应答缓存1天,其他为1分钟。

fastcgi_cache_min_uses 1;

缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数,如上例,如果在5分钟内某文件1次也没有被使用,那么这个文件将被移除。

fastcgi_cache_use_stale error timeout invalid_header http_500;

不知道这个参数的作用,猜想应该是让nginx知道哪些类型的缓存是没用的。
################################################################################################

附件有调好的配置。

时间: 2024-08-02 11:49:39

Nginx 配置详解的相关文章

nginx配置详解-url重写、反向代理、负载均衡

应用层的负载均衡 master/worker结构:一个master进程,生成一个或多个worker子进程 nginx请求的连接方式epoll 是Linux下多路复用IO接口select/poll的增强版本 select 遍历epoll无需遍历 1.nginx安装(未安装邮件服务器模块) ./configure --help查看编译选项 配置文件中路径没加/以prefix指定的路径开始./configure \--prefix=/usr \--sbin-path=/usr/sbin/nginx \

nginx配置详解

转载文章,转载自: http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html Nginx配置文件详细说明 在此记录下Nginx服务器nginx.conf的配置文件说明, 部分注释收集与网络. #运行用户 user www-data;     #启动进程,通常设置成和cpu的数量相等 worker_processes  1; #全局错误日志及PID文件 error_log  /var/log/nginx/error.l

nginx调用php-fpm出错解决方法和nginx配置详解_nginx

装完了nginx和php-5.5,配置好了nginx调用php后,就开始启动php-fpm. 使用下面的命令 复制代码 代码如下: /usr/local/php/sbin/php-fpm 就可以启动了. 在nginx的目录中创建个php的检测脚本index.php 结果在打开http://localhost/index.php 悲剧的发现居然无法打开 .查看日志文件,看了下报错原因 复制代码 代码如下: 2013/07/01 22:34:26 [error] 3214#0: *64 FastCG

Laravel的Nginx配置详解

例子  代码如下 复制代码 server {     listen 80;     server_name tool.lu;     root /var/www/html/www.111cn.net/public;     index index.html index.php;     location / {         try_files $uri $uri/ /index.php?$query_string;     }     location ~ .php$ {         f

nginx技术(2)nginx的配置详解

nginx的配置 1,启动nginx [root@centos6 nginx-1.2.9]# /usr/sbin/nginx -c /etc/nginx/nginx.conf 启动nginx [root@centos6 nginx-1.2.9]# ps -ef|grep nginx 查看进程 root 5479 1 0 04:15 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 548

Linux服务器下nginx的安全配置详解_nginx

Nginx是一个轻量级,高性能的Web服务器/反向代理和电子邮件 代理(IMAP/POP3),它可以运行在UNIX,GNU/Linux,BSD变种,MAC OS X,Solaris和Microsoft Windows上.根据Netcraft的调查数据显示,互联网上6%的域名都使用了Nginx Web服务器.Nginx是解决C10K问题的服务器之一,与传统服务器不一样,Nginx不依赖于线程处理请求,相反,它使用了一个更具扩展性的事件驱 动(异步)架构.Nginx在很多高流量网站上得到了应用,如W

CentOS6.3下nginx性能优化配置详解(1/2)

一.NGINX优化配置 1.主配置文件优化: # vi /usr/local/nginx/conf/nginx.conf ----------------------------------------- user nginx nginx; worker_processes 8; worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000; error_log  /usr/local/ngi

Nginx服务器中为网站或目录添加认证密码的配置详解_nginx

nginx可以为网站或目录甚至特定的文件设置密码认证.密码必须是crypt加密的.可以用apache的htpasswd来创建密码. 格式为: htpasswd -b -c site_pass username password site_pass为密码文件.放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址. 如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如果是centos可以执行如下来安装,

Nginx服务器中的location配置详解_nginx

语法location  [=|~|~*|^~] /uri/  {...} 规则= : 表示精确的URI匹配(有兴趣的同学可以看一下url和uri的区别) -: 表示区分大小写的正则匹配 -*:表示不区分大小写的正则匹配 !~ && !~*:表示区分大小写不匹配的正则和不区分大小写的不匹配的正则 /:通用匹配,任何请求都会匹配到 location匹配目标location匹配测试只使用请求URI的部分,而不使用参数部分.(原因:参数的写法太多,无法精确匹配) location匹配顺序多个loc