Nginx经验总结

openkk 12年前

1. post方法请求静态文件

默认情况下,web服务器都不允许post方法请求静态文件,会返回响应403 Not Allowed。但是有些时候确实有这种需求。可以通过配置文件来改变这种设置:在需要处理静态文件的location里这样配置即可,
    location /static/ {            root /path/to/files/;            error_page 405 =200 $uri;        }  

2. Nginx默认一次只能发送50个子请求(subrequest)

在nginx源码中,src/http/ngx_http_request.h文件中:
    #define NGX_HTTP_MAX_SUBREQUESTS        50  

在使用Openresty时,可以向configure脚本传参置这个限制, ./configure --with-cc-opt="-D NGX_HTTP_MAX_SUBREQUESTS=250"</div>

3. Nginx location匹配规则

匹配顺序:
a. 字符串匹配,和location块的顺序无关,根据uri匹配所有的location,从而得到一个匹配度最大的location。
b. 正则匹配,按照location块的顺序从前向后,如果找到匹配的location,则直接由该location处理请求。如果所有的location都不匹配,则由在字符串匹配中,匹配度最大的location处理。
匹配规则:
= /uri/   ——字符串精确匹配
^~ /uri/ ——字符串前缀匹配
~ /uri/   ——大小写区分的正则匹配
~* /uri/ ——大小写不区分的正则匹配
<div>@ /uri/ ——命名location,只用于内部重定向请求</div>
其中,如果=和^~匹配成功之后会立即停止搜索,即不再进行正则匹配。

4. 监控Nginx的状态

需要HttpStubStatusModule模块,默认情况是不开启的,所以需要编译时,指定开启这个模块。
 
./configure --with-http_stub_status_modules  
nginx的配置:</div>
    location /nginx_status {          # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/          stub_status on;          access_log   off;          allow SOME.IP.ADD.RESS;          deny all;        }  

然后通过浏览器访问localhost/nginx_status,浏览器显示Nginx的状态
</div>
    Active connections: 291        server accepts handled requests          16630948 16630948 31070465        Reading: 6 Writing: 179 Waiting: 106  
</div>

5. Nginx启用aio

</div>
默认Nginx是没有开启aio的,需要在配置编译时,加上相应选项否则启动Nginx会报错unknown directive “aio”。
./configure --with-file-aio