zoukankan      html  css  js  c++  java
  • nginx常用的请求过滤

    以下为字符串匹配操作符:
    ~  为区分大小写匹配
    ~* 为不区分大小写匹配
    !~和!~*分别为区分大小写不匹配及不区分大小写不匹配

    1: 限制某些类型的客户端的访问

    1.    
    2. location / {  
    3. if ($http_user_agent ~ MSIE) {  
    4. return 503;  
    5.   }  
    6. }#限制IE访问  

    如果把MSIE改成 Mozilla 就基本上把IE和firefox这样pc浏览器限制了
    2和3主要是针对盗链做处理


    2:针对不同的文件类型

    1.    
    2. location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {  
    3.      if ($http_referer ~* hecks.tk) {  
    4.      #rewrite ^/ http://www.hecks.tk/403.html;  
    5.      return 403;  
    6.       }  
    7. }  


    3:针对不同的目录

    1.    
    2. location /img/ {  
    3.     root /data/img/;  
    4.    if ($http_referer ~* hecks.tk) {  
    5.              rewrite  ^/  http://www.hecks.tk/images/error.gif  
    6.              #return   403;  
    7.     }  
    8. }  



    另外的一个nginx配置例子

    worker_processes 2; #工作进程数,在网上看到说最优是cpu的二倍

    1. error_log   current_path/log/nginx.error.log debug;  
    2. pid         shared_path/pids/nginx.pid;  
    3.   
    4. events {  
    5.   worker_connections 1024;#最大连接数  
    6. }  
    7.   
    8. http {  
    9.   include           /usr/local/nginx/conf/mime.types;#content type 文件  
    10.   default_type      application/octet-stream;  
    11.   
    12.   log_format  main  '$remote_addr - $remote_user [$time_local] $status '  
    13.                     '"$request" $body_bytes_sent "$http_referer" '  
    14.                     '"$http_user_agent" "$http_x_forwarded_for"';  
    15.   
    16.   access_log  current_path/log/nginx.access.log main;#log文件存放地方  
    17.   
    18.   sendfile          on;  
    19.   tcp_nopush        on;  
    20.   tcp_nodelay       on;  
    21.   keepalive_timeout 70;  
    22.   
    23.   gzip              on;  
    24.   gzip_min_length   1000;  
    25.   gzip_buffers      4 8k;  
    26.   gzip_comp_level   9;  
    27.   gzip_proxied      any;  
    28.   gzip_types        application/xml application/javascript application/x-javascript application/atom+xml application/rss+xml;  
    29.   gzip_types        text/css text/html text/javascript text/js text/plain text/xml;  
    30.   
    31.   upstream mongrel {#proxy 负载均衡配置  
    32.     server 127.0.0.1:8000;#服务器1  
    33.     server 127.0.0.1:8001;#服务器2  
    34.   }  
    35.   
    36.   server {  
    37.     listen 80;  
    38.     server_name hecks.tk www.hecks.tk;  
    39.     root current_path/public;  
    40.     index index.html index.htm;  
    41.   
    42.     location / {  
    43.       proxy_set_header  X-Real-IP  $remote_addr;  
    44.       proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  
    45.       proxy_set_header Host "www.hecks.tk";  
    46.       proxy_redirect false;  
    47.       proxy_max_temp_file_size 0;  
    48.   
    49.       # rewrite 'hecks.tk' -> 'www.hecks.tk'  
    50.       if ($host = 'hecks.tk' ) {  
    51.           rewrite  ^/(.*)$  http://www.hecks.tk/$1  permanent;  
    52.       }  
    53.      #如果静态文件存在服务器,则跳过rewrite规则  
    54.       if (-f $request_filename) {  
    55.           expires max;  
    56.           break;  
    57.       }  
    58.       # redirect feed requests to feedburner, unless its the feedburner agent  
    59.       if ($http_user_agent !~ FeedBurner) {  
    60.         rewrite ^/feed/atom.xml$ http://feeds.feedburner.com/hecks;  
    61.       }  
    62.       if (-f $request_filename/index.html) {  
    63.         expires 7d;  
    64.         rewrite (.*) $1/index.html break;  
    65.       }  
    66.       # support rails page caching  
    67.       if (-f $request_filename.html) {  
    68.         rewrite (.*) $1.html break;  
    69.       }  
    70.       # pass it onto upstream mongrel cluster  
    71.       if (!-f $request_filename) {  
    72.         proxy_pass http://www.hecks.tk;  
    73.         break;  
    74.       }  
    75.     }  
    76.   
    77.     location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov).*?$ {  
    78.       root current_path/public;  
    79.       if (!-f $request_filename) {  
    80.         proxy_pass http://www.hecks.tk;  
    81.         break;  
    82.       }  
    83.     }      
    84.   
    85.     error_page 500 502 503 504 /50x.html;  
    86.     location = /50x.html {  
    87.       root current_path/public;  
    88.     }  
    89.   }  
    90. }  
  • 相关阅读:
    Diocp截图
    [DIOCP视频]-DIOCPFileServer视频
    DIOCP 运作核心探密
    DIOCP-DIOCPv5的处理能力
    【被C折腾系列】用C调DIOCP编码客户端通信
    DIOCP-V5发布
    MyBean通用报表插件介绍
    Amazon
    输出所有大小写字符的组合
    删除最少字符生成Palindrome
  • 原文地址:https://www.cnblogs.com/qq78292959/p/2440390.html
Copyright © 2011-2022 走看看