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

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

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

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

是不是清爽了许多?有木有?!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值