我用ngx.location.capture跳转到子请求去发送正真的代理请求失败,我的openresty 1.7.2 on ubuntu 14.04。
我的应用场景:
我的openresty服务作为代理服务器,但是因为我对代理的http请求有不同的处理方式,所以我要判断uri来通过子请求执行正真的代理服务。
我的测试用例如下:
worker_processes 2;
# dump core dump, for production, it is not needed
#worker_rlimit_core 50M;
#working_directory /home/zhen/temp/nginx/;
#working_directory /tmp;
events {
worker_connections 1024;
}
http {
error_log logs/ngx_error.log debug;
types {
text/html html htm shtml;
text/css css;
application/x-_javascript_ js;
}
resolver 8.8.8.8;
default_type application/octet-stream;
sendfile on;
lua_package_path '$prefix/lua/?.lua;;';
lua_package_cpath '$prefix/lua/lib/?.so;;';
lua_max_pending_timers 4096;
lua_max_running_timers 1024;
lua_socket_log_errors off;
lua_check_client_abort on;
lua_shared_dict ws_locks 100k;
#lua_code_cache on;
lua_code_cache off;
init_by_lua '
require "resty.core"
';
server {
listen 9010 so_keepalive=2s:2s:8;
location / {
content_by_lua '
local res = ngx.location.capture("/proxy", { share_all_vars = true })
for k, v in pairs(res.header) do
ngx.header[k] = v
end
ngx.print(res.body)
';
}
location /proxy {
#proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_pass http://$http_host;
proxy_set_header X-Real-IP $remote_addr;
}
location ~ ^/t/(.+) {
set $lua_file_path lua/t/$1.lua;
content_by_lua_file $lua_file_path;
}
}
}
我对一个图片的uri
http://www.awaker.hk/wp-content/uploads/2014/07/d669591.jpg
进行测试:
curl -I http://www.awaker.hk/wp-content/uploads/2014/07/d669591.jpg -p http://127.0.0.1:9010
显示:
HTTP/1.1 200 OK
Server: nginx/1.2.9
Date: Fri, 18 Jul 2014 02:27:07 GMT
Content-Type: image/jpeg
Content-Length: 40930
Last-Modified: Wed, 16 Jul 2014 10:14:50 GMT
Connection: keep-alive
Accept-Ranges: bytes
HTTP/1.1 200 OK
Server: openresty/1.7.2.1
Date: Fri, 18 Jul 2014 02:28:16 GMT
Content-Type: text/html
Connection: keep-alive
Content-Length: 168
如果不用代理,直接请求: curl -I http://www.awaker.hk/wp-content/uploads/2014/07/d669591.jpg
HTTP/1.1 200 OK
Server: nginx/1.2.9
Date: Fri, 18 Jul 2014 02:40:18 GMT
Content-Type: image/jpeg
Content-Length: 40930
Last-Modified: Wed, 16 Jul 2014 10:14:50 GMT
Connection: keep-alive
Accept-Ranges: bytes
可发现我的代理服务返回的http头都和真实的头不一样,比如content-type和content-length不一样。
请问这问题发生在哪里呢?