使用ngx rewrite方法简化ngx conf文件

        小菜对ngx的使用仍停留在“复杂可实现”的程度,写出的ngx配置文件,虽然可用,但让明眼人一看不禁想骂一句“oh,shit!”
        之前对rewrite的了解只停留在感性的url重写的层面上,对于为什么要进行重写没有体会。下面结合最近的项目时间谈谈对rewrite的认识
1. 对于rest化的url针对请求方式进行rewrite
e.g
RESTFUL uri—— http://example.com/user/$uid/photo
需求:
请求方式 期望处理
GET 获取某一张照片的信息
POST 添加照片
PUT 更新照片信息
DELETE 删除照片

        针对上述需求完全可以用rewrite进行处理,从而实现 不同的业务请求方式可以映射到不同的后端处理逻辑中
server{
    server_name example.com ;
    if ( $request_method = GET ){
        rewrite ^(/user/(.*)/photo)$ index.php?do=get_photo&uid=$2 break;
    }
    if ( $request_method = POST ){
        rewrite ^(/user/(.*)/photo)$ index.php?do=add_photo&uid=$2 break;
    }
    if ( $request_method = PUT ){
        rewrite ^(/user/(.*)/photo)$ index.php?do=update_photo&uid=$2    break;
    }
    if ( $request_method = DELETE ){
        rewrite ^(/user/(.*)/photo)$ index.php?do=del_photo&uid=$2   break;
    }
}

2. 金玉其外“败絮”其中
        为前端展现优雅的url,利用rewrite从url中解析出需要的参数,映射到后端逻辑进行处理
3.“漏斗”式后台处理
        不同的前端url,基于rewrite统一后端处理入口。举例说明:
        图片展现的两种url:
url 说明
http://example.com/([^/]*).jpg 图片系统存储默认生成的url
http://example.com/d/(.*)   图片系统支持用户自定义url

小菜初始的nginx规则为:
location ~ ^/d/(.*)$ {
    root           ${SRC_ROOT}/apps/fnt ;
    expires max;
    fastcgi_cache   cache_php;
    set $PREFIX "";
    if ( $request_method = HEAD ) {
        set $PREFIX "HEAD_"; 
    }                  
    fastcgi_cache_key $PREFIX$1;
    fastcgi_cache_valid 200 302 3d;
    fastcgi_cache_valid 301 1d;
    fastcgi_cache_valid any 1m;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    tcp_nodelay on;
   
    include        fastcgi_params ;
    fastcgi_pass   127.0.0.1:${CGI_PORT};
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  ${SRC_ROOT}/apps/fnt/index.php ;
    fastcgi_param  QUERY_STRING     do=d&path=$1 ;

    client_max_body_size       100m;
    fastcgi_connect_timeout 1000s;
    fastcgi_send_timeout 1000s;
    fastcgi_read_timeout 1000s;
}
 location ~ ^/([^/]*)\.(jpg|png|bmp|gif)$ {
     root           ${SRC_ROOT}/apps/fnt ;
     expires max;
     fastcgi_cache   cache_php;
     set $PREFIX "";
     if ( $request_method = HEAD ) {  
         set $PREFIX "HEAD_";    
     }
     fastcgi_cache_key $PREFIX$1;
     fastcgi_cache_valid 200 302 3d;
     fastcgi_cache_valid 301 1d;
     fastcgi_cache_valid any 1m;
     fastcgi_cache_min_uses 1;
     fastcgi_cache_use_stale error timeout invalid_header http_500;
     open_file_cache max=204800 inactive=20s;
     open_file_cache_min_uses 1;
     open_file_cache_valid 30s;
     tcp_nodelay on;

     include        fastcgi_params ;
     fastcgi_pass   127.0.0.1:${CGI_PORT};
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME  ${SRC_ROOT}/apps/fnt/index.php ;
     fastcgi_param  QUERY_STRING     key=$1&postfix=$2 ;

     client_max_body_size       100m;
     fastcgi_connect_timeout 1000s;
     fastcgi_send_timeout 1000s;
     fastcgi_read_timeout 1000s;
}

明眼人一眼就能看出,里面的冗余
在师傅的指导下利用rewrite进行修改如下:
 location ~* ^/([^/]*)\.(jpg|png|bmp|gif)$ {
    rewrite ^/([^/]*)\.(jpg|png|bmp|gif)$ /backend/?key=$1&postfix=$2 last;
 }     
 location ~ ^/d/(.*)$ {
    rewrite ^/d/(.*)$ /backend/?path=$1&do=d
 }     
 location = /backend/ {
     internal;
     root           ${SRC_ROOT}/apps/fnt ;
     set $key $arg_path; 
     if ( $key = "" ){
         set $key $arg_key;  
     }
     expires max;  
     fastcgi_cache   cache_php;  
     set $PREFIX "";
     if ( $request_method = HEAD ){ 
        set $PREFIX "HEAD_"; 
     } 
     fastcgi_cache_key $PREFIX$1; 
     fastcgi_cache_valid 200 302 3d;                                 
     fastcgi_cache_valid 301 1d;                                     
     fastcgi_cache_valid any 1m;                                     
     fastcgi_cache_min_uses 1;                                       
     fastcgi_cache_use_stale error timeout invalid_header http_500;  
     open_file_cache max=204800 inactive=20s;                        
     open_file_cache_min_uses 1;                                     
     open_file_cache_valid 30s;                                      
     tcp_nodelay on;                                                 
                                                                
     include        fastcgi_params ;
     fastcgi_pass   127.0.0.1:${CGI_PORT};                           
     fastcgi_index  index.php;                                       
     fastcgi_param  SCRIPT_FILENAME  ${SRC_ROOT}/apps/fnt/index.php ;
     fastcgi_param  QUERY_STRING     $query_string;                  
                                                                  
     client_max_body_size       100m;                                
     fastcgi_connect_timeout 1000s;                                  
     fastcgi_send_timeout 1000s;                                     
     fastcgi_read_timeout 1000s;                                     
 }                                            

是不是清爽了许多?有木有?!

猜你喜欢

转载自godlovesdog.iteye.com/blog/1314944