jxp2002 发表于 2018-11-11 10:12:47

nginx配置汇总

  1、限制下载速度
  context:http,server,location
  例:
  location /down/ {
  root /data/webroot/yitea.cn;
  limit_rate 100k;
  limit_rate_after 100m;
  }
  注:
  limit_rate_after 100m 全速下载100MB后,速度再降至limit_rate 100KB/S;也可以只使用limit_rate;(可使用的单位有k m);子区段的配置如果与上级配置有冲突,会覆盖上级区段的配置。
  2、限制并发连接:limit_req和limit_conn
  limit_conn限制每IP并发连接数($binary_remote_addr因为使用该变量作KEY)
  例:
  http {
  limit_conn_zone $binary_remote_addr zone=perip:1m;
  limit_conn_zone $server_name zone=perserver:1m;
  ...
  server {
  limit_conn perip 2;
  limit_conn perserver 1;
  ...
  }
  }
  注:
  1、limit_conn_zone
  context:http
  语法:limit_conn_zone$variablezone=name:size;
  作用: 定义一个名为name,大小为size的会话内存池
  limit_conn
  作用:使用limit_conn_zone定义的zone
  context:http,server,location
  2、在以上事例中,定义了两个会话池,大小分别为1m;$variable的值为KEY,值不同,大小则不同,同样大小的内存池对于不同的KEY保存的最大会话(连接)数也不同。如:$binary_remote_addr为二进制远程用户IP,大小为4字节,单个会话信息为32字节。1m可以保存的最大会话数为1*1024*1024/32=32768个
  3、在上面的例子中,一个server使用了两个会话内存池,则该server受两个内存池的使用限制,虽然允许同一IP的并发连接为2,但是只能允许一个IP存在活动的链接(即使有多个server_name),因为定义的key为server_name。
  4、其它一些命令及默认值
  syntax:limit_conn_log_level info | notice | warn | error;
  default:limit_conn_log_level error;
  context:http, server, location
  This directive appeared in version 0.8.18.
  syntax:limit_conn_status code;
  default:limit_conn_status 503;
  context:http, server, location
  This directive appeared in version 1.3.15.
  5、所有设置都会继承到子区段,以最小的限制为准;子区段的配置如果与上级配置有冲突,会覆盖上级区段的配置
  多个虚拟主机可以使用同一个会话池。
  limit_req限制每IP并发连接频率($binary_remote_addr因为使用该变量作KEY)
  举例说明:
  http {
  limit_req_zone $binary_remote_addr zone=reqone:1m rate=1r/s;
  ...
  server {
  limit_req zone=reqone burst=4 nodelay;
  ...
  }
  }
  注1:
  rate只是设置了该zone的处理请求的效率;
  limit_req 使用定义的zone来限制一秒内接受到的客户最大并发请求的个数;
  可以不使用burst参数,不使用burst时,默认使用了nodelay参数,默认每秒接受一个请求,其它请求返回503;
  在该例中使用了burst=4,代表每秒接受的单IP并发为:默认值1+突发值4=5r/s;
  使用nodelay的话忽略rate的设置,这个例子中5个请求认为是合法的,直接一起处理,不延时到下一秒处理;
  不使用nodelay的话,则使用rate设定的速度,在这里rate为1r/s ,则5个并发需要5秒完成。
  注2:
  不建议burst设置的数量比实际页面(还有实际用户)同时解析的资源少,更不建议在burst数比实际用户和总资源小的情况下使用nodelay,这样的话会导致产生很多503错误。
  3、隐藏版本号
  server_tokens off;
  context:http,server,location
  注:也可以在安装上编辑源码文件,把nginx改为自己想要的server名。
  4、访问控制
  location /down/ {
  root /data/webroot/webdir;
  satisfy any;
  auth_basic "need auth";
  auth_basic_user_file /usr/local/nginx/conf/htpasswd.user;
  allow 192.168.2.26;
  deny all;
  }
  注:
  satisfy any; 满足其中任意一个条件就可以获得权限,如果ip已allow,则不弹出认证窗口。
  satisfy all; 两个条件都满足才可以获得权限,如果ip已deny,直接返回403。
  allow deny 自上而下匹配,一旦匹配就不再继续往下匹配其它规则。
  5、nginx列出目录
  打开nginx.conf文件,在location server 或 http段中加入
  autoindex on; #GMT时间、具体字节数,相当于以下两个参数同时使用:
  autoindex on;
  autoindex_exact_size on;
  如果想显示单位是kB或者MB或者GB
  autoindex_exact_size off;
  默认显示的文件时间为GMT时间
  autoindex_localtime off;
  显示的文件时间为服务器时间
  autoindex_localtime on;
  6、nginx支持pathinfo
  详见:http://sndapk.blog.51cto.com/5385144/1268721
  7、gzip压缩
  context:http,server,location,location{if}
gzip on;  
gzip_min_length 1k;
  
gzip_buffers 4 8k;
  
#gzip_http_version 1.0;
  
gzip_comp_level 5;
  
gzip_types text/plain text/xml text/css application/x-javascript;
  
gzip_disable "msie6";
  注:
  gzip_min_length测试了一下,677B压缩后为375B,网上说是小于1KB会越压越大,这里遵循先辈们的传统1k。
  #gzip_http_version 1.0; 如果nginx作为反向代理,nginx会使用http1.0与后端服务器通信,需要开启此项。
  gzip_comp_level 范围1-9,文本压缩后效果明显,设置这个级别差别不算太大。
  gzip_types 不指定参数默认压缩text/html类型;如需添加其它类型,无需指定“text/html”。
  gzip_disable "msie6" 对于xpsp2和2003sp1之前系统版本中的IE6不进行压缩(bug)。
  关于IE6版本:xpsp2和2003sp1和它之后的IE6版本,User-Agent中多了一个SV1安全标识,如下:
  xpsp2中IE6的User-Agent: MSIE 6.0; Windows NT 5.1; SV1;
  2003sp1中IE6的User-Agent:MSIE 6.0; Windows NT 5.2; SV1;
  详见:http://www.cnblogs.com/birdshome/archive/2005/07/28/202331.html
  测试:启用gzip后,响应头中没有Content-Length,压缩后的传输的文件大小可以 去日志中查看。
  # curl -I -H Accept-Encoding:gzip,deflate y.cn/1.html
  HTTP/1.1 200 OK
  Server: nginx
  Date: Thu, 12 Sep 2013 10:34:51 GMT
  Content-Type: text/html
  Last-Modified: Thu, 12 Sep 2013 10:20:29 GMT
  Connection: keep-alive
  Content-Encoding: gzip
  8、关于nginx日志
  详见:http://sndapk.blog.51cto.com/5385144/1297051
  9、缓存过期
  expires 1h; 从本次请求的时间开始计算,3600秒后缓存过期 cache-control max-age=3600
  expires modified +1h; 从修改时间开始1小时为过期,超出这个期限: cache-control max-age=no-cache
  expires @18h30m; 最近的一个18点半过期; cache-control max-age=实际的到该时间的秒数
  expires 0; cache-control max-age=0
  expires -1; cache-control no-cache
  expires epoch; cache-control no-cache
  expires max; cache-control max-age=315360000#10年
  expires off; 关闭缓存过期设置
  10、添加,改写响应头
  add_header
  语法:add_header name value
  默认:none
  context: http,server,location
  location /public/upload/ {
  expires 30d;
  add_header cache-control private; #由于设置的过期时间,cache-control已经存在,这里为必定
  add_header cache-test test; #添加了一个自定义响应头。
  }
Accept-RangesbytesCache-Controlno-cache, privateConnectionkeep-aliveContent-Length20267Content-Typeimage/jpegDateTue, 17 Sep 2013 06:12:07 GMTExpiresThu, 01 Jan 1970 00:00:01 GMTLast-ModifiedThu, 05 Sep 2013 03:05:25 GMTServernginxcache-testtest  注:
  该指令只能在响应代码为200,204,301,302,304时有效;
  不能添加last-modified头;
  不能重写已经存在的头,如server,可以使用其它指令重写
  附:cache-control头


  11、图片防盗链
  详见:http://sndapk.blog.51cto.com/5385144/1298403
  
  12、限制直接以IP访问
  
server {  
    listen 80 default_server;
  
    server_name _;
  
    return 444;
  
}
  
  
  
  
  


页: [1]
查看完整版本: nginx配置汇总