nginx +ttserver 做URL转发

tt版本1.1.41

by : Alex

查找

/* handle the HTTP GET command */
static void do_http_get(TTSOCK *sock, TASKARG *arg, TTREQ *req, int ver, const char *uri){


里面有:

int vsiz;
char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz);
if(vbuf){
tcxstrprintf(xstr, "HTTP/1.1 200 OK\r\n");
tcxstrprintf(xstr, "Content-Type: application/octet-stream\r\n");
tcxstrprintf(xstr, "Content-Length: %d\r\n", vsiz);
tcxstrprintf(xstr, "\r\n");
tcxstrcat(xstr, vbuf, vsiz);
tcfree(vbuf);
}else {
arg->counts[TTSEQNUM*req->idx+TTSEQGETMISS]++;
int len = sprintf(line, "Not Found\n");
tcxstrprintf(xstr, "HTTP/1.1 404 Not Found\r\n");
tcxstrprintf(xstr, "Content-Type: text/plain\r\n");
tcxstrprintf(xstr, "Content-Length: %d\r\n", len);
tcxstrprintf(xstr, "\r\n");
tcxstrcat(xstr, line, len);
}


修改为

int vsiz;
char *vbuf = tcadbget(adb, kbuf, ksiz, &vsiz);
if(vbuf){
tcxstrprintf(xstr, "HTTP/1.1 302 OK\r\n");
tcxstrprintf(xstr, "Content-Type: text/html; charaset=utf-8\r\n");
tcxstrprintf(xstr, "Location: %s\r\n", vbuf);
tcxstrprintf(xstr, "\r\n");
tcfree(vbuf);
}else {
//这个地方处理未找到值的情况,类似404,根据需要决定是否修改
arg->counts[TTSEQNUM*req->idx+TTSEQGETMISS]++;
int len = sprintf(line, "Not Found\n");
tcxstrprintf(xstr, "HTTP/1.1 404 Not Found\r\n");
tcxstrprintf(xstr, "Content-Type: text/plain\r\n");
tcxstrprintf(xstr, "Content-Length: %d\r\n", len);
tcxstrprintf(xstr, "\r\n");
tcxstrcat(xstr, line, len);
}

编译后安装

这样访问http://localhost:11211/thekey就会直接跳转到thekey在ttserver hashmap里对应的value这个location

用nginx过滤掉对http访问的put\update\delete等操作

location ~* /count(.*) {
if ($request_method = PUT ) {
return 403;
}
if ($request_method = DELETE ) {
return 403;
}
if ($request_method = POST ) {
return 403;
}
proxy_method GET;
}

或者注意在1051行开始的这段代码(ttserver.c)

if(!strcmp(cmd, "GET")){
do_http_get(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "HEAD")){
do_http_head(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "PUT")){
do_http_put(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "POST")){
do_http_post(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "DELETE")){
do_http_delete(sock, arg, req, ver, uri);
} else if(!strcmp(cmd, "OPTIONS")){
do_http_options(sock, arg, req, ver, uri);
}

可以把不需要的功能注释掉(这段未经过测试检验)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值