Skip to content

Instantly share code, notes, and snippets.

@hamishforbes
Created September 23, 2014 13:56
Show Gist options
  • Save hamishforbes/fab6ed71b4858455332a to your computer and use it in GitHub Desktop.
Save hamishforbes/fab6ed71b4858455332a to your computer and use it in GitHub Desktop.
location capture X-Forwarded-For
hamish@Hamish-MBP ~/edge> curl -v -s -H'X-Forwarded-For: 123.123.123.123' -H'X-Test: foobar' localhost/test/foo
> GET /test/foo HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost
> Accept: */*
> X-Forwarded-For: 123.123.123.123
> X-Test: foobar
>
< HTTP/1.1 200 OK
< Server: ngx_openresty/1.4.3.6
< Date: Tue, 23 Sep 2014 13:54:49 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
host: test
x-forwarded-for: 123.123.123.123
x-proxy-test: 123.123.123.123
x-test: foobar
accept: */*
user-agent: curl/7.30.0
connection: close
hamish@Hamish-MBP ~/edge> curl -v -s -H'X-Forwarded-For: 123.123.123.123' -H'X-Test: foobar' localhost/test/foo
> GET /test/foo HTTP/1.1
> User-Agent: curl/7.30.0
> Host: localhost
> Accept: */*
> X-Forwarded-For: 123.123.123.123
> X-Test: foobar
>
< HTTP/1.1 200 OK
< Server: openresty/1.5.8.1
< Date: Tue, 23 Sep 2014 13:54:27 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
host: test
connection: close
x-test: foobar
accept: */*
user-agent: curl/7.30.0
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
keepalive_timeout 65;
upstream test {
server 127.0.0.1:81;
keepalive 128;
}
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # works
#proxy_set_header X-Forwarded-For "10.10.10.10"; # works
proxy_set_header X-Forwarded-For $http_x_forwarded_for; # does not work
proxy_set_header X-Proxy-Test $http_x_forwarded_for; # does not work
proxy_set_header X-Test $http_x_test; # works
server {
listen 80;
location /test {
content_by_lua '
local res = ngx.location.capture("/__test" .. ngx.var.request_uri)
ngx.say(res.body)
';
}
location /test2 {
rewrite .* /__test$request_uri;
}
location /test3 {
content_by_lua '
ngx.exec("/__test"..ngx.var.uri)
';
}
location /__test {
proxy_pass $scheme://test;
}
}
server {
listen 81;
location / {
content_by_lua '
local h = ngx.req.get_headers()
for k,v in pairs(h) do
ngx.say(k,": ", v)
end
';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment