Chinaunix首页 | 论坛 | 博客
  • 博客访问: 24199
  • 博文数量: 20
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-11 17:31
文章分类

全部博文(20)

文章存档

2012年(20)

我的朋友
最近访客

分类: LINUX

2012-10-27 13:14:19

东拚西凑了一些有用的东东,可以贴在nginx配置文件中,

功能比较杂,防盗链、防注入等等

 

 

  1.    location = /robots.txt { access_log off; log_not_found off; } 
  2.    location = /favicon.ico { access_log off; log_not_found off; } 
  3.    location ~ /\. { access_log off; log_not_found off; deny all; } 
  4.    location ~ ~$ { access_log off; log_not_found off; deny all; } 
  5.    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } 
  6.    location ~ .*\.(js|css)?$ {  expires  12h; } 
  7.  
  8.  
  9. location ~* \.(gif|jpg|png|swf|flv)$ 
  10. if ($host !=’*.yourdomain.com’) { 
  11. return 403; 
  12.  
  13.    ## Block SQL injections 
  14.    set $block_sql_injections 0; 
  15.    if ($query_string ~ "union.*select.*\(") { 
  16.        set $block_sql_injections 1; 
  17.    } 
  18.    if ($query_string ~ "union.*all.*select.*") { 
  19.        set $block_sql_injections 1; 
  20.    } 
  21.    if ($query_string ~ "concat.*\(") { 
  22.        set $block_sql_injections 1; 
  23.    } 
  24.    if ($block_sql_injections = 1) { 
  25.        return 403; 
  26.    } 
  27.  
  28.    ## Block file injections 
  29.    set $block_file_injections 0; 
  30.    if ($query_string ~ "[a-zA-Z0-9_]=http://") { 
  31.        set $block_file_injections 1; 
  32.    } 
  33.    if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { 
  34.        set $block_file_injections 1; 
  35.    } 
  36.    if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { 
  37.        set $block_file_injections 1; 
  38.    } 
  39.    if ($block_file_injections = 1) { 
  40.        return 403; 
  41.    } 
  42.  
  43.    ## Block common exploits 
  44.    set $block_common_exploits 0; 
  45.    if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { 
  46.        set $block_common_exploits 1; 
  47.    } 
  48.    if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { 
  49.        set $block_common_exploits 1; 
  50.    } 
  51.    if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { 
  52.        set $block_common_exploits 1; 
  53.    } 
  54.    if ($query_string ~ "proc/self/environ") { 
  55.        set $block_common_exploits 1; 
  56.    } 
  57.    if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") { 
  58.        set $block_common_exploits 1; 
  59.    } 
  60.    if ($query_string ~ "base64_(en|de)code\(.*\)") { 
  61.        set $block_common_exploits 1; 
  62.    } 
  63.    if ($block_common_exploits = 1) { 
  64.        return 403; 
  65.    } 
  66.  
  67.    ## Block spam 
  68.    set $block_spam 0; 
  69.    if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") { 
  70.        set $block_spam 1; 
  71.    } 
  72.    if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") { 
  73.        set $block_spam 1; 
  74.    } 
  75.    if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") { 
  76.        set $block_spam 1; 
  77.    } 
  78.    if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") { 
  79.        set $block_spam 1; 
  80.    } 
  81.    if ($block_spam = 1) { 
  82.        return 403; 
  83.    } 
  84.  
  85.    ## Block user agents 
  86.    set $block_user_agents 0; 
  87.  
  88.    # Don't disable wget if you need it to run cron jobs! 
  89.    #if ($http_user_agent ~ "Wget") { 
  90.    #    set $block_user_agents 1; 
  91.    #} 
  92.  
  93.    # Disable Akeeba Remote Control 2.5 and earlier 
  94.    if ($http_user_agent ~ "Indy Library") { 
  95.        set $block_user_agents 1; 
  96.    } 
  97.  
  98.    # Common bandwidth hoggers and hacking tools. 
  99.    if ($http_user_agent ~ "libwww-perl") { 
  100.        set $block_user_agents 1; 
  101.    } 
  102.    if ($http_user_agent ~ "GetRight") { 
  103.        set $block_user_agents 1; 
  104.    } 
  105.    if ($http_user_agent ~ "GetWeb!") { 
  106.        set $block_user_agents 1; 
  107.    } 
  108.    if ($http_user_agent ~ "Go!Zilla") { 
  109.        set $block_user_agents 1; 
  110.    } 
  111.    if ($http_user_agent ~ "Download Demon") { 
  112.        set $block_user_agents 1; 
  113.    } 
  114.    if ($http_user_agent ~ "Go-Ahead-Got-It") { 
  115.        set $block_user_agents 1; 
  116.    } 
  117.    if ($http_user_agent ~ "TurnitinBot") { 
  118.        set $block_user_agents 1; 
  119.    } 
  120.    if ($http_user_agent ~ "GrabNet") { 
  121.        set $block_user_agents 1; 
  122.    } 
  123.  
  124.    if ($block_user_agents = 1) { 
  125.        return 403; 
  126.    } 

 

使用方法

1、按照rpm的标准安装的路径,假设配置文件为/etc/nginx/nginx.conf 和/etc/nginx/conf.d/default.conf ,将上述内容保存为 /etc/nginx/nginx-extra.conf

 

2、在server 栏中加入,“ include nginx-extra.conf ” ,也就是如下格式:

  1. server { 
  2. [...] 
  3.  
  4. include nginx-extra.conf 

 

注:有大段代码摘自

本文出自 “Purple_Grape的博客” 博客,请务必保留此出处http://purplegrape.blog.51cto.com/1330104/957731

阅读(579) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~