一.nginx.conf的组成模块如下:



events{

.................

      }

http{

...............

Server{

................

              }

Server{

................

 }

...............

https{

...............

     }

}



二nginx.conf文件详解


user  nginx nginx;

定义nginx运行的用户及用户组


worker_processes  2;

nginx进程数,根据硬件调整,建议设置为等于CPU总核心数或2倍于CPU


error_log  logs/error.log;

error_log  logs/error.log  notice;

error_log  logs/error.log  info;

错误日志,存放路径


pid     logs/nginx.pid;

pid(进程标识符),存放路径

   

worker_rlimit_nofile 204800;

指定进程可以打开的最大描述符:数目。

这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。


##配置事件

  events 

{


  use epoll;

使用epoll的I/O 模型,linux建议epoll,FreeBSD建议采用kqueue,window下不指定。

补充说明:

与apache相类,nginx针对不同的操作系统,有不同的事件模型

A)标准事件模型

Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll

B)高效事件模型

Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。

Epoll:使用于Linux内核2.6版本及以后的系统。


worker_connections  1024;

个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%


keepalive_timeout 60;

keepalive超时时间


client_header_buffer_size 4k;

客户端请求头部的缓冲区大小。这个可以根据系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。

分页大小可以用命令getconf PAGESIZE 取得。

[root@web001 ~]# getconf PAGESIZE

4096

但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数


open_file_cache max=65535 inactive=60s;

这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。


open_file_cache_valid 80s;

这个是指多长时间检查一次缓存的有效信息



open_file_cache_min_uses 1;

open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。

}


##设定http服务器,利用它的反向代理功能提供负载均衡支持

http

{

include mime.types;

设定mime类型,类型由mime.type文件定义


default_type application/octet-stream;

 

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

日志格式设置。

$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;

$remote_user:用来记录客户端用户名称;

$time_local: 用来记录访问时间与时区;

$request: 用来记录请求的url与http协议;

$status: 用来记录请求状态;成功是200,

$body_bytes_sent :记录发送给客户端文件主体内容大小;

$http_referer:用来记录从哪个页面链接访问过来的;

$http_user_agent:记录客户浏览器的相关信息;

通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。


access_log  logs/host.access.log  main;

access_log  logs/host.access.404.log  log404;

用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径


sendfile on;

sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

 

tcp_nopush on;

此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用


keepalive_timeout 120;

keepalive超时时间


gzip  on;

开启gzip压缩输出


upstream server.com {
upstream的负载均衡,此处用weight

server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

补充:

upstream的负载均衡方式:

1.轮询(默认),每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉能自动剔除。这种方式简便、成本低廉;缺点是:可靠性低和负载分配不均衡,适用于图片服务器集群和纯静态页面服务器集群。

2.weight(权重),指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。

3.ip_hash(访问ip),每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

4.fair(第三方),后端服务器的响应时间来分配请求,响应时间短的优先分配,与weight分配策略类似。

5.url_hash(第三方),按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。


##设定虚拟主机

server

 {

listen       80;

监听端口


server_name  localhost;

域名可有多个,用空格隔开


charset koi8-r;

编码方式,默认为utf-8


access_log  logs/host.access.log  main;

虚拟主机的访问日志


 location / {

 root   html;

 index  index.php index.html index.htm;

        }



error_page  404              /404.html;


redirect server error pages to the static page /50x.html

error_page   500 502 503 504  /50x.html;

重定向服务器端错误到静态页面

location = /50x.html

 {

 root   html;

        }



proxy the PHP scripts to Apache listening on 127.0.0.1:80

代理php脚本到Apache

location ~ \.php$ 

{

proxy_pass   http://127.0.0.1;

 }



pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

将php脚本交给FastCGI处理

location ~ \.php$

 {

root           html;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

include        fastcgi.conf;

   }



deny access to .htaccess files, if Apache's document root concurs with nginx's one

拒绝处理所有.htxxx文件

location ~ /\.ht

 {

deny  all;

   }

    }




another virtual host using mix of IP-, name-, and port-based configuration

虚拟主机示例

server {

listen       8000;

listen       somename:8080;

server_name  somename  alias  another.alias;


location / 

{

root   html;

index  index.html index.htm;

  }



location /NginxStatus 

设定查看Nginx状态的地址

{

stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file confpasswd;

htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
 }


##本地动静分离反向代理配置

location ~ .(jsp|jspx|do)?$

所有jsp的页面均交由tomcat或resin处理

{

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_pass http://127.0.0.1:8080;

}



location ~ .*.(htm|html|gif|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$

所有静态文件由nginx直接读取不经过tomcat或resin

{ expires 15d; }

location ~ .*.(js|css)?$

{ expires 1h; }

}

}



##设定https服务器

HTTPS server


server {

listen       443 ssl;

server_name  localhost;

启用ssl加密


ssl_certificate      nginx.pem;

ssl_certificate_key  nginx.pem;

证书及密钥


ssl_session_cache    shared:SSL:1m;

ssl_session_timeout  5m;


ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;


location /

 {

root   html;

index  index.html index.htm;

       }

   }

}


更详细的模块参数请参考:http://wiki.nginx.org/Main

nginx官网:http://nginx.org/en/

j_0080.gif