首页 > 其他 > 详细

nginx(2)、nginx常用配置

时间:2015-12-24 02:17:24      阅读:172      评论:0      收藏:0      [点我收藏+]
#user??nobody;
error_log??logs/error.log??info;?
pid????????logs/nginx.pid;
1、user  nobody:
定义Nginx运行的用户和用户组

2、error_log  logs/error.log  info:
全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] 

3、pid        logs/nginx.pid; :
进程pid文件
?
worker_processes??auto;
worker_rlimit_nofile 65535;
1、worker_processes:
nginx进程数,建议设置为当前主机的CPU总核心数,或者设置自动配置 auto


2、worker_rlimit_nofile 65535:
一个nginx进程打开的最多文件描述符数目
理论值应该是系统最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535
?
#工作模式与连接数上限
events{
? ?use?epoll;
? ?worker_connections?65535;
1、  use epoll :
参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

2、worker_connections 65535
单个进程最大连接数。
最大连接数=连接数*进程数,如果是代理后端服务器,还要再除以2。
}
#http服务器
http?{
? ? include???????mime.types; ? ? ? ? ? ? ? ? ? ? ? ? ??
????default_type??application/octet-stream;?
 1、include       mime.types:
文件扩展名与文件类型映射表

2、default_type  application/octet-stream;
默认文件类型
?
? ? #access_log??logs/access.log ?main;?
? ? #log_format??main??‘$remote_addr?-?$remote_user?[$time_local]?"$request"?‘
????#??????????????????‘$status?$body_bytes_sent?"$http_referer"?‘
????#??????????????????‘"$http_user_agent"?"$http_x_forwarded_for"‘;
?
1、access_log  logs/access.log  main
设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快


2、日志格式设定
    #log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
    #                  ‘$status $body_bytes_sent "$http_referer" ‘
    #                  ‘"$http_user_agent" "$http_x_forwarded_for"‘;
?
????sendfile ? ? ? ?on;?
????#tcp_nopush ? ??on;
????keepalive_timeout??65;
1、sendfile        on;  
开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
    
2、tcp_nopush     on;
防止网络阻塞

3、keepalive_timeout  65;
连接超时时间,单位秒
?
? ?#gzip?on;?
 #gzip模块设置
1、gzip on; #开启gzip压缩输出
2、gzip_min_length 1k; #最小压缩文件大小
3、gzip_buffers 4 16k; #压缩缓冲区
4、gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
5、gzip_comp_level 2; #压缩等级
6、gzip_types text/plain application/x-javascript text/css application/xml;
   #压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
7、gzip_vary on;
?
? ? #定义虚拟机
????server?{
? ? ? ??listen???????80;
????????server_name??localhost;
? ? ? ? charset utf-8;
1、listen       80;#监听端口
2、server_name  localhost;#定义域名
3、charset utf-8;#字符集
?
? ? ? ?#资源定位
????????location?/?{
? ? ? ? ? ? #定位的根目录
????????????root???html;
????????????index??index.html;
????????}
? ? ? ??
? ? ? ? #定义一些404,5xx页面
????????#error_page??404??????????????/404.html;
????????error_page???500?502?503?504??/50x.html;
????????location?=?/50x.html?{
????????????root???html;
????????}
? ? ? ? #图片缓存时间设置
? ? ? ? location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${
? ? ? ? ? ? ? ? ?expires 10d;
? ? ? ? }
? ? ? ? #JS和CSS缓存时间设置
? ? ? ? location ~ .*.(js|css)?${
? ? ? ? ? ?expires 1h;
? ? ? ?}
????}
}
?
参考资料:
http://www.cnblogs.com/nixi8/p/4871057.html
http://www.chinaz.com/web/2015/0424/401323_2.shtml
http://www.nginx.cn/76.html
http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html
?
?
?

nginx(2)、nginx常用配置

原文:http://haoran-10.iteye.com/blog/2265966

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!