liseen / lua-resty-http

Lua http client driver for the ngx_lua based on the cosocket API
188 stars 70 forks source link

Proxy configuration #40

Closed o1da closed 8 years ago

o1da commented 8 years ago

How to configure a connection to e.g. Squid proxy which runs on localhost? I need to send requests to an endpoint on the internet through this proxy. In the proxy example, it seems that httpc:connect(HOST, PORT) is for endpoint configuration. So how should I tell LUA to use the proxy on the specific port on the localhost? Is it possible?

local http = require "resty.http"
local httpc = http.new()

httpc:set_timeout(500)
local ok, err = httpc:connect(HOST, PORT)

if not ok then
  ngx.log(ngx.ERR, err)
  return
end

httpc:set_timeout(2000)
httpc:proxy_response(httpc:proxy_request())
httpc:set_keepalive()
o1da commented 8 years ago

I have just realized that I wanted to use it as forward proxy and example speaks about reverse proxy, so the solution would be Squid configured like the reverse proxy - URL for the endpoint configured there and LUA connected to Squid. Am I correct or completely wrong? :)

qrof commented 8 years ago

The best way to handle external connections is to create an nginx upstream (local reverse proxy) and then use ngx.location.capture to do requests to this local endpoint

in nginx.conf

    location ~* /interneturl/(.*)$ {
            proxy_pass http://www.someinternetsite.com/$1$is_args$args;
            proxy_redirect off;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

then in lua script: local resp = ngx.location.capture("/interneturl/content/" .. param, { method = ngx.HTTP_GET })

etc...

only time I would use lua resty http is when you need to dynamically set target domain / IP

so you dont need squid, just use nginx for reverse proxy, this way all is non-blocking

o1da commented 8 years ago

Thank you :)