×
新网 > 虚机资讯 > 正文

Nginx配置文件介绍

摘要: 本文主要介绍Nginx中配置文件nginx.conf主要结构以及基本的配置说明。本文是我通过收集资料和结合自己的学习,做简单总结。

在学习nginx的时候,我们会觉得比较难,为什么呢?因为配置文件中东西太多,如果没有一个系统的了解,就容易思维混乱,理不清头绪,所以下面,介绍下nginx配置相关的一些基础信息。

timg (30).jpg

#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; #工作模式及连接数上限 events{ #epoll是多路复用IO(I/OMultiplexing)中的一种方式, #仅用于linux2.6以上内核,可以大大提高nginx的性能 useepoll; #单个后台workerprocess进程的最大并发链接数 worker_connections1024; #并发总数是worker_processes和worker_connections的乘积 #即max_clients=worker_processes*worker_connections #在设置了反向代理的情况下,max_clients=worker_processes*worker_connections/4为什么 #为什么上面反向代理要除以4,应该说是一个经验值 #根据以上条件,正常情况下的NginxServer可以应付的最大连接数为:4*8000=32000 #worker_connections值的设置跟物理内存大小有关 #因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数 #而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右 #我们来看看360M内存的VPS可以打开的文件句柄数是多少: #$cat/proc/sys/fs/file-max #输出34336 #32000<34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内 #所以,worker_connections的值需根据worker_processes进程数目和系统可以打开的最大文件总数进行适当地进行设置 #使得并发总数小于操作系统可以打开的最大文件数目 #其实质也就是根据主机的物理CPU和内存进行配置 #当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。 #ulimit-SHn65535 } http{ #设定mime类型,类型由mime.type文件定义 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_loglogs/access.logmain; #sendfile指令指定nginx是否调用sendfile函数(zerocopy方式)来输出文件, #对于普通应用,必须设为on, #如果用来进行下载等应用磁盘IO重负载应用,可设置为off, #以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfileon; #tcp_nopushon; #连接超时时间 #keepalive_timeout0; keepalive_timeout65; #开启gzip压缩 #gzipon; #设定请求缓冲 client_header_buffer_size128k; large_client_header_buffers4128k; server{ listen80; server_namelocalhost; #定义服务器的默认网站根目录位置 roothtml; #charsetkoi8-r; #设定本虚拟主机的访问日志 #access_loglogs/host.access.logmain; #location/{ #roothtml; #定义首页索引文件的名称 #indexindex.htmlindex.htm; #} #error_page404/404.html; #redirectservererrorpagestothestaticpage/50x.html #定义错误提示页面 error_page500502503504/50x.html; location=/50x.html{ roothtml; } #静态文件,nginx自己处理 #location~^/(images|javascript|js|css|flash|media|static)/{ ##过期30天,静态文件不怎么更新,过期可以设大一点, ##如果频繁更新,则可以设置得小一点。 #expires30d; #} #当请求80端口时,自动将请求交个一下端口的服务器处理,这里我做得是一个tomcat服务器。 location/{ proxy_passhttp://169.254.15.128:8090/; } #所有一下三种格式的图片,就在本机和nginx.conf文件同级的images文件夹下去查找。 location~.(gif|jpg|png)${ rootimages; } #proxythePHPscriptstoApachelisteningon127.0.0.1:80 # #location~.php${ #proxy_passhttp://127.0.0.1; #} #passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000 # #location~.php${ #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #} #denyaccessto.htaccessfiles,ifApache\'sdocumentroot #concurswithnginx\'sone # #location~/.ht{ #denyall; #} } #anothervirtualhostusingmixofIP-,name-,andport-basedconfiguration # #server{ #listen8000; #listensomename:8080; #server_namesomenamealiasanother.alias; #location/{ #roothtml; #indexindex.htmlindex.htm; #} #} #HTTPSserver # #server{ #listen443; #server_namelocalhost; #sslon; #ssl_certificatecert.pem; #ssl_certificate_keycert.key; #ssl_session_timeout5m; #ssl_protocolsSSLv2SSLv3TLSv1; #ssl_ciphersHIGH:!aNULL:!MD5; #ssl_prefer_server_cipherson; #location/{ #roothtml; #indexindex.htmlindex.htm; #} #} }

基础配置就是这些,如果想研究得更深一点,请查阅nginx官方问文档http://nginx.org/en/docs/,里面讲的非常详细。

 

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading