openresty / openresty/lua-nginx-module
Content-Type set incorrectly in lua-nginx-module
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 11.8k
- Forks
- 2.1k
- Avg merge
- 6h 1m
- Merged PRs (30d)
- 6
Description
lua-nginx-module sets a default content-type and does not override it from lua script, but nginx can using the conf. Details follow:
- Version: openresty-1.11.2.2 on Ubuntu 16.04 on EC2
- Scenario:
-
I have set up a nginx + lua for 3scale for API application management. I meter the payload sizes in the HTTP requests and responses and deny access when they cross a certain threshold. The reporting to 3scale occurs using the nginx.location.capture function. Reporting to 3scale is always a POST operation, and they expect an application/x-www-form-urlencoded Content-Type, or none, which defaults to urlencoded. Anything other than these two are rejected by 3scale.
-
When I get a GET request for my backend (without a Content-Type header), the content-type is not set in nginx.location.capture and 3scale can easily accept requests by defaulting it to urlencoded payload, and everything works.
-
When I get a POST request for my backend (with a Content-Type of application/json), 3scale starts rejecting the reporting call. This is absurd as it's the same code that gets executed in both the cases.
-
tshark showed that the content-type set for reporting is derived from the original request, which is of type application/json.
-
Overriding the original request with
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded") in the lua script had no effect. -
Solved it by adding a
proxy_set_header Content-Type "application/x-www-form-urlencoded"; -
Issue being that we cannot enforce a content type using the lua script while using nginx.location.capture.
- Snippet:
ngx.req.set_header("Content-Type", "application/x-www-form-urlencoded")
local res1 = ngx.location.capture("/report", {method = ngx.HTTP_POST, body = ngx.encode_args(report), share_all_vars = true })
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the ngx.req.set_header and ngx.location.capture behavior shown in the snippet, then compare it with the proxy_set_header workaround. Reproduce the reported GET and POST cases using openresty-1.11.2.2; done means the capture request honors the Lua-set application/x-www-form-urlencoded Content-Type without requiring proxy configuration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua, nginx
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100