Chinaunix首页 | 论坛 | 博客
  • 博客访问: 999190
  • 博文数量: 361
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1759
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-22 23:18
个人简介

学海无涯 个人blog lnmps.com 新站

文章分类

全部博文(361)

文章存档

2017年(1)

2015年(2)

2014年(55)

2013年(303)

分类: LINUX

2013-12-10 13:20:49



原文地址 http://blog.csdn.net/chosen0ne/article/details/7269926

1. post方法请求静态文件

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

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

在nginx源码中,src/http/ngx_http_request.h文件中:
  1. #define NGX_HTTP_MAX_SUBREQUESTS        50  
在使用Openresty时,可以向configure脚本传参置这个限制, ./configure --with-cc-opt="-D NGX_HTTP_MAX_SUBREQUESTS=250"

3. Nginx location匹配规则

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

4. 监控Nginx的状态

需要HttpStubStatusModule模块,默认情况是不开启的,所以需要编译时,指定开启这个模块。
  1. ./configure --with-http_stub_status_modules  
nginx的配置:
  1. location /nginx_status {  
  2.   # copied from http://blog.kovyrin.net/2006/04/29/monitoring-nginx-with-rrdtool/  
  3.   stub_status on;  
  4.   access_log   off;  
  5.   allow SOME.IP.ADD.RESS;  
  6.   deny all;  
  7. }  
然后通过浏览器访问localhost/nginx_status,浏览器显示Nginx的状态
  1. Active connections: 291  
  2. server accepts handled requests  
  3.   16630948 16630948 31070465  
  4. Reading: 6 Writing: 179 Waiting: 106  

5. Nginx启用aio

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

6. 限制请求内容的大小

指令:client_max_body_size,用于设置这个值,默认是1m。context可以是http,server或者location。

7. 通过echo模块合并静态文件请求

正常html中包含多个js文件或者css文件,那么浏览器需要多次http请求才能完成这些文件的加载。比如html文件:
  1. <html>  
  2. <head>  
  3.     <script type='text/javascript' src='/static/a.js'>script>  
  4.     <script type='text/javascript' src='/static/b.js'>script>  
  5.     <script type='text/javascript' src='/static/c.js'>script>  
  6. ……  
  7. html>  
那么就需要3次请求。下面介绍echo模块实现请求合并。先修改html:
  1. <html>  
  2. <head>  
  3.     <script type='text/javascript' src='/merge?/static/a.js&/static/b.js&/static/c.js'>script>  
  4. ……  
  5. html>  
nginx配置文件:
  1. location /static/ {  
  2.     root /home/www/doc_root;  
  3. }  
  4.   
  5. location /merge {  
  6.     default_type 'text/javascript';  
  7.   
  8.     echo_foreach_split '&' $query_string;    # 将查询字符串以&分割  
  9.         echo_location_async $echo_it;        # 发送子请求到$echo_it对应的location  
  10.         echo;  
  11.     echo_end;  
  12. }  
通过这种方式可以有效减少客户端请求,降低服务器端的压力。

8. nginx开启gzip

  1. gzip    on;    # 开启gzip,默认关闭  
  2. gzip_comp_level    5;    # 压缩级别,1-9,级别越高压缩率越高,但是相应的耗cpu  
  3. gzip_min_length    1025;    # 当响应内容大小大于多少bytes后使用gzip  
  4. gzip_types    text/plain application/x-javascript application/json text/javascript text/css    # 对于什么类型的内容使用gzip 
阅读(878) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~