feature: add precontent_by_lua directives#2472
Merged
zhuizhuhaomeng merged 3 commits intoopenresty:masterfrom Jan 17, 2026
Merged
feature: add precontent_by_lua directives#2472zhuizhuhaomeng merged 3 commits intoopenresty:masterfrom
zhuizhuhaomeng merged 3 commits intoopenresty:masterfrom
Conversation
53814d4 to
b43f761
Compare
|
Just tried and works perfectly. really helpful, thanks bro |
zhuizhuhaomeng
requested changes
Jan 16, 2026
src/ngx_http_lua_common.h
Outdated
| #define NGX_HTTP_LUA_CONTEXT_EXIT_WORKER 0x00002000 | ||
| #define NGX_HTTP_LUA_CONTEXT_SSL_CLIENT_HELLO 0x00004000 | ||
| #define NGX_HTTP_LUA_CONTEXT_SERVER_REWRITE 0x00008000 | ||
| #define NGX_HTTP_LUA_CONTEXT_PRECONTENT 0x00020000 |
Contributor
There was a problem hiding this comment.
move the code after NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY
|
|
||
| /* | ||
| * Copyright (C) Xiaozhe Wang (chaoslawful) | ||
| * Copyright (C) Yichun Zhang (agentzh) |
Contributor
There was a problem hiding this comment.
change to your email
src/ngx_http_lua_precontentby.c
Outdated
| #if 0 | ||
| if (cur_ph == last_ph) { | ||
| dd("XXX our handler is already the last precontent phase handler"); | ||
| } |
Contributor
There was a problem hiding this comment.
don't keep the dead code copy from other file
b43f761 to
b2faf26
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.
Since balancer_by_lua_block does not support consocket, getting upstream peers from the outside is usually performed in the access_by_lua_*. (refer to https://github.com/openresty/lua-nginx-module?tab=readme-ov-file#balancer_by_lua_block)
However, since the access phase is mainly responsible for access control, many subrequests usually skip the access phase, resulting in subrequests being unable to get upstream peers in the access phase. The current solution is to identify sub-requests and then move the relevant processing logic forward to rewrite_by_lua_*. But it is ultimately an unreasonable solution, as it inappropriately utilizes the rewrite or access phase to provide functionality that does not belong to them.
In nginx, there is actually a precontent phase after the post_access phase. Initially, nginx named this phase
NGX_HTTP_TRY_FILES_PHASE, and it was a dedicated phase for the try_files module (other module handlers could not be registered). As nginx's functionality expanded, it was renamedNGX_HTTP_PRECONTENT_PHASE, which also allowed the registration of new module handlers.By injecting Lua code into the precontent phase, we can implement proxy/upstream-related preparation functions, including upstream header rewriting, proxy authentication, proxy URI rewriting, and upstream information retrieval (such as obtaining the upstream peer list via DNS). Furthermore, the precontent phase will not be bypassed in sub-requests (one of the biggest problems with registering non-access control related functions in the access phase).
This PR added 3 directives:
The PR for lua-resty-core: openresty/lua-resty-core#519
close #2253