Nginx 配置文件nginx.conf的完整配置说明

#用户 用户组
user www www;
#工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点
worker_processes 5;
#错误日志
error_log logs/error.log;
#pid文件位置
pid logs/nginx.pid;
worker_rlimit_nofile 8192;

events {
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
worker_connections 4096;
}

http {
include conf/mime.types;
#反向代理配置,可以打开proxy.conf看看
include /etc/nginx/proxy.conf;
#fastcgi配置,可以打开fastcgi.conf看看
include /etc/nginx/fastcgi.conf;

default_type application/octet-stream;
#日志的格式
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#访问日志
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#根据实际情况调整,如果server很多,就调大一点
server_names_hash_bucket_size 128; # this seems to be required for some vhosts

#这个例子是fastcgi的例子,如果用fastcgi就要仔细看
server { # php/fastcgi
listen 80;
#域名,可以有多个
server_name domain1.com www.domain1.com;
#访问日志,和上面的级别不一样,应该是下级的覆盖上级的
access_log logs/domain1.access.log main;
root html;

location / {
index index.html index.htm index.php;
}

#所有php后缀的,都通过fastcgi发送到1025端口上
#上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。
location ~ .php$ {
fastcgi_pass 127.0.0.1:1025;
}
}

#这个是反向代理的例子
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;

#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires 30d;
}

#把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
location / {
proxy_pass http://127.0.0.1:8080;
}
}

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。
upstream big_server_com {
server 127.0.0.3:8000 weight=5;
server 127.0.0.3:8001 weight=5;
server 192.168.0.1:8000;
server 192.168.0.1:8001;
}

server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;

location / {
proxy_pass http://big_server_com;
}
}
} 



    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;
  }
  }


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值