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.2k times
7
David Eyk
- 647
- 1
- 7
- 16
2 Answers
7
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 – AnthumChris May 02 '20 at 17:17
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
- 218
- 2
- 6