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

可以把不需要的功能注释掉(这段未经过测试检验)

猜你喜欢

转载自alexzh.iteye.com/blog/754240