Skip to content

log the size of the response headers #2

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

Open
wants to merge 1 commit into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ func (s *Server) baseHandler(w http.ResponseWriter, r *http.Request) {
return
}
}

func (s *Server) largeHandler(w http.ResponseWriter, r *http.Request) {
b := make([]byte, 8192)
for i := range b {
b[i] = 'A'
}

w.Header().Add("X-Large-Header", string(b))
fmt.Fprintf(w, "very large\n")
}
2 changes: 2 additions & 0 deletions backend/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (s *Server) Run() error {

mux := http.NewServeMux()
mux.HandleFunc("/", s.baseHandler)
mux.HandleFunc("/large", s.largeHandler)
mux.HandleFunc("/large_error", s.largeHandler)

hs := &http.Server{
Addr: fmt.Sprintf("%s:%d", s.Address, s.Port),
Expand Down
16 changes: 10 additions & 6 deletions outer-proxy/conf/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ server {
proxy_pass http://inner-proxy;
}

#error_page 404 /404.html;
location /large {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
proxy_buffer_size 9k;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
location /large_error {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://backend;
}
}
2 changes: 1 addition & 1 deletion outer-proxy/conf/lua/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
local _M = {}

function _M.run()
ngx.var.example_var = "foo"
-- ngx.var.example_var = "foo"
end

return _M
20 changes: 20 additions & 0 deletions outer-proxy/conf/lua/log_headers_size.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- log the total size of the response's headers.
-- https://github.com/openresty/lua-nginx-module#ngxrespget_headers

local _M = {}

function _M.run()
local h, err = ngx.resp.get_headers()
if err == "truncated" then
ngx.log(ngx.ERR, "truncated headers from lua")
return
end

local size=0
for k, v in pairs(h) do
size = size + string.len(v)
end
ngx.var.headers_size = size
end

return _M
9 changes: 8 additions & 1 deletion outer-proxy/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ http {
'"headers":{'
'"x_forwarded_for": "$http_x_forwarded_for",'
'"x_forwarded_host": "$http_x_forwarded_host",'
'"x_forwarded_proto": "$http_x_forwarded_proto"'
'"x_forwarded_proto": "$http_x_forwarded_proto",'
'"response_size": $headers_size'
'},'
'"host": "$host",'
'"method": "$request_method", '
Expand Down Expand Up @@ -88,11 +89,17 @@ http {
# Set default values for variables manipulated by Lua scripts.
map $request $example_var { default ""; }

map $request $headers_size { default 0; }

# Run the following Lua scripts in the log phase.
log_by_lua '
require("example").run()
';

header_filter_by_lua_block {
require("log_headers_size").run()
}

# Misc settings
sendfile on;
keepalive_timeout 65;
Expand Down