Nginx 配置文件nginx.conf的完整配置说明-<转>
2012-11-07 14:41
417 查看
研究这个东西好多天了...没人教,难啊...什么都得一个一个搜索。Fuck
来源v
51cto
http://blog.s135.com/tags/nginx/
中文文档:http://wiki.nginx.org/NginxChs
#用户用户组 userwwwwww; #工作进程,根据硬件调整,有人说几核cpu,就配几个,我觉得可以多一点 worker_processes5; #错误日志 error_loglogs/error.log; #pid文件位置 pidlogs/nginx.pid; worker_rlimit_nofile8192;
events{
#工作进程的最大连接数量,根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行
worker_connections4096;
}
http{
includeconf/mime.types;
#反向代理配置,可以打开proxy.conf看看
include/etc/nginx/proxy.conf;
#fastcgi配置,可以打开fastcgi.conf看看
include/etc/nginx/fastcgi.conf;
default_typeapplication/octet-stream;
#日志的格式
log_formatmain‘$remote_addr–$remote_user[$time_local]$status‘
‘”$request”$body_bytes_sent“$http_referer”‘
‘”$http_user_agent”“$http_x_forwarded_for”’;
#访问日志
access_loglogs/access.logmain;
sendfileon;
tcp_nopushon;
#根据实际情况调整,如果server很多,就调大一点
server_names_hash_bucket_size128;#thisseemstoberequiredforsomevhosts
#这个例子是fastcgi的例子,如果用fastcgi就要仔细看
server{#php/fastcgi
listen80;
#域名,可以有多个
server_namedomain1.comwww.domain1.com;
#访问日志,和上面的级别不一样,应该是下级的覆盖上级的
access_loglogs/domain1.access.logmain;
roothtml;
location/{
indexindex.htmlindex.htmindex.php;
}
#所有php后缀的,都通过fastcgi发送到1025端口上
#上面include的fastcgi.conf在此应该是有作用,如果你不include,那么就把fastcgi.conf的配置项放在这个下面。
location~.php${
fastcgi_pass127.0.0.1:1025;
}
}
#这个是反向代理的例子
server{#simplereverse-proxy
listen80;
server_namedomain2.comwww.domain2.com;
access_loglogs/domain2.access.logmain;
#静态文件,nginx自己处理
location~^/(images|javascript|js|css|flash|media|static)/{
root/var/www/virtual/big.server.com/htdocs;
#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
expires30d;
}
#把请求转发给后台web服务器,反向代理和fastcgi的区别是,反向代理后面是web服务器,fastcgi后台是fasstcgi监听进程,当然,协议也不一样。
location/{
proxy_pass
}
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。据说nginx可以根据后台响应时间调整。后台需要多个web服务器。
01 | upstreambig_server_com{ |
02 | server127.0.0.3:8000weight=5; |
03 | server127.0.0.3:8001weight=5; |
04 | server192.168.0.1:8000; |
05 | server192.168.0.1:8001; |
06 | } |
07 |
08 | server{ |
09 | listen80; |
10 | server_namebig.server.com; |
11 | access_loglogs/big.server.access.logmain; |
12 |
13 | location/{ |
14 | proxy_passhttp: //big_server_com; |
15 | } |
16 | } |
17 | } |
Nginx安置后只有一个法式文件,自己并不供给各类办理法式,它是利用参数和体系旌旗灯号机制对Nginx历程自己举行节制的。Nginx的参数包罗有如下几个:
-c:利用指定的设置装备摆设文件而不是conf目次下的nginx.conf。
-t:测试设置装备摆设文件是否准确,在运行时必要从头加载设置装备摆设的时辰,此号令很是主要,用来检测所点窜的设置装备摆设文件是否有语法错误。
-v:表现nginx版本号。
-V:表现nginx的版本号以及编译情况信息以及编译时的参数。
比方我们要测试某个设置装备摆设文件是否誊写准确,我们可以利用以下号令
sbin/nginx–t–cconf/nginx2.conf
Nginx撑持下表中的旌旗灯号:
旌旗灯号名
感化形貌
TERM,INT
快速封闭法式,中断当前正在处置的恳求
QUIT
处置完当前恳求后,封闭法式
HUP
从头加载设置装备摆设,并开启新的事情历程,封闭就的历程,此操纵不会间断恳求
USR1
从头打开日记文件,用于切换日记,比方天天天生一个新的日记文件
USR2
光滑进级可实行法式
WINCH
自在封闭事情历程
有两种体例来经由过程这些旌旗灯号去节制Nginx,第一是经由过程logs目次下的nginx.pid检察当前运行的Nginx的历程ID,经由过程
kill–XXX来节制Nginx,此中XXX便是上表中列出的旌旗灯号名。若是您的体系中只有一个Nginx历程,那您也可以经由过程
killall号令来完成,比方运行
killall–sHUPnginx来让Nginx从头加载设置装备摆设。
设置装备摆设Nginx
先来看一个现实的设置装备摆设文件:
usernobody;#事情历程的属主worker_processes4;#事情历程数,一样平常与CPU核数等同#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{useepoll;#Linux下机能最好的event模式worker_connections2048;#每个事情历程许可最大的同时毗连数}http{includemime.types;default_typeapplication/octet-stream;#log_formatmain'$remote_addr-$remote_user[$time_local]$request'#'"$status"$body_bytes_sent"$http_referer"'#'"$http_user_agent""$http_x_forwarded_for"';#access_logoff;access_loglogs/access.log;#日记文件名sendfileon;#tcp_nopushon;tcp_nodelayon;keepalive_timeout65;includegzip.conf;#集群中的全部背景办事器的设置装备摆设信息upstreamtomcats{server192.168.0.11:8080weight=10;server192.168.0.11:8081weight=10;server192.168.0.12:8080weight=10;server192.168.0.12:8081weight=10;server192.168.0.13:8080weight=10;server192.168.0.13:8081weight=10;}server{listen80;#HTTP的端口server_namelocalhost;charsetutf-8;#access_loglogs/host.access.logmain;location~^/NginxStatus/{stub_statuson;#Nginx状况监控设置装备摆设access_logoff;}location~^/(WEB-INF)/{denyall;}location~\.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)${root/opt/webapp;expires24h;}location/{proxy_passhttp://tomcats;#反向代办署理includeproxy.conf;}error_page404/html/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page502503/html/502.html;error_page500504/50x.html;location=/50x.html{roothtml;}}}
上面是一个现实网站的设置装备摆设实例,此中灰色笔墨为设置装备摆设申明。上述设置装备摆设中,起首我们界说了一个location~^/NginxStatus/,如许经由过程
Activeconnections:70serveracceptshandledrequests145538191455381919239266Reading:0Writing:3Waiting:67
activeconnections–当前Nginx正处置的勾当毗连数。
serveracceptshandledrequests—统共处置了14553819个毗连,乐成建立14553819次握手(证实中心没有失败的),统共处置了19239266个恳求(均匀每次握手处置了1.3个数据恳求)。
reading—nginx读取到客户真个Header信息数。
writing—nginx返回给客户真个Header信息数。
waiting—开启keep-alive的环境下,这个值即是active–(reading+writing),意思便是Nginx已经处置完正在等待下一次恳求指令的驻留毗连。
静态文件处置
经由过程正则表达式,我们可让Nginx辨认出各类静态文件,比方images路径下的全部恳求可以写为:
location~^/images/{root/opt/webapp/images;}
location~\.(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js|txt)${root/opt/webapp;expires24h;}
expires指令来节制其在欣赏器的缓存,以削减不需要的恳求。
expires指令可以节制HTTP应答中的“Expires”和“Cache-Control”的头标(起到节制页面缓存的感化)。您可以利用比方以下的格局来誊写Expires:
expires1January,1970,00:00:01GMT;expires60s;expires30m;expires24h;expires1d;expiresmax;expiresoff;
Nginx自己并不撑持此刻风行的JSP、ASP、PHP、PERL等动态页面,可是它可以经由过程反向代办署理将恳求发送到后真个办事器,比方Tomcat、Apache、IIS等来完成动态页面的恳求处置。前面的设置装备摆设示例中,我们起首界说了由Nginx直接处置的一些静态文件恳求后,其他全部的恳求经由过程proxy_pass指令传送给后真个办事器(在上述例子中是Tomcat)。最简略的
proxy_pass用法如下:
location/{proxy_passhttp://localhost:8080;proxy_set_headerX-Real-IP$remote_addr;}
当页面的拜候量很是大的时辰,每每必要多个应用办事器来配合负担动态页面的实行操纵,这时我们就必要利用集群的架构。Nginx经由过程
upstream指令来界说一个办事器的集群,最前面阿谁完备的例子中我们界说了一个名为tomcats的集群,这个集群中包罗了三台办事器共6个Tomcat办事。而proxy_pass指令的写法酿成了:
location/{proxy_passhttp://tomcats;proxy_set_headerX-Real-IP$remote_addr;}
总结
尽管整个法式包只有五百多K,但麻雀虽小、五脏俱全。Nginx官方供给的各类功效模块包罗万象,连系这些模块可以完备各类百般的设置装备摆设要求,比方:压缩、防盗链、集群、FastCGI、流媒体办事器、Memcached撑持、URL重写等等,更关头的是Nginx拥有Apache和其他HTTP办事器无法对比的高机能。您乃至可以在不转变原有网站的架构上,经由过程在前端引入Nginx来晋升网站的拜候速率。
本文只是简略先容了Nginx的安置以及常见的根基的设置装备摆设和利用,更多关于Nginx的信息请阅读文章背面的参考资本。在这里要很是感激我的伴侣——陈磊(chanix@msn.com),他一向在做Nginx的中文WIKI(
若是您的网站是运行在Linux下,若是您并没有利用一些很是庞大的并且确定Nginx无法完成的功效,那您应该尝尝Nginx。
相关文章推荐
- Nginx 配置文件nginx.conf的完整配置说明-<转>
- (总结)Nginx配置文件nginx.conf中文详解 <转>
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Ngin的配置文件nginx.conf完整配置说明(包括fastcgi和负载均衡设置)
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- Nginx 配置文件nginx.conf的完整配置说明
- springMVC配置文件 <context:component-scan>使用说明
- 比较完整的Nginx配置文件nginx.conf常用参数中文详解
- nginx.conf配置文件的简单说明
- Nginx(三):nginx.conf配置文件说明 【2】 文件结构和配置高亮
- nginx.conf 配置文件说明
- nginx.conf的配置文件说明
- nginx.conf的完整配置说明