Nginx 如何去除 Content-Length 请求头并防止转发到后端
使用 Nginx 的 ngx_http_headers_module 和 ngx_http_lua_module 模块可以去除请求头中的'Content-Length: 10',并防止其被转发到后端服务。以下是一种方法:
- 确认已安装所需模块: 使用以下命令检查 ngx_http_headers_module 和 ngx_http_lua_module 是否已安装:
nginx -V
-
编辑 Nginx 配置文件: 通常为
/etc/nginx/nginx.conf。 -
在
http配置块中添加以下内容:
http {
# ...
lua_shared_dict headers 1m;
init_by_lua_block {
local headers = ngx.shared.headers
headers:set('Content-Length', nil)
}
# ...
}
- 在
server或location配置块中添加以下内容:
location / {
# ...
access_by_lua_block {
local headers = ngx.shared.headers
headers:set('Content-Length', nil)
}
# ...
}
- 重新加载或重启 Nginx 服务。
经过上述配置后,Nginx 将不会将 'Content-Length: 10' 的头转发到后端服务。请注意,这可能会导致请求的 'Content-Length' 需要重新计算,因此请确保请求的实际内容长度与 'Content-Length' 头中的值匹配。
原文地址: https://www.cveoy.top/t/topic/pd4o 著作权归作者所有。请勿转载和采集!