---
title: "nginx.conf文件详解及调优"
author: "xpdr14xpdr"
date: "2012-01-17T03:36:44Z"
tags: []
categories: ["非技术"]
slug: "nginx-conf"
draft: false
lastmod: "2012-01-17T03:36:44Z"
id: 1352490
url: "https://xpdr14xpdr.iteye.com/blog/1352490"
---

nginx.conf文件详解及调优 
2011年10月17日
　　# Info   : The conf for nginx**　　# Author : dingtm**　　# CTime  : 2010.07.01**　　user    www www;           #运行NGINX所使用的用户和组**　　worker_processes     4;    #nginx进程数，建议按照cpu数目来指定，一般为它的倍数，每个进程消耗约10M内存**　　error_log       /data/logs/nginx/error.log  crit;**　　pid             /elain/apps/nginx/nginx.pid;**　　worker_rlimit_nofile  65535;   #nginx能打开文件的最大句柄数，最好与ulimit -n的值保持一致，使用ulimit -SHn 65535 设置**　　events {**　　use epoll;          #使用epoll的I/O模型**　　connections 20000;  #每个进程允许的最多连接数**　　worker_connections 65535;   #该值受系统进程最大打开文件数限制，需要使用命令ulimit -n 查看当前设置**　　maxclients=65535*2**　　}**　　http {**　　include mime.types;           #mine.types内定义各文件类型映像**　　types {**　　text/html  html;**　　image/gif  gif;**　　image/jpeg jpg;**　　image/png  png;**　　}**　　default_type application/octet-stream;  #设置默认类型是二进制流，若未设置时，比如未加载PHP时，是不予解析，用浏览器访问则出现下载窗口**　　server_names_hash_bucket_size 128;    #不能带单位！配置个主机时必须设置该值，否则无法运行Nginx或测试时不通过，该设置与server_names_hash_max_size 共同控制保存服务器名的HASH表，hash bucket size总是等于hash表的大小，并且是一路处理器缓存大小的倍数。若hash bucket size等于一路处理器缓存的大小，那么在查找键的时候，最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址，第二次是在存储单元中查找键 值。若报出hash max size 或 hash bucket size的提示，则我们需要增加server_names_hash_max_size的值。**　　client_header_buffer_size 128k;    #客户端请求头部的缓冲区大小，根据系统分页大小设置，分页大小可用命令getconf PAGESIZE取得**　　large_client_header_buffers 4 128k;  #4为个数，128k为大小，默认是4k。申请4个128k。当http 的URI太长或者request header过大时会报414 Request URI too large或400 bad request，这是很有可能是cookie中写入的值太大造成的，因为header中的其他参数的size一般比较固定，只有cookie可能被写入较 大的数据，这时可以调大上述两个值，相应的浏览器中cookie的字节数上限会增大。**　　client_max_body_size 8m;   #HTTP请求的BODY最大限制值，若超出此值，报413 Request Entity Too Large**　　open_file_cache max=65535 inactive=20s;  #max指定缓存数量，建议和打开文件数一致，inactive是指经过多长时间文件没被请求后删除缓存。**　　open_file_cache_valid 30s;  #指多长时间检查一次缓存的有效信息**　　open_file_cache_min_uses 1; #open_file_cache指令中的inactive参数时间内文件的最少使用次数，如果超过这个数字，文件描述符一直是在缓存中打开的，如上例， 如果有一个文件在inactive时间内一次没被使用，它将被移除。**　　server_tokens off;          #关闭错误时Nginx版本显示**　　#提高文件传输性能**　　sendfile on;                #打开系统函数sendfile（）支持**　　tcp_nopush on;              #打开linux下TCP_CORK，sendfile打开时才有效，作减少报文段的数量之用**　　keepalive_timeout 60;       #keepalive超时时间**　　tcp_nodelay on;             #打开TCP_NODELAY在包含了keepalive才有效**　　fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间**　　fastcgi_send_timeout 300;    #向FastCGI传送请求的超时时间，这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。**　　fastcgi_read_timeout 300;    #接收FastCGI应答的超时时间，这个值是指已经完成两次握手后接收FastCGI应答的超时时间。**　　fastcgi_buffer_size 64k;     #这里可以设置为fastcgi_buffers指令指定的缓冲区大小**　　fastcgi_buffers 16 16k;      #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答**　　fastcgi_busy_buffers_size 128k;  #建议为fastcgi_buffers的两倍**　　fastcgi_temp_file_write_size 128k;   #在写入fastcgi_temp_path时将用多大的数据块，默认值是fastcgi_buffers的两倍，设置上述数值设置太小时若负载上来时可能报 502 Bad Gateway**　　fastcgi_cache dingtm     #开启FastCGI缓存并且为其制定一个名称，有效降低CPU负载，并且防止502错误**　　fastcgi_cache_valid 200 302 1h;  #指定应答代码缓存时间为1小时**　　fastcgi_cache_valid 301 1d;      #1天**　　fastcgi_cache_valid any 1m;      #其它为1分钟**　　fastcgi_cache_min_uses 1;        #缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数                f**　　gzip on;                    #打开GZIP压缩，实时压缩输出数据流**　　gzip_min_length  1k;        #从Content-Length中数值获取验证，小于1K会越压越大**　　gzip_buffers  4 16k;        #以16K为单位4倍的申请内存做压缩结果流缓存**　　gzip_http_version 1.1;**　　gzip_comp_level 3;          #压缩比率1-9，1压缩比最小处理速度最快，9压缩比最大但处理最慢且耗CPU**　　gzip_types      text/plain application/x-javascript text/css application/xml;  #压缩类型**　　include   vhosts/*.conf;      #虚拟主机**　　}**　　#虚拟主机**　　server {**　　listen 80;**　　server_name  www.elain.org;     #多域名用空格隔开**　　index index.php index.html index.shtml;**　　root  /elain/data/htdocs/elain;**　　#limit_conn connlimit 20;     #限制一个IP只能最多只能发起20个连接，超过报 503 Service unavailable,可防止恶意连接**　　access_log /elain/logs/nginx/access_www.elain.org.log access;**　　error_log  /elain/logs/nginx/error_www.elain.org.log;**　　location / {**　　ssi on;                 #WEB文档根目录打开SSI支持**　　ssi_types text/html;**　　ssi_silent_errors off;  #处理SSI出错时不提示**　　}**　　location ~ .*.（gif|jpg|jpeg|png|bmp|swf）$ {**　　access_log    off;**　　expires       30d;**　　}**　　location ~ .*.（js|css）?$ {**　　expires      1h;**　　add_header Cache_Control private;**　　}**　　location ~ /.ht {**　　deny all;**　　}**　　location /NginxStatus {           #设定查看Nginx状态的地址**　　stub_status on;**　　access_log off;**　　auth_basic “NginxStatus”；     #标识**　　auth_basic_user_file conf/.htpasswd;   #网页加密，提示登录框，输入用户名和密码可查看**　　}**　　location ~ .*.（php|php5）?$ {                           #匹配文件后缀php, php5**　　#fastcgi_pass  unix:/tmp/php-cgi.sock;  #SOCKET方式转交fastcgi处理**　　fastcgi_pass  127.0.0.1:9000;           #9000端口方式fastcgi**　　fastcgi_index index.php;**　　include fastcgi_params;                 #包含fastcgi配置**　　#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;**　　}**　　}
********************************************************************************************************************************************************************************************