nginx目录学习
目录
一、 Nginx 基础知识
二、 Nginx 安装及调试
三、 Nginx Rewrite
四、 Nginx Redirect
五、 Nginx 目录自动加斜线:
六、 Nginx Locatio
七、 Nginx expire
八、 Nginx 防盗链
九、 Nginx 访问控制
十、 Nginx 日志处理
十一、Nginx Cache服务配置
十二、Nginx 负载均衡
十三、Nginx简单优化
十四、如何构建高性的LEMP
十五、Nginx监控
十六、常见问题与错误处理
1、简介
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服
务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超
过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳
定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。
更多的请见官方wiki: http://wiki.codemongers.com/
2、 Nginx的优点
ginx做为HTTP服务器,有以下几项基本特性:
1) 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.
2) 无缓存的反向代理加速,简单的负载均衡和容错.
3) FastCGI,简单的负载均衡和容错.
4) 模块化的结构。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。
如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不
需要相互等待。
5) 支持SSL 和 TLS SNI.
Nginx专为性能优化而开发,性能是其最重要的考量, 实现上非常注重效率 。它支持内核Poll模型,
能经受高负载的考验, 有报告表明能支持高达 50,000 个并发连接数。
Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,
也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到
200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与
内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS这
样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginx比lighthttpd更胜一筹。
Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不
需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。
Nginx采用master-slave模型, 能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻
塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。
Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大
的Upstream与Filter链。 Upstream为诸如reverse proxy, 与其他服务器通信模块的编写奠定了很好的
基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter
的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器
发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。
Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+),accept-filter (FreeBSD
4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能
二、 Nginx 安装及调试
1、Pcre 安装
./configure
make && make install
cd ../
2. nginx 编译安装
/configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module
--with-openssl=/usr/local/openssl
make && make install
更详细的模块定制与安装请参照官方wiki.
3、Nginx 配置文件测试:
# /usr/local/nginx/sbin/nginx -t //Debug 配置文件的关键命令需要重点撑握.
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf
yntax is ok
2008/12/16 09:08:35 [info] 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf wa
tested successfully
3、 Nginx 启动:
# /usr/local/nginx/sbin/nginx
4、 Nginx 配置文件修改重新加载:
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid `
三、 Nginx Rewrite
1. Nginx Rewrite 基本标记(flags)
last - 基本上都用这个Flag。
※相当于Apache里的[L]标记,表示完成rewrite,不再匹配后面的规则
reak - 中止Rewirte,不再继续匹配
redirect - 返回临时重定向的HTTP状态302
ermanent - 返回永久重定向的HTTP状态301
※原有的url支持正则 重写的url不支持正则
2. 正则表达式匹配,其中:
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配
3. 文件及目录匹配,其中:
-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行
3. Nginx 的一些可用的全局变量,可用做条件判断:
$args #这个变量等于请求行中的参数。
$content_length #请求头中的Content-length字段。
$content_type #请求头中的Content-Type字段。
$document_root #当前请求在root指令中指定的值。
$host #请求主机头字段,否则为服务器名称。
$http_user_agent #客户端agent信息
$http_cookie #客户端cookie信息
$limit_rate #这个变量可以限制连接速率。
$request_body_file #客户端请求主体信息的临时文件名。
$request_method #客户端请求的动作,通常为GET或POST。
$remote_addr #客户端的IP地址。
$remote_port #客户端的端口。
$remote_user #已经经过Auth Basic Module验证的用户名。
$request_filename #当前请求的文件路径,由root或alias指令与URI请求生成。
$query_string #与$args相同。
$scheme #HTTP方法(如http,https)。
$server_protocol #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr #服务器地址,在完成一次系统调用后可以确定这个值。
$server_name #服务器名称。
$server_port #请求到达服务器的端口号。
$request_uri #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri #不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri #与$uri相同。
四、 Nginx Redirect
将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到http://www.linuxtone.org
erver {
listen 80
erver_name linuxtone.org netseek.linuxtone.org
index index.html index.ph
root /data/www/wwwroot
if ($host !~ "^www\.linxtone\.org$") {
rewrite ^(.*) http://www.linuxtone.org$1 redirect
}
.....................
}
五、 Nginx 目录自动加斜线:
if (-d $request_filename){
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent
}
六、 Nginx Locatio
1.基本语法:[和上面rewrite正则匹配语法基本一致]
location [=|~|~*|^~] /uri/ { … }
~ 为区分大小写匹配
~* 为不区分大小写匹配
!~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配
示例1:
location = / {
# matches the query / only.
# 只匹配 / 查询。
}
匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配
示例2:
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何以 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
}
示例3:
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# 匹配任何以 gif、jpg 或 jpeg 结尾的请求。
}
七、 Nginx expire
1.根据文件类型判断,添加expire
# Add expires header for static content
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
root /data/www/wwwroot/
expires 1d
reak
}
}
2、根据某个目录判断,添加expire
# serve static file
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /data/www/wwwroot/dow
expires 30d
}
八、 Nginx 防盗链
1. 针对不同的文件类型
#Preventing hot linking of images and other file type
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com
if ($invalid_referer) {
rewrite ^/ http://www.linuxtone.org/images/default/logo.gif
# return 403
}
}
2. 针对不同的目录
location /img/ {
root /data/www/wwwroot/bbs/img/
valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com
if ($invalid_referer) {
rewrite ^/ http://www.linuxtone.org/images/default/logo.gif
#return 403
}
}
3. 同实现防盗链和expires的方法
#Preventing hot linking of images and other file type
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost
if ($invalid_referer) {
rewrite ^/ http://www.linuxtone.org/images/default/logo.gif
}
access_log off
root /data/www/wwwroot/
expires 1d
reak
}
九、 Nginx 访问控制
1. Nginx 身份证验证
#cd /usr/local/nginx/conf
#mkdir htpasswd
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone
#添加用户名为linuxtone
New password: (此处输入你的密码)
Re-type new password: (再次输入你的密码)
Adding password for user
http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录
下)
将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:
location ~ ^/(tongji)/ {
root /data/www/wwwroot/count
auth_basic "LT-COUNT-TongJi"
auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji
}
2. Nginx 禁止访问某类型的文件.
如,Nginx下禁止访问*.txt文件,配置方法如下.
location ~* \.(txt|doc)$ {
if (-f $request_filename) {
root /data/www/wwwroot/linuxtone/test
#rewrite …..可以重定向到某个URL
reak
}
}
方法2:
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test
deny all
}
实例:
禁止访问某个目录
location ~ ^/(WEB-INF)/ {
deny all
}
3. 使用ngx_http_access_module限制ip访问
location / {
deny 192.168.1.1
allow 192.168.1.0/24
allow 10.1.1.0/16
deny all
}
详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow
4. Nginx 下载限制并发和速率
limit_zone linuxtone $binary_remote_addr 10m
erver {
listen 80
erver_name down.linuxotne.org
index index.html index.htm index.ph
root /data/www/wwwroot/dow
#Zone limit
location / {
limit_conn linuxtone 1
limit_rate 20k
}
..........
}
只允许客房端一个线程,每个线程20k.