一个简单的配置文件
配置文件路径:/usr/local/nginx/conf/nginx.conf
#-----------------------------------core 基本模块
# 使用的用户和组
user www www;
# 指定工作进程数,通常设置和CPU数量相等
worker_processes 1;
# 可以使用 [ debug | info | notice | warn | error | crit ] 参数
#error_log logs/error.log;
#error_log logs/error.log notice;
# 指定 pid 存放的路径
#pid logs/nginx.pid;
#-----------------------------------evens 事件模块
events {
#每个worker的最大连接数
worker_connections 1024;
}
#-----------------------------------HTTP 模块
http {
#包含一个文件描述了:不同文件后缀对应的MIME,见案例分析
include mime.types;
#制定默认MIME类型为二进制字节流
default_type application/octet-stream;
#指令 access_log 指派路径、格式和缓存大小。
#access_log off;
#开启调用Linux的sendfile(),提供文件传输效率
sendfile on;
#是否允许使用socket的TCP_NOPUSH或TCP_CORK选项
#tcp_nopush on;
#指定客户端连接保持活动的超时时间,在这个时间之后,服务器会关掉连接。
keepalive_timeout 65;
#设置gzip,压缩文件
#gzip on;
#为后端服务器提供简单的负载均衡
upstream apaches {
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
#配置一台虚拟机
server {
listen 8012;
server_name localhost;
location / {
proxy_pass http://apaches;
}
}
}
== 模块介绍 ==
模块划分:
#Core 核心模块
#Events 事件模块
#HTTP HTTP模块
#Mail 邮件模块
*核心模块的常用组件
- user
- 语法: user user [group]
- 缺省值: nobody nobody
- 指定Nginx Worker进程运行用户,默认是nobody帐号。
- error_log
- 语法: error_log file [ debug | info | notice | warn | error | crit ]
- 缺省值: ${prefix}/logs/error.log
- 制定错误日志的存放位置和级别。
- include
- 语法: include file | *
- 缺省值: none
- include 指令还支持像下面配置一样的全局包含的方法,例如包含一个目录下所有以".conf"结尾的文件: include vhosts/*.conf;
- pid
- 语法: pid file
- 进程id存储文件。可以使用 kill -HUP cat /var/log/nginx.pid/ 对Nginx进行配置文件重新加载。
- worker_processes
- 语法: worker_processes number
- 缺省值: 1
- 指定工作进程数。nginx可以使用多个worker进程。
*事件模块的常用组件
- worker_connections
- 语法:worker_connections number
- 通过worker_connections和worker_proceses可以计算出maxclients: max_clients = worker_processes * worker_connections
- 作为反向代理,max_clients为: max_clients = worker_processes * worker_connections/4 ,因为浏览器访问时会通过连接池建立多个连接。
- use
- 语法:use [ kqueue | rtsig | epoll | /dev/poll | select | poll | eventport ]
- 如果在./configure的时候指定了不止一种事件模型,那么可以设置其中一个,以便告诉nginx使用哪种事件模型。默认情况下nginx会在./configure时找出最适合系统的事件模型。
- 事件模型是指Nginx处理连接的方法。
*HTTP模块的核心组件和变量
- 三个作用域:http, server, location
- server
- 语法:server {...}
- 作用域: http
- 配置一台虚拟机。
- location
- 语法: location [=|~|~*|^~] /uri/ { ... }
- 作用域: server
- 配置访问路径的处理方法。
- listen
- 语法: listen address:port [ default [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ]
- 默认值: listen 80
- 作用域: server
- 指定当前虚拟机的监听端口。
- alias
- 语法: alias file-path|directory-path;
- 作用域: location
- 该指令设置指定location使用的路径.注意它跟 root 相似,但是不改变文件的根路径,仅仅是使用文件系统路径
- root
- 语法: root path
- 默认值:root html
- 作用域:http, server, location
- alias指定的目录是准确的,root是指定目录的上级目录,并且该上级目录要含有location指定名称的同名目录。
- 区别:
- location /abc/ {
- alias /home/html/abc/;
- }
- 在这段配置下,http://test/abc/a.html就指定的是/home/html/abc/a.html。这段配置亦可改成
- location /abc/ {
- root /home/html/;
- }
- 这样,nginx就会去找/home/html/目录下的abc目录了,得到的结果是相同的。
- HTTP模块的其他基本组件将结合案例介绍。
- 变量:
- HTTP header 里边 特定HEADER的值,变量会转成小写,比如 $http_user_agent, $http_referer... header信息 "YOUR-STRANGE-HEADER: values" 能通过 $http_your_strange_header获得.
- $arg_PARAMETER
- $http_HEADER
- $query_string = $args
*邮件模块的常用组件(略)
== 常用场景配置 ==
1.多台服务器配置负载均衡
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- upstream allserver {
- #ip_hash;
- server 127.0.0.1:8083 down;
- server 127.0.0.1:8084 weight=3;
- server 127.0.0.1:8001;
- server 127.0.0.1:8002 backup;
- }
- server {
- listen 8012;
- server_name localhost;
- location / {
- proxy_pass http://allserver;
- }
- }
- }
ip_hash; nginx中的ip_hash技术能够将某个ip的请求定向到同一台后端,这样一来这个ip下的某个客户端和某个后端就能建立起稳固的session
1.down 表示单前的 server 暂时不参与负载
2.weight 默认为 1.weight 越大,负载的权重就越大。
3.backup: 其它所有的非 backup 机器 down 或者忙的时候,请求 backup机器。所以这台机器压力会最轻。
2.通过手机客户端的头信息或者请求的参数转发到不用目录
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- upstream apaches {
- server 127.0.0.1:8001;
- server 127.0.0.1:8002;
- }
- upstream tomcats {
- server 127.0.0.1:8083;
- server 127.0.0.1:8084;
- }
- server {
- listen 8012;
- server_name localhost;
- location / {
- set $ismob 0;
- # 注意if后的空格
- if ( $http_chip ~* "(NOKIA3500)|(NOKIA3200)" )
- {
- set $ismob 1;
- proxy_pass http://apaches;
- }
- if ( $http_chip ~* "(NOKIA3500)|(NOKIA3200)" )
- {
- set $ismob 1;
- proxy_pass http://tomcats;
- }
- if ( $ismob = 0 )
- {
- root /usr/local/nginx/nginx8012/html;
- }
- }
- location ~* /rewrite/testXID.jsp {
- if ( $arg_XID = "13800138000")
- {
- rewrite ^(.*)$ http://192.168.0.190:8084/testSID.jsp break;
- }
- }
- }
- }
1、正则表达式匹配,其中:
= 完全相等;
~为区分大小写匹配;
~*为不区分大小写匹配;
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配。
2、文件及目录匹配,其中:
-f和!-f用来判断是否存在文件;
-d和!-d用来判断是否存在目录;
-e和!-e用来判断是否存在文件或目录;
-x和!-x用来判断文件是否可执行。
if (-d $request_filename){ ... }
哪些地方会出现正则表达式:
1.location ~* /.(gif|jpg|png|swf|flv)${...}
2.rewrite ^(.*)$ /nginx-ie/$1 break;
正则表达式举例:
1.多目录转成参数 abc.domian.com/sort/2 => abc.domian.com/index.php?act=sort&name=abc&id=2
if ($host ~* (.*)/.domain/.com) {
set $sub_name $1;
rewrite ^/sort//(/d+)//?$ /index.php?act=sort&cid=$sub_name&id=$1 last;
}
2.目录对换 /123456/xxxx -> /xxxx?id=123456
rewrite ^/(/d+)/(.+)/ /$2?id=$1 last;
3.防盗链
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- server {
- listen 8012;
- server_name localhost;
- location / {
- root html;
- }
- location ~* ^.+/.(gif|jpg|png|swf|flv|rar|zip)$ {
- valid_referers none blocked server_names http://localhost baidu.com;
- if ($invalid_referer) {
- rewrite ^/ html/50x.html;
- }
- }
- }
- }
4.访问控制:身份验证、限制IP
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- upstream tomcats {
- server 127.0.0.1:8083;
- server 127.0.0.1:8084;
- }
- server {
- listen 8012;
- server_name localhost;
- location / {
- allow 192.168.4.8;
- deny all;
- auth_basic "index";
- auth_basic_user_file ../htpasswd;
- proxy_pass http://tomcats;
- }
- }
- }
cp /usr/local/apache/apache8001/bin/htpasswd /usr/local/bin/
/usr/local/bin/htpasswd -c htpasswd root
5.查看Nginx的运行状态
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- upstream apaches {
- server 127.0.0.1:8001;
- server 127.0.0.1:8002;
- }
- upstream tomcats {
- server 127.0.0.1:8083;
- server 127.0.0.1:8084;
- }
- server {
- listen 8012;
- server_name localhost;
- location / {
- proxy_pass http://tomcats;
- }
- location /NginxStatus {
- stub_status on;
- access_log off;
- auth_basic "NginxStatus";
- auth_basic_user_file ../htpasswd;
- }
- }
- }
== 进阶内容 ==
1.查看Nginx的运行状态
Active connections: 364
server accepts handled requests
5477919 5477919 17515830
Reading: 10 Writing: 26 Waiting: 328
意思如下:
active connections – 当前 Nginx 正处理的活动连接数。
serveraccepts handled requests -- 总共处理了 5477919 个连接 , 成功创建 5477919 次握手 (证明中间没有失败的 ), 总共处理了 17515830 个请求 ( 平均每次握手处理了 3.2 个数据请求 )。
reading -- nginx 读取到客户端的 Header 信息数。
writing -- nginx 返回给客户端的 Header 信息数。
waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
2.案例分析:
将web server由apache换为nginx后,却带来意想不到的问题.多个页面显示模块显示"正在加载中..."然后一直停顿,使用FireBug调试前端,XSL文件解析失败.但载入又是HTTP 200 的正常状态.
继续用FireBug调试,发现XSL文件下载时的HTTP响应头中,
Content-Type是oct/stream ,而在原来的apache中,是text/xml,于是修改/etc/nginx/mime.types文件.将XSL的扩展名加到xml组中.问题解决.
3. 通过系统的信号控制 Nginx
使用信号加载新的配置
平滑升级到新的二进制代码
4. 使用Nginx限制下载速率和并发数
limit_zone limit_conn limit_rate
5. 使用Nginx进行地址转发
rewrite
nginx rewrite中last和break的区别: http://blog.sina.com.cn/s/blog_4b01279a0100hd4c.html
6.Nginx Internals: Nginx源代码、内部机制的分析
http://blog.zhuzhaoyuan.com/2009/09/nginx-internals-slides-video/
== 参考资料 ==
Nginx中文文档:
http://wiki.nginx.org/NginxChs
服务器系统架构分析日志:
http://www.sudone.com/
使用 Nginx 提升网站访问速度:
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/
推荐:http://blog.csdn.net/shootyou/article/details/6093562/