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?
Asked
Active
Viewed 1.3k times
7
Jesse Nickles
- 250
- 1
- 12
David Eyk
- 667
- 1
- 7
- 17
-
Related: https://serverfault.com/questions/482875/nginx-removes-content-length-header-for-chunked-content – Jesse Nickles Sep 06 '22 at 20:58
2 Answers
8
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.
1
Here's a performant Nginx solution to add a x-file-size
header:
https://github.com/AnthumChris/fetch-progress-indicators/issues/13
## 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")
file:close()
return size
end
ngx.header["X-File-Size"] = file_len(ngx.var.request_filename);
}
AnthumChris
- 228
- 2
- 6