服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2010-12-1 11:12:56

Nginx 常见应用技术指南

目 录<BR><FONT color=green><I>一、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 基础知识</I></FONT><BR><FONT color=green><I>二、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 安装及调试</I></FONT><BR><FONT color=green><I>三、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx Rewrite</I></FONT><BR><FONT color=green><I>四、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx Redirect</I></FONT><BR><FONT color=green><I>五、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 目录自动加斜线:</I></FONT><BR><FONT color=green><I>六、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx Location</I></FONT><BR><FONT color=green><I>七、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx expires</I></FONT><BR><FONT color=green><I>八、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 防盗链</I></FONT><BR><FONT color=green><I>九、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 访问控制</I></FONT><BR><FONT color=green><I>十、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx日志处理</I></FONT><BR><FONT color=green><I>十一、&nbsp; &nbsp;&nbsp;&nbsp;Nginx Cache</I></FONT><BR><FONT color=green><I>十二、&nbsp; &nbsp;&nbsp;&nbsp;</I></FONT><FONT color=green><I>Nginx负载均衡</I></FONT><BR><FONT color=green><I>十三、 </I></FONT><FONT color=green><I>&nbsp; &nbsp;&nbsp; &nbsp;Nginx简单优化</I></FONT>&nbsp; &nbsp;&nbsp; &nbsp; <BR><FONT color=green><I>十四、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;如何构建高性能的LEMP环境</I></FONT><BR><FONT color=green><I>十五、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx服务监控</I></FONT><BR><FONT color=green><I>十六、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;常见问题与错误处理.</I></FONT><BR><FONT color=green><I>十七、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;相关资源下载</I></FONT><BR><BR>【前言】:<BR><FONT size=2>编写此技术指南在于推广普及NGINX在国内的使用,更方便的帮助大家了解和掌握NGINX的一些使用技巧。本指南很多技巧来自于网络和工作中或网络上朋友们问我的问题.在此对网络上愿意分享的朋友们表示感谢和致意!欢迎大家和我一起丰富本技术指南提出更好的建议!请朋友们关注: <A href="http://www.linuxtone.org/" target=_blank>http://www.linuxtone.org</A> 技术分享社区! 互想学习共同进步!</FONT><BR><BR><STRONG>一、 Nginx 基础知识</STRONG><BR>1、简介<BR>&nbsp; &nbsp;Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。<BR>更多的请见官方wiki: <A href="http://wiki.codemongers.com/" target=_blank>http://wiki.codemongers.com/</A><BR><BR>2、 Nginx的优点<BR>nginx做为HTTP服务器,有以下几项基本特性:<BR><FONT color=green>1)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;处理静态文件,索引文件以及自动索引;打开文件描述符缓冲.</FONT><BR><FONT color=green>2)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;无缓存的反向代理加速,简单的负载均衡和容错.</FONT><BR><FONT color=green>3)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;FastCGI,简单的负载均衡和容错.</FONT><BR><FONT color=green>4)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;模块化的结构。包括gzipping, byte ranges, chunked responses, 以及 SSI-filter等filter。如果由FastCGI或其它代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。</FONT><BR><FONT color=green>5)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;支持SSL 和 TLS SNI</FONT>.<BR><BR>Nginx专为性能优化而开发,性能是其最重要的考量, 实现上非常注重效率 。它支持内核Poll模型,能经受高负载的考验, 有报告表明能支持高达 50,000 个并发连接数。<BR>Nginx具有很高的稳定性。其它HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,也很可能会导致服务器物理内存耗尽频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。nginx官方表示保持10,000个没有活动的连接,它只占2.5M内存,所以类似DOS这样的攻击对nginx来说基本上是毫无用处的。就稳定性而言, nginx比lighthttpd更胜一筹。<BR>Nginx支持热部署。它的启动特别容易, 并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下,对软件版本进行进行升级。<BR>Nginx采用master-slave模型, 能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。<BR>Nginx代码质量非常高,代码很规范, 手法成熟, 模块扩展也很容易。特别值得一提的是强大的Upstream与Filter链。 Upstream为诸如reverse proxy, 与其他服务器通信模块的编写奠定了很好的基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter的输出做为当前filter的输入,这有点像Unix的管线。这意味着,一个模块可以开始压缩从后端服务器发送过来的请求,且可以在模块接收完后端服务器的整个请求之前把压缩流转向客户端。<BR>Nginx采用了一些os提供的最新特性如对sendfile (Linux 2.2+),accept-filter (FreeBSD 4.1+),TCP_DEFER_ACCEPT (Linux 2.4+) 的支持,从而大大提高了性能<BR><BR><STRONG>二、 Nginx 安装及调试</STRONG><BR>1、Pcre 安装
<DIV class=blockcode>
<DIV id=code0>./configure<BR>&nbsp; &nbsp;make &amp;&amp; make install<BR>&nbsp; &nbsp;cd ../</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>2.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;nginx 编译安装
<DIV class=blockcode>
<DIV id=code1>./configure --user=www --group=www --prefix=/usr/local/nginx/ --with-http_stub_status_module --with-openssl=/usr/local/openssl <BR>make &amp;&amp; make install</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>更详细的模块定制与安装请参照官方wiki. <BR><BR>3、Nginx 配置文件测试:
<DIV class=blockcode>
<DIV id=code2># /usr/local/nginx/sbin/nginx -t&nbsp;&nbsp;//Debug 配置文件的关键命令需要重点撑握.<BR><BR>2008/12/16 09:08:35 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok <BR>2008/12/16 09:08:35 28412#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully </DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>3、Nginx 启动:
<DIV class=blockcode>
<DIV id=code3># /usr/local/nginx/sbin/nginx </DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>4、Nginx 配置文件修改重新加载:
<DIV class=blockcode>
<DIV id=code4># kill -HUP `cat /usr/local/nginx/logs/nginx.pid</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>`<BR><STRONG>三、Nginx Rewrite</STRONG><BR><BR>1.&nbsp;&nbsp;Nginx Rewrite 基本标记(flags)<BR><FONT color=green><I>last - 基本上都用这个Flag。</I></FONT><BR><FONT color=green><I>※相当于Apache里的标记,表示完成rewrite,不再匹配后面的规则</I></FONT><BR><FONT color=green><I>break - 中止Rewirte,不再继续匹配</I></FONT><BR><FONT color=green><I>redirect - 返回临时重定向的HTTP状态302</I></FONT><BR><FONT color=green><I>permanent - 返回永久重定向的HTTP状态301</I></FONT><BR>&nbsp; &nbsp;&nbsp; &nbsp;<STRONG>※原有的url支持正则&nbsp;&nbsp;重写的url不支持正则</STRONG><BR><BR>2.&nbsp;&nbsp;正则表达式匹配,其中:<BR><I>&nbsp; &nbsp; * ~&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;为区分大小写匹配</I><BR><I>&nbsp; &nbsp; * ~*&nbsp; &nbsp;&nbsp; &nbsp; 为不区分大小写匹配</I><BR><I>&nbsp; &nbsp; * !~和!~*&nbsp; &nbsp;分别为区分大小写不匹配及不区分大小写不匹配</I><BR><BR>3. 文件及目录匹配,其中:<BR>&nbsp;&nbsp;<I> * -f和!-f用来判断是否存在文件</I><BR><I>&nbsp; &nbsp; * -d和!-d用来判断是否存在目录</I><BR><I>&nbsp; &nbsp; * -e和!-e用来判断是否存在文件或目录</I><BR><I>&nbsp; &nbsp; * -x和!-x用来判断文件是否可执行</I><BR><BR><BR>3.&nbsp;&nbsp;Nginx 的一些可用的全局变量,可用做条件判断:
<DIV class=blockcode>
<DIV id=code5>$args<BR>$content_length<BR>$content_type<BR>$document_root<BR>$document_uri<BR>$host<BR>$http_user_agent<BR>$http_cookie<BR>$limit_rate<BR>$request_body_file<BR>$request_method<BR>$remote_addr<BR>$remote_port<BR>$remote_user<BR>$request_filename<BR>$request_uri<BR>$query_string<BR>$scheme<BR>$server_protocol<BR>$server_addr<BR>$server_name<BR>$server_port<BR>$uri</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR><BR><STRONG>四、 Nginx Redirect</STRONG><BR>将所有linuxtone.org与netseek.linuxtone.org域名全部自跳转到<A href="http://www.linuxtone.org/" target=_blank>http://www.linuxtone.org</A><BR>
<DIV class=blockcode>
<DIV id=code6>server<BR>{<BR>listen 80;<BR>server_name linuxtone.org netseek.linuxtone.org;<BR>index index.html index.php;<BR>root /data/www/wwwroot;<BR>if ($host !~ "^www\.linxtone\.org$") {<BR>rewrite ^(.*) http://www.linuxtone.org$1 redirect;<BR>}<BR>........................<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR><STRONG>五、 Nginx 目录自动加斜线:</STRONG>
<DIV class=blockcode>
<DIV id=code7>if (-d $request_filename){<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;<BR>&nbsp; &nbsp;&nbsp; &nbsp;}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><STRONG>六&nbsp;&nbsp;Nginx Location</STRONG><BR><BR>1.基本语法:[和上面rewrite正则匹配语法基本一致]<BR><STRONG>location [=|~|~*|^~] /uri/ { … }</STRONG><BR>&nbsp; &nbsp; <FONT color=green><I>* ~&nbsp;&nbsp;为区分大小写匹配</I></FONT><BR><FONT color=green><I>&nbsp; &nbsp; * ~* 为不区分大小写匹配</I></FONT><BR><FONT color=green><I>&nbsp; &nbsp; * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配</I></FONT><BR><BR>示例1:<BR><I>location = / {</I><BR><I># matches the query / only.</I><BR><I># 只匹配 / 查询。</I><BR><I>}</I><BR><STRONG>匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配</STRONG><BR><BR>示例2:<BR><I>location ^~ /images/ {</I><BR><I># matches any query beginning with /images/ and halts searching,</I><BR><I># so regular expressions will not be checked.</I><BR># 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。<BR><BR>示例3:<BR><FONT color=green><I>location ~* \.(gif|jpg|jpeg)$ {</I></FONT><BR><FONT color=green><I># matches any request ending in gif, jpg, or jpeg. However, all</I></FONT><BR><FONT color=green><I># requests to the /images/ directory will be handled by </I></FONT><BR><FONT color=green><I>}</I></FONT><BR><STRONG># 匹配任何已 gif、jpg 或 jpeg 结尾的请求。</STRONG><BR><BR><BR><STRONG>七、 Nginx expires</STRONG><BR><BR>1.根据文件类型expires
<DIV class=blockcode>
<DIV id=code8># Add expires header for static content<BR>location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {<BR>&nbsp; &nbsp;&nbsp;&nbsp;if (-f $request_filename) {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;root /data/www/wwwroot/bbs;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;expires&nbsp; &nbsp;&nbsp; &nbsp;1d;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;break;<BR>&nbsp; &nbsp;&nbsp;&nbsp;}<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>2、根据判断某个目录
<DIV class=blockcode>
<DIV id=code9># serve static files<BR>&nbsp;&nbsp;location ~ ^/(images|javascript|js|css|flash|media|static)/&nbsp;&nbsp;{<BR>&nbsp;&nbsp;root&nbsp; &nbsp; /data/www/wwwroot/down;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;expires 30d;<BR>&nbsp; &nbsp;}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><STRONG>八、&nbsp;&nbsp;Nginx 防盗链</STRONG><BR><BR>1.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;针对不同的文件类型
<DIV class=blockcode>
<DIV id=code10>#Preventing hot linking of images and other file types<BR>location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost baidu.com;<BR>if ($invalid_referer) {<BR>&nbsp; &nbsp;&nbsp; &nbsp; rewrite&nbsp; &nbsp;^/&nbsp; &nbsp;;<BR>&nbsp; &nbsp;&nbsp; &nbsp;# return&nbsp; &nbsp;403;<BR>&nbsp; &nbsp;&nbsp; &nbsp; }<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>2.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;针对不同的目录
<DIV class=blockcode>
<DIV id=code11>location /img/ {<BR>&nbsp; &nbsp;&nbsp;&nbsp;root /data/www/wwwroot/bbs/img/;<BR>&nbsp; &nbsp;&nbsp;&nbsp;valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;<BR>&nbsp; &nbsp;&nbsp;&nbsp;if ($invalid_referer) {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;rewrite&nbsp;&nbsp;^/&nbsp;&nbsp;;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;#return&nbsp; &nbsp;403;<BR>&nbsp; &nbsp;&nbsp;&nbsp;}<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>3.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;同实现防盗链和expires的方法
<DIV class=blockcode>
<DIV id=code12>#Preventing hot linking of images and other file types<BR>location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;valid_referers none blocked server_names *.linuxtone.org linuxtone.org http://localhost ;<BR>if ($invalid_referer) {<BR>&nbsp; &nbsp;&nbsp; &nbsp; rewrite&nbsp; &nbsp;^/&nbsp; &nbsp;;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<BR>&nbsp; &nbsp;&nbsp; &nbsp;access_log off;<BR>&nbsp; &nbsp;&nbsp; &nbsp;root /data/www/wwwroot/bbs;<BR>expires 1d;<BR>&nbsp; &nbsp;&nbsp; &nbsp;break;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><STRONG>九、 Nginx 访问控制</STRONG><BR><BR>1.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 身份证验证
<DIV class=blockcode>
<DIV id=code13>#cd /usr/local/nginx/conf<BR>#mkdir htpasswd<BR>/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone <BR>#添加用户名为linuxtone<BR>New password:&nbsp; &nbsp;(此处输入你的密码)<BR>Re-type new password:&nbsp; &nbsp;(再次输入你的密码)<BR>Adding password for user<BR>http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录下)<BR>将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:<BR>location ~ ^/(tongji)/&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;root&nbsp; &nbsp; /data/www/wwwroot/count;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; auth_basic&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;"LT-COUNT-TongJi";<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; auth_basic_user_file&nbsp;&nbsp;/usr/local/nginx/conf/htpasswd/tongji;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>2.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 禁止访问某类型的文件.<BR>如,Nginx下禁止访问*.txt文件,配置方法如下.
<DIV class=blockcode>
<DIV id=code14>location ~* \.(txt|doc)$ {<BR>&nbsp; &nbsp; if (-f $request_filename) {<BR>&nbsp; &nbsp; root /data/www/wwwroot/linuxtone/test;<BR>&nbsp; &nbsp; #rewrite …..可以重定向到某个URL<BR>&nbsp; &nbsp; break;<BR>&nbsp; &nbsp; }<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>方法2:
<DIV class=blockcode>
<DIV id=code15>location ~* \.(txt|doc)${<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;root /data/www/wwwroot/linuxtone/test;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;deny all;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>实例:<BR>禁止访问某个目录
<DIV class=blockcode>
<DIV id=code16>location ~ ^/(WEB-INF)/ { <BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; deny all; <BR>}&nbsp;&nbsp;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>3.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;使用ngx_http_access_module限制ip访问
<DIV class=blockcode>
<DIV id=code17>location / {<BR>&nbsp; &nbsp;&nbsp;&nbsp;deny&nbsp; &nbsp; 192.168.1.1;<BR>&nbsp; &nbsp;&nbsp;&nbsp;allow&nbsp; &nbsp;192.168.1.0/24;<BR>&nbsp; &nbsp;&nbsp;&nbsp;allow&nbsp; &nbsp;10.1.1.0/16;<BR>&nbsp; &nbsp;&nbsp;&nbsp;deny&nbsp; &nbsp; all;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>详细参见wiki: <A href="http://wiki.codemongers.com/NginxHttpAccessModule#allow" target=_blank>http://wiki.codemongers.com/NginxHttpAccessModule#allow</A><BR><BR>4.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 下载限制并发和速率
<DIV class=blockcode>
<DIV id=code18>limit_zone&nbsp; &nbsp;linuxtone&nbsp;&nbsp;$binary_remote_addr&nbsp;&nbsp;10m;<BR>server<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; listen&nbsp; &nbsp;&nbsp; &nbsp; 80;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; server_name&nbsp;&nbsp;down.linuxotne.org;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; index index.html index.htm index.php;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; root&nbsp; &nbsp;/data/www/wwwroot/down;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; #Zone limit<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; location / {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;limit_conn&nbsp; &nbsp;linuxtone&nbsp;&nbsp;1;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;limit_rate&nbsp;&nbsp;20k;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; }<BR>..........<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>只允许客房端一个线程,每个线程20k.<BR>【注】<FONT color=green>limit_zone&nbsp; &nbsp;linuxtone&nbsp;&nbsp;$binary_remote_addr&nbsp;&nbsp;10m; 这个可以定义在主的</FONT><BR><BR><BR>5.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 实现Apache一样目录列表
<DIV class=blockcode>
<DIV id=code19>location&nbsp;&nbsp;/&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp;&nbsp;autoindex&nbsp;&nbsp;on;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>6.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;上文件大小限制<BR>主配置文件里加入如下,具体大小根据你自己的业务做调整。<BR><FONT color=green>client_max_body_size 10m;&nbsp; &nbsp;&nbsp; &nbsp; </FONT>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<BR><BR>十、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 日志处理<BR><BR>1.Nginx 日志切割<BR><FONT color=green>#contab -e</FONT><BR><FONT color=green>59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2&gt;&amp;1</FONT><BR><FONT color=green># cat /usr/local/sbin/logcron.sh</FONT>
<DIV class=blockcode>
<DIV id=code20>#!/bin/bash<BR>log_dir="/data/logs"<BR>time=`date +%Y%m%d`&nbsp;&nbsp;<BR>/bin/mv&nbsp;&nbsp;${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log<BR>kill -USR1 `cat&nbsp;&nbsp;/var/run/nginx.pid`</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>更多的日志分析与处理就关注(同时欢迎你参加讨论):<A href="http://bbs.linuxtone.org/forum-8-1.html" target=_blank>http://bbs.linuxtone.org/forum-8-1.html</A><BR><BR>2.利用AWSTATS分析NGINX日志<BR>&nbsp;&nbsp;设置好Nginx日志格式,仍后利用awstats进行分析.<BR>请参考: <A href="http://bbs.linuxtone.org/thread-56-1-1.html" target=_blank>http://bbs.linuxtone.org/thread-56-1-1.html</A><BR><BR>3.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 如何不记录部分日志<BR>日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了<BR>location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$<BR>{<BR>&nbsp; &nbsp;&nbsp;&nbsp;access_log off;<BR>}<BR><BR><STRONG>十一、Nginx Cache服务配置</STRONG><BR><BR>如果需要将文件缓存到本地,则需要增加如下几个子参数:
<DIV class=blockcode>
<DIV id=code21>proxy_store on;<BR>proxy_store_access user:rw group:rw all:rw;<BR>proxy_temp_path 缓存目录;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>其中,<BR>proxy_store on用来启用缓存到本地的功能,<BR>proxy_temp_path用来指定缓存在哪个目录下,如:proxy_temp_path html;<BR><BR>在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取文件,为了避免去远端拉取文件,必须修改
<DIV class=blockcode>
<DIV id=code22>proxy_pass:<BR>if ( !-e $request_filename) {<BR>&nbsp; &nbsp;&nbsp;&nbsp;proxy_pass&nbsp;&nbsp;http://mysvr;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下不存在时,再向后端拉取。<BR><BR>&nbsp; &nbsp;<BR>更多更高级的应用可以研究ncache,详细请参照<A href="http://bbs.linuxtone.org/" target=_blank>http://bbs.linuxtone.org</A> 里ncache相关的贴子.<BR><BR><STRONG>十二、Nginx 负载均衡</STRONG><BR>1. Nginx 负载均衡基础知识<BR>nginx的upstream目前支持4种方式的分配<BR><FONT color=seagreen>1)、轮询(默认)</FONT><BR><FONT color=seagreen>每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。</FONT><BR><FONT color=seagreen>2)、weight</FONT><BR><FONT color=seagreen>指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。</FONT><BR><FONT color=seagreen>2)、ip_hash</FONT><BR><FONT color=seagreen>每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。</FONT><BR><FONT color=seagreen>3)、fair(第三方)</FONT><BR><FONT color=seagreen>按后端服务器的响应时间来分配请求,响应时间短的优先分配。 </FONT><BR><FONT color=seagreen>4)、url_hash(第三方)</FONT><BR><BR>2.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 负载均衡实例1
<DIV class=blockcode>
<DIV id=code23>upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态<BR>&nbsp; &nbsp;&nbsp;&nbsp;server 127.0.0.1:9090 down;<BR>&nbsp; &nbsp;&nbsp;&nbsp;server 127.0.0.1:8080 weight=2;<BR>&nbsp; &nbsp;&nbsp;&nbsp;server 127.0.0.1:6060;<BR>&nbsp; &nbsp;&nbsp;&nbsp;server 127.0.0.1:7070 backup;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>在需要使用负载均衡的server中增加<BR>proxy_pass <A href="http://bbs.linuxtone.org/" target=_blank>http://bbs.linuxtone.org/</A>;<BR><BR>每个设备的状态设置为:<BR><FONT color=seagreen>a)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;down 表示单前的server暂时不参与负载</FONT><BR><FONT color=seagreen>b)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;weight 默认为1.weight越大,负载的权重就越大。</FONT><BR><FONT color=seagreen>c)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误</FONT><BR><FONT color=seagreen>d)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;fail_timeout:max_fails次失败后,暂停的时间。</FONT><BR><FONT color=seagreen>e)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。</FONT><BR><BR>nginx支持同时设置多组的负载均衡,用来给不用的server来使用。<BR><BR>client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug<BR>client_body_temp_path 设置记录文件的目录 可以设置最多3层目录<BR>location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡<BR><BR><BR>3.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;Nginx 负载均衡实例 2<BR>按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,也可以用作提高Squid缓存命中率.<BR><BR>简单的负载均等实例:<BR>#vi nginx.conf&nbsp;&nbsp;//nginx主配置文件核心配置
<DIV class=blockcode>
<DIV id=code24>……….<BR>#loadblance my.linuxtone.org<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;upstream&nbsp;&nbsp;my.linuxtone.org&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;ip_hash;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;127.0.0.1:8080;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;192.168.169.136:8080;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;219.101.75.138:8080;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;192.168.169.117;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;192.168.169.118;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;server&nbsp; &nbsp;192.168.169.119;<BR>&nbsp; &nbsp;&nbsp; &nbsp;}<BR>…………..<BR>include&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; vhosts/linuxtone_lb.conf;<BR>………<BR># vi proxy.conf<BR>proxy_redirect off;<BR>proxy_set_header Host $host;<BR>proxy_set_header X-Real-IP $remote_addr;<BR>proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<BR>client_max_body_size 50m;<BR>client_body_buffer_size 256k;<BR>proxy_connect_timeout 30;<BR>proxy_send_timeout 30;<BR>proxy_read_timeout 60;<BR>&nbsp;&nbsp;<BR>proxy_buffer_size 4k;<BR>proxy_buffers 4 32k;<BR>proxy_busy_buffers_size 64k;<BR>proxy_temp_file_write_size 64k;<BR>proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;<BR>proxy_max_temp_file_size 128m;<BR>proxy_store on;<BR>proxy_store_access&nbsp; &nbsp;user:rw&nbsp;&nbsp;group:rw&nbsp;&nbsp;all:r;<BR>#nginx cache&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<BR>#client_body_temp_path&nbsp;&nbsp;/data/nginx_cache/client_body 1 2;<BR>proxy_temp_path /data/nginx_cache/proxy_temp 1 2;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>#vi&nbsp;&nbsp;linuxtone_lb.conf
<DIV class=blockcode>
<DIV id=code25>server<BR>&nbsp; &nbsp;&nbsp;&nbsp;{<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;listen&nbsp;&nbsp;80;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;server_name my.linuxtone.org;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;index index.php;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;root /data/www/wwwroot/mylinuxtone;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (-f $request_filename) {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if (-f $request_filename/index.php) {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;rewrite (.*) $1/index.php break;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<BR>&nbsp;&nbsp;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;error_page 403 http://my.linuxtone.org/member.php?m=user&amp;a=login;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;location / {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;if ( !-e $request_filename) {<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; proxy_pass http://my.linuxtone.org;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; break;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;include /usr/local/nginx/conf/proxy.conf;<BR>&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;}<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><STRONG><BR><BR>十三、Nginx简单优化</STRONG><BR><BR>1.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;减小nginx编译后的文件大小 (Reduce file size of nginx)<BR>默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以后只有几百K<BR>在 auto/cc/gcc,最后几行有:<BR># debug
<DIV class=blockcode>
<DIV id=code26>CFLAGS=”$CFLAGS -g”</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>注释掉或删掉这几行,重新编译即可。<BR><BR>2.&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;修改Nginx的header伪装服务器<BR>1)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;修改nginx.h
<DIV class=blockcode>
<DIV id=code27>#vi nginx-0.7.30/src/core/nginx.h<BR>#define NGINX_VERSION&nbsp; &nbsp;&nbsp; &nbsp;"1.8"<BR>#define NGINX_VER&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "LTWS/" NGINX_VERSION<BR><BR>#define NGINX_VAR&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; "NGINX"<BR>#define NGX_OLDPID_EXT&nbsp; &nbsp;&nbsp;&nbsp;".oldbin"</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>2) 修改nginx_http_header_filter_module<BR>#vi nginx-0.7.30/src/http/ngx_http_header_filter_module.c<BR>将如下
<DIV class=blockcode>
<DIV id=code28>static char ngx_http_server_string[] = "Server: nginx" CRLF;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>修改为
<DIV class=blockcode>
<DIV id=code29>static char ngx_http_server_string[] = "Server: LTWS" CRLF;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>a)&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;修改nginx_http_header_filter_module<BR>#vi nginx-0.7.30/src/http/ngx_http_special_response.c<BR>将如下:
<DIV class=blockcode>
<DIV id=code30>static u_char ngx_http_error_full_tail[] =<BR>"&lt;hr&gt;&lt;center&gt;" NGINX_VER "&lt;/center&gt;" CRLF<BR>"&lt;/body&gt;" CRLF<BR>"&lt;/html&gt;" CRLF<BR>;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>
<DIV class=blockcode>
<DIV id=code31>static u_char ngx_http_error_tail[] =<BR>"&lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt;" CRLF<BR>"&lt;/body&gt;" CRLF<BR>"&lt;/html&gt;" CRLF<BR>;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>修改为:
<DIV class=blockcode>
<DIV id=code32>static u_char ngx_http_error_full_tail[] =<BR>"&lt;center&gt; "NGINX_VER" &lt;/center&gt;" CRLF<BR>"&lt;hr&gt;&lt;center&gt;http://www.linuxtone.org&lt;/center&gt;" CRLF<BR>"&lt;/body&gt;" CRLF<BR>"&lt;/html&gt;" CRLF<BR>;<BR><BR><BR>static u_char ngx_http_error_tail[] =<BR>"&lt;hr&gt;&lt;center&gt;LTWS&lt;/center&gt;" CRLF<BR>"&lt;/body&gt;" CRLF<BR>"&lt;/html&gt;" CRLF<BR>;</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>修改后重新编译一下环境,<BR>404错误的时候显示效果图(如果没有指定错误页的话):<BR><IMG id=aimg_471 onmouseover="showMenu({'ctrlid':this.id,'pos':'12'})" title=404.png alt=404.png src="http://bbs.linuxtone.org/data/attachment/forum/day_090201/0902020020af4e729b21834ea7.png" width=298 status="2" file="data/attachment/forum/day_090201/0902020020af4e729b21834ea7.png">
<DIV style="POSITION: absolute; DISPLAY: none" id=aimg_471_menu class=tatt>
<DIV class=crly>
<DIV class=y>2009-2-2 00:20 上传</DIV><A title="404.png 下载次数:4" href="http://bbs.linuxtone.org/forum-attachment-aid-NDcxfGNmN2Q5YzEwfDEyOTExNzI5MjB8OTU3MQ%3D%3D-nothumb-yes.html" target=_blank><STRONG>下载附件</STRONG> <SPAN class=xs0><FONT size=1>(3.94 KB)</FONT></SPAN></A> </DIV>
<DIV class=mncr></DIV></DIV><BR><BR>利用curl命令查看服务器header<BR><IMG id=aimg_472 title=curl.png alt=curl.png src="http://bbs.linuxtone.org/data/attachment/forum/day_090201/0902020020be603d5e167a6db8.png" width=358 initialized="true" unselectable="true" outfunc="null" status="2" file="data/attachment/forum/day_090201/0902020020be603d5e167a6db8.png">
<DIV style="Z-INDEX: 301; POSITION: absolute; DISPLAY: none; TOP: 13021px; LEFT: 247px" id=aimg_472_menu class=tatt initialized="true" ctrlkey="aimg_472" mtype="menu" layer="1" cover="0" fade="false" cache="1">
<DIV class=crly>
<DIV class=y>2009-2-2 00:20 上传</DIV><A title="curl.png 下载次数:4" href="http://bbs.linuxtone.org/forum-attachment-aid-NDcyfGQzNmRhYWJkfDEyOTExNzI5MjB8OTU3MQ%3D%3D-nothumb-yes.html" target=_blank><STRONG>下载附件</STRONG> <SPAN class=xs0><FONT size=1>(3.02 KB)</FONT></SPAN></A> </DIV>
<DIV class=mncr></DIV></DIV><BR><BR>3.为特定的CPU指定CPU类型编译优化.<BR>默认nginx使用的GCC编译参数是-O<BR>需要更加优化可以使用以下两个参数<BR>--with-cc-opt='-O3' \<BR>--with-cpu-opt=opteron \<BR>使得编译针对特定CPU以及增加GCC的优化.<BR>此方法仅对性能有所改善并不会有很大的性能提升,供朋友们参考.<BR>CPUD类型确定: # cat /proc/cpuinfo | grep "model name"<BR>编译优化参数参考:<A href="http://en.gentoo-wiki.com/wiki/Safe_Cflags" target=_blank>http://en.gentoo-wiki.com/wiki/Safe_Cflags</A><BR>4.Tcmalloc优化Nginx 性能
<DIV class=blockcode>
<DIV id=code33># wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz<BR># tar zxvf libunwind-0.99-alpha.tar.gz<BR># cd libunwind-0.99-alpha/<BR># CFLAGS=-fPIC ./configure<BR># make CFLAGS=-fPIC<BR># make CFLAGS=-fPIC install<BR># wget http://google-perftools.googlecode.com/files/google-perftools-0.98.tar.gz<BR># tar zxvf google-perftools-0.98.tar.gz<BR># cd google-perftools-0.98/<BR># ./configure<BR># make &amp;&amp; make install<BR># echo "/usr/local/lib" &gt; /etc/ld.so.conf.d/usr_local_lib.conf<BR># ldconfig<BR># lsof -n | grep tcmalloc</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>编译nginx 加载google_perftools_module:<BR>./configure --with-google_perftools_module<BR>在主配置文件加入nginx.conf 添加:<BR>google_perftools_profiles /path/to/profile;<BR><BR>5.内核参数优化<BR># vi /etc/sysctl.conf&nbsp; &nbsp;#在末尾增加以下内容:
<DIV class=blockcode>
<DIV id=code34>net.ipv4.tcp_fin_timeout = 30<BR>net.ipv4.tcp_keepalive_time = 300<BR>net.ipv4.tcp_syncookies = 1<BR>net.ipv4.tcp_tw_reuse = 1<BR>net.ipv4.tcp_tw_recycle = 1<BR>net.ipv4.ip_local_port_range = 5000 65000</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>#使配置立即生效<BR>/sbin/sysctl -p<BR><BR><STRONG>十四、如何构建高性的LEMP</STRONG><BR>请参见: <A href="http://www.linuxtone.org/lemp/lemp.pdf" target=_blank>http://www.linuxtone.org/lemp/lemp.pdf</A><BR>1、提供完整的配置脚本下载:<A href="http://www.linuxtone.org/lemp/scripts.tar.gz" target=_blank>http://www.linuxtone.org/lemp/scripts.tar.gz</A><BR>2、提供NGINX常见配置范例含(虚拟主机,防盗链,Rewrite,访问控制,负载均衡<BR>Discuz相关程序静态化及等等),你只要稍稍修改即可线上应用。 3、将原版的xcache替换成EA,并提供相关简单调优脚本及配置文件。<BR>更多的及更新资料请关注: <A href="http://www.linuxtone.org/" target=_blank>http://www.linuxtone.org</A><BR><BR><STRONG>十五、Nginx监控</STRONG><BR>1、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;RRDTOOL+Perl脚本画图监控<BR>先安装好rrdtool ,关于rrdtool本文不作介绍,具体安装请参照linuxtone监控版块.<BR>#cd /usr/local/sbnin<BR>#wget <A href="http://blog.kovyrin.net/files/mrtg/rrd_nginx.pl.txt" target=_blank>http://blog.kovyrin.net/files/mrtg/rrd_nginx.pl.txt</A><BR>#mv rrd_nginx.pl.txt rrd_nginx.pl<BR>#chmod a+x rrd_nginx.pl<BR><BR>#vi rrd_nginx.pl&nbsp; &nbsp;//配置脚本文件设置好路径<BR>#!/usr/bin/perl<BR>use RRDs;<BR>use LWP::UserAgent;<BR><BR># define location of rrdtool databases<BR>my $rrd = '/data/www/wwwroot/nginx/rrd';<BR># define location of images<BR>my $img = '/data/www/wwwroot/nginx/html';<BR># define your nginx stats URL<BR>my $URL = "http://219.232.244.13/nginx_status";<BR>…………<BR>【注】根据自己具体的状况修改相应的路径.<BR>#crontab ?e //加入如下<BR>* * * * * /usr/local/sbin/rrd_nginx.pl<BR>重启crond后,通过配置nginx虚拟主机指到/data/www/wwwroot/nginx/html目录,通过crond自动执行perl脚本会生成很多图片.<BR>http://xxx/connections-day.png即可看到服务器状态图。<BR><BR>2、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;官方Nginx-rrd 监控服务(多虚拟主机)(推荐)<BR>网址:<A href="http://www.nginx.eu/nginx-rrd.html" target=_blank>http://www.nginx.eu/nginx-rrd.html</A><BR>此解决方案其实是基于上述监控方案的一个改进和增强,同样先安装好rrdtool这个画图工具和相应的perl模块再做如下操作:<BR># yum install perl-HTML*<BR>先建立好生成的库存和图片存放录<BR>
<DIV class=blockcode>
<DIV id=code35>#mkdir -p /data/www/wwwroot/nginx/{rrd,html}<BR><BR>#cd /usr/local/sbin<BR>#wget http://www.nginx.eu/nginx-rrd/nginx-rrd-0.1.4.tgz<BR>#tar zxvf nginx-rrd-0.1.4.tgz<BR>#cd nginx-rrd-0.1.4<BR>#cd etc/<BR>#cp nginx-rrd.conf /etc<BR>#cd etc/cron.d<BR>#cp nginx-rrd.cron /etc/cron.d<BR><BR>#cd /usr/local/src/nginx-rrd-0.1.4/html<BR># cp index.php /data/www/wwwroot/nginx/html/<BR><BR>#cd /usr/local/src/nginx-rrd-0.1.4/usr/sbin<BR>#cp * /usr/sbin/</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR>#vi /etc/nginx-rrd.conf<BR>
<DIV class=blockcode>
<DIV id=code36>#####################################################<BR>#<BR># dir where rrd databases are stored<BR>RRD_DIR="/data/www/wwwroot/nginx/rrd";<BR># dir where png images are presented<BR>WWW_DIR="/data/www/wwwroot/nginx/html";<BR># process nice level<BR>NICE_LEVEL="-19";<BR># bin dir<BR>BIN_DIR="/usr/sbin";<BR># servers to test<BR># server_utl;server_name<BR>SERVERS_URL="http://219.32.205.13/nginx_status;219.32.205.13&nbsp;&nbsp;http://www.linuxtone.org/nginx_status;www.linuxtone.org"" </DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>//根据你的具体情况做调整.<BR>SEVERS_URL 格式 http://domain1/nginx_status;domain1 http://domain2/nginx_status;domain2<BR>这种格式监控多虚拟主机连接状态:<BR>重点启crond服务,仍后通过<A href="http://219.32.205.13/nginx/html/" target=_blank>http://219.32.205.13/nginx/html/</A> 即可访问。配置过程很简单!<BR><BR>3、&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;CACTI模板监控Nginx<BR>利用Nginx_status状态来画图实现CACTI监控<BR>nginx编译时允许http_stub_status_module<BR><BR># vi /usr/local/nginx/conf/nginx.conf<BR>
<DIV class=blockcode>
<DIV id=code37>location /nginx_status {<BR>stub_status on;<BR>access_log off;<BR>allow 192.168.1.37;<BR>deny all;<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR>
<DIV class=blockcode>
<DIV id=code38># kill -HUP `cat /usr/local/nginx/logs/nginx.pid`<BR><BR># wget http://forums.cacti.net/download.php?id=12676<BR># tar xvfz cacti-nginx.tar.gz<BR># cp cacti-nginx/get_nginx_socket_status.pl /data/cacti/scripts/<BR># cp cacti-nginx/get_nginx_clients_status.pl /data/cacti/scripts/<BR># chmod 755 /data/cacti/scripts/get_nginx*</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR><BR>检测插件<BR>
<DIV class=blockcode>
<DIV id=code39># /data/cacti/scripts/get_nginx_clients_status.pl http://192.168.1.37/nginx_status</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV><BR>在cacti管理面板导入<BR>cacti_graph_template_nginx_clients_stat.xml<BR>cacti_graph_template_nginx_sockets_stat.xml<BR><BR><STRONG>十六、常见问题与错误处理</STRONG><BR>1、400 bad request错误的原因和解决办法<BR>配置nginx.conf相关设置如下.<BR>client_header_buffer_size 16k;<BR>large_client_header_buffers 4 64k;<BR>根据具体情况调整,一般适当调整值就可以。<BR><BR>2、Nginx 502 Bad Gateway错误<BR>proxy_next_upstream error timeout invalid_header http_500 http_503;<BR>或者尝试设置:<BR>large_client_header_buffers 4 32k;<BR><BR><BR>3、Nginx出现的413 Request Entity Too Large错误<BR>这个错误一般在上传文件的时候会出现,<BR>编辑Nginx主配置文件Nginx.conf,找到http{}段,添加<BR><FONT color=green>client_max_body_size 10m</FONT>; //设置多大根据自己的需求作调整.<BR>如果运行php的话这个大小client_max_body_size要和php.ini中的如下值的最大值一致或者稍大,这样就不会因为提交数据大小不一致出现的错误。<BR><FONT color=green>post_max_size = 10M</FONT><BR><FONT color=green>upload_max_filesize = 2M</FONT><BR><BR>4、解决504 Gateway Time-out(nginx)<BR>遇到这个问题是在升级discuz论坛的时候遇到的<BR>一般看来, 这种情况可能是由于nginx默认的fastcgi进程响应的缓冲区太小造成的, 这将导致fastcgi进程被挂起, 如果你的fastcgi服务对这个挂起处理的不好, 那么最后就极有可能导致504 Gateway Time-out<BR>现在的网站, 尤其某些论坛有大量的回复和很多内容的, 一个页面甚至有几百K。<BR>默认的fastcgi进程响应的缓冲区是8K, 我们可以设置大点<BR>在nginx.conf里, 加入: fastcgi_buffers 8 128k<BR>这表示设置fastcgi缓冲区为8×128k<BR>当然如果您在进行某一项即时的操作, 可能需要nginx的超时参数调大点,例如设置成60秒:send_timeout 60;<BR>只是调整了这两个参数, 结果就是没有再显示那个超时, 可以说效果不错, 但是也可能是由于其他的原因, 目前关于nginx的资料不是很多, 很多事情都需要长期的经验累计才有结果, 期待您的发现哈!<BR><BR>5、如何使用Nginx Proxy<BR>朋友一台服务器运行tomcat 为8080端口,IP:192.168.1.2:8080,另一台机器IP:192.168.1.8. 朋友想通过访问<A href="http://192.168.1.8/" target=_blank>http://192.168.1.8</A>即可访问tomcat服务.配置如下:<BR>在192.168.1.8的nginx.conf上配置如下:
<DIV class=blockcode>
<DIV id=code40>server {<BR>listen 80;<BR>server_name java.linuxtone.org<BR>location / {<BR>proxy_pass http://192.168.1.2:8080;<BR>include /usr/local/nginx/conf/proxy.conf;<BR>}<BR>}</DIV><FONT color=#336699 size=2>复制代码</FONT></DIV>
<P>6、如何关闭Nginx的LOG<BR>access_log /dev/null; error_log /dev/null;<BR></P>
<P>&nbsp;</P>
<P>来源:<A href="http://bbs.linuxtone.org/forum-viewthread-tid-1685-highlight.html">http://bbs.linuxtone.org/forum-viewthread-tid-1685-highlight.html</A></P>
页: [1]
查看完整版本: Nginx 常见应用技术指南