Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua script to render an unmodified X-Forwarded-For header #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion inner-proxy/conf/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ server {
}

location /app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Bad-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-For $realip_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
Expand Down
11 changes: 11 additions & 0 deletions inner-proxy/conf/lua/realip-x-forwarded-for.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
local _M = {}

function _M.run()
if (ngx.var.http_x_forwarded_for == "" or ngx.var.http_x_forwarded_for == nil) then
ngx.var.realip_add_x_forwarded_for = ngx.var.realip_remote_addr
else
ngx.var.realip_add_x_forwarded_for = ngx.var.http_x_forwarded_for .. ", " .. ngx.var.realip_remote_addr
end
end

return _M
5 changes: 5 additions & 0 deletions inner-proxy/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ http {
set_real_ip_from 10.20.30.2/32;
real_ip_header X-Forwarded-For;

map $request $realip_add_x_forwarded_for { default ""; }
access_by_lua_block {
require("realip-x-forwarded-for").run()
}

# Include the rest of the configuration
include /etc/nginx/conf.d/*.conf;
}