`
godlovesdog
  • 浏览: 99464 次
社区版块
存档分类
最新评论

使用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;                                     
 }                                            

是不是清爽了许多?有木有?!
分享到:
评论

相关推荐

    nginx rewrite规则怎么配置-.docx

    - `NGX_HTTP_SRV_CONF`: 在配置文件的`server`块内。 - `NGX_HTTP_SIF_CONF`: 在`server`块内的`if`语句中。 - `NGX_HTTP_LOC_CONF`: 在`location`块内。 - `NGX_HTTP_LIF_CONF`: 在`location`块内的`if`语句中...

    nginx脚本引擎与rewrite设计原理(三)

    本文将深入探讨Nginx中的一个重要组成部分——脚本引擎与rewrite设计原理,特别是针对ngx_http_core_main_conf_t结构中的两个关键成员:`cmcf->variables_keys` 和 `cmcf->variables` 的详细解析。 #### 二、核心...

    nginx-1.25.1

    4. **启动与管理**:安装完成后,使用`nginx`命令启动服务,配置文件通常位于`/etc/nginx/nginx.conf`,可以通过`systemctl`或`service`进行服务管理。 ### 配置Nginx Nginx的配置文件主要包含服务器块(server ...

    nginx1.24.0,包含GCC/zlib/prce依赖

    例如,可以添加 ngx_http_rewrite_module 实现 URL 重写,ngx_http_proxy_module 实现反向代理,或者 ngx_http_gzip_static_module 实现静态文件的 gzip 压缩。 **6. 安装与配置** 在安装 Nginx 1.24.0 时,需要...

    nginx相关配置文件

    8. **模块配置:** Nginx支持多种模块,如`ngx_http_rewrite_module`用于URL重写,`ngx_http_proxy_module`用于反向代理,`ngx_http_gzip_module`用于GZIP压缩等。每个模块都有自己的配置选项,可以在`http`、`...

    nginx开源代码

    Nginx的配置文件解析是通过`ngx_conf_parse`函数完成的,它将配置文件转换为内部数据结构,供后续处理使用。理解配置文件解析流程有助于定制和扩展Nginx配置。 4. **内存池**: Nginx使用自定义的内存池`ngx_pool...

    nginx程序启动过程

    具体的执行顺序为:`opre` -> `ocreate_main_conf` -> 执行 `ngx_command_t` 定义的函数(被视为 `init_main_conf` 的自动处理部分)-> `oinit_main_conf` -> `opostconfig`。 **2. 监听端口** - 在完成配置文件...

    nginx-1.14.2.rar

    - 配置文件:主要配置文件为`nginx.conf`,其中可以定义服务器块(server blocks),每个服务器块可以设置监听端口、代理设置、SSL配置等。 - 虚拟主机:通过配置文件中的多个服务器块实现虚拟主机,每个服务器块...

    ngnix工具包,无需安装直接使用

    Nginx支持模块化设计,可以通过安装额外的模块扩展其功能,如`ngx_http_rewrite_module`用于URL重写,`ngx_http_proxy_module`实现反向代理,`ngx_http_gzip_static_module`提供GZIP压缩静态文件等。同时,根据实际...

    nginx代码阅读

    - 最终,赋值指令被转化为代码,并存储在`ngx_http_rewrite_loc_conf_t`结构体的`code`数组中,该数组的元素类型是`ngx_http_script_value_code_t`。 综上所述,Nginx通过精心设计的监听结构、事件处理机制以及...

    nginx-1.12.0安装包和配置文件

    同时,Nginx的模块化设计使得扩展功能变得非常方便,如通过添加`ngx_http_rewrite_module`实现URL重写,或者使用`ngx_http_access_module`控制访问权限。 总之,掌握Nginx的安装和配置对于Web服务的管理和优化至关...

    nginx-1.8.0.zip

    3. 配置文件:Nginx的配置文件通常位于`conf/nginx.conf`,在这里可以设置监听端口、服务器块、虚拟主机等。 4. 启动Nginx:编译完成后,可以在命令行输入`nginx`或`sudo systemctl start nginx`启动服务。 5. 测试...

    nginx-1.18.0.tar.gz 安装包

    6. 配置启动:编辑配置文件`/usr/local/nginx/conf/nginx.conf`,根据实际需求调整服务器配置。 7. 启动:运行`/usr/local/nginx/sbin/nginx`启动Nginx服务。 8. 监控与管理:使用`nginx -s reload`命令来平滑重启...

    nginx配置lua所需组件

    此外,`lua_shared_dict`可以用来在Nginx实例间共享数据,`lua休息`用于设置定时任务,`log_by_lua`允许在日志记录中使用Lua,而`rewrite_by_lua`则可以在重写规则中执行Lua代码。 为了调试和优化Lua脚本,还可以...

    Nginx Rewrite模块应用的几种场景

    以下我们将详细介绍Nginx Rewrite模块在不同场景下的应用,以及如何配置和使用。 ### 应用场景1:基于域名的跳转 #### 场景描述 假设公司原域名`www.accp.com`需要更换为`www.kgc.com`,但旧域名不能废弃,需要将...

    nginx文件包

    1. **模块扩展**:Nginx通过模块化设计,可以方便地添加或扩展功能,如`ngx_http_rewrite_module`用于URL重写,`ngx_http_access_module`进行访问控制。 2. **性能优化**:包括调整`worker_connections`限制最大...

    nginx-0.8.45

    在配置方面,Nginx使用配置文件(通常为nginx.conf)来定义服务器的行为。配置文件包括多个块,如http、server和location,它们分别定义了全局设置、虚拟主机设置和URL匹配规则。例如,通过`server_name`指令指定...

Global site tag (gtag.js) - Google Analytics