页面接口防刷 解决思路一nginx

线上环境 很多接口 如果不做缓存 可能导致有人拿到url  每秒几万次的访问后台程序,导致系统down机.

此处, nginx可以加一层缓存.   

expires起到控制页面缓存的作用,合理的配置expires可以减少很多服务器的请求
要配置expires,可以在http段中或者server段中或者location段中加入
PS. 可以限制单个接口的访问

 location ~ ^/user/ {

proxy_pass http://user.example.com;
expires 1; //1h //1d //expires max; 部分文件看需要可以永久 }
nginx wiki  http://nginx.org/en/docs/http/ngx_http_core_module.html#location

语法: expires [time|epoch|max|off]

默认值: expires off

作用域: http, server, location

使用本指令可以控制HTTP应答中的“Expires”和“Cache-Control”的头标,(起到控制页面缓存的作用)。

可以在time值中使用正数或负数。“Expires”头标的值将通过当前系统时间加上您设定的 time 值来获得。

epoch 指定“Expires”的值为 1 January, 1970, 00:00:01 GMT。

max 指定“Expires”的值为 31 December 2037 23:59:59 GMT,“Cache-Control”的值为10年。

-1 指定“Expires”的值为 服务器当前时间 -1s,即永远过期

“Cache-Control”头标的值由您指定的时间来决定:

  • 负数:Cache-Control: no-cache
  • 正数或零:Cache-Control: max-age = #, # 为您指定时间的秒数。

"off" 表示不修改“Expires”和“Cache-Control”的值

demo:

控制图片等过期时间为30天,当然这个时间可以设置的更长。具体视情况而定

比如

location~ \.(gif|jpg|jpeg|png|bmp|ico)$ {

           expires 30d;

       }

控制匹配/resource/或者/mediatorModule/里所有的文件缓存设置到最长时间

比如

      

       location ~ /(resource|mediatorModule)/ {

                root    /opt/demo;

                expires max;

        }

另附其他方案: Nginx限制某个IP同一时间段的访问次数 nginx webcache配置 http://blog.sina.com.cn/s/blog_664553710100zmxr.html
posted @ 2017-01-09 10:34  托马斯布莱克  阅读(5758)  评论(0编辑  收藏  举报