We're running Nginx 0.7.65[-1ubuntu2.3]. I've just noticed that when serving local static files using an alias directive and gzip on, the Content-Length header is not getting sent. Since it's serving files from the local filesystem, it shouldn't have any problem getting the length. How can I force Nginx to send a Content-Length header with these files?


2 Answers 2


It turns out that when using dynamic Gzip then the Content-Length header is not sent, as the Transfer-Encoding is chunked. Pre-compressing my files and switching to static Gzip allows Nginx to know ahead of time the file size and send an appropriate Content-Length header.

  • I recommend using x-file-size below May 2, 2020 at 17:17

Here's a performant Nginx solution to add a x-file-size header:


## Nginx Lua module must be installed https://docs.nginx.com/nginx/admin-guide/dynamic-modules/lua/
## https://github.com/openresty/lua-nginx-module#header_filter_by_lua
header_filter_by_lua_block {
  function file_len(file_name)
    local file = io.open(file_name, "r")

    if (file == nil) then return -1 end

    local size = file:seek("end")
    return size

  ngx.header["X-File-Size"] = file_len(ngx.var.request_filename);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.