nginx的优化加固

简介:

东拚西凑了一些有用的东东,可以贴在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 

 

注:有大段代码摘自howtoforge










本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/957731,如需转载请自行联系原作者

目录
相关文章
|
5月前
|
应用服务中间件 nginx
nginx优化:URI过长或request header过大导致400或414报错
当出现URI过长或请求头过大导致400或414报错时,可以通过以下方式对Nginx进行优化: 1. 调整client_max_body_size参数:该参数用于限制请求体的大小。默认情况下,Nginx的client_max_body_size参数设置为1M。如果请求体超过这个大小,Nginx会返回400错误。您可以根据实际需求适当增加这个值,例如设置为10M或更大。 ``` http { client_max_body_size 10M; } ``` 2. 调整large_client_header_buffers参数:该参数用于调整请求头缓冲区的大
784 0
|
5月前
|
应用服务中间件 Linux PHP
|
6月前
|
缓存 安全 应用服务中间件
百度搜索:蓝易云【Nginx的搭建和优化详细教程。】
通过以上步骤,你已经成功搭建和优化了Nginx服务器。你可以根据自己的需求进行进一步的配置和调整,以满足你的网站的性能和安全需求。
261 3
|
2月前
|
JavaScript 前端开发 应用服务中间件
vue项目加载慢,Nginx页面优化
vue项目加载慢,Nginx页面优化
47 0
|
4月前
|
数据可视化 应用服务中间件 Linux
Nginx 可视化管理工具与 cpolar 配置:实现远程访问本地服务的优化
Nginx 可视化管理工具与 cpolar 配置:实现远程访问本地服务的优化
Nginx 可视化管理工具与 cpolar 配置:实现远程访问本地服务的优化
|
4月前
|
缓存 负载均衡 算法
百度搜索:蓝易云【如何优化Nginx服务进程详细。
优化Nginx服务进程是一个持续的过程,需要不断地监测和调整。建议在优化之前备份配置文件,并逐步应用和测试每个优化步骤的效果,以确保服务的稳定性和可靠性。
26 0
|
4月前
|
应用服务中间件 nginx
简单理解Nginx内存池:优化性能的关键
简单理解Nginx内存池:优化性能的关键
107 0
|
4月前
|
缓存 监控 应用服务中间件
百度搜索:蓝易云【Nginx优化教程。】
这些是一些常见的Nginx优化技巧,具体的优化策略可以根据实际情况和需求进行调整。在进行任何更改之前,请确保备份配置文件,并进行适当的测试和性能监测,以确保所做的优化不会导致任何不良影响。
22 0
|
5月前
|
缓存 负载均衡 应用服务中间件
Nginx 的优化思路,并解析网站防盗链
Nginx 的优化思路,并解析网站防盗链
120 1
|
5月前
|
缓存 应用服务中间件 Linux
深入理解Nginx工作原理及优化技巧(下)
深入理解Nginx工作原理及优化技巧