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

 
사용 방법
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 
  5. } 

 
주: howtoforge 에서 큰 코드 를 따 왔 습 니 다.

좋은 웹페이지 즐겨찾기