Skip to content

Conversation

@HanadaLee
Copy link

@HanadaLee HanadaLee commented Jan 11, 2026

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 renamed NGX_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:

precontent_by_lua_block
precontent_by_lua_file
precontent_by_lua_no_postpone

The PR for lua-resty-core: openresty/lua-resty-core#519

close #2253

@4O4-Not-F0und
Copy link

Just tried and works perfectly. really helpful, thanks bro

#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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move the code after NGX_HTTP_LUA_CONTEXT_PROXY_SSL_VERIFY


/*
* Copyright (C) Xiaozhe Wang (chaoslawful)
* Copyright (C) Yichun Zhang (agentzh)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to your email

#if 0
if (cur_ph == last_ph) {
dd("XXX our handler is already the last precontent phase handler");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't keep the dead code copy from other file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

why not support lua phase in pre_content phase

3 participants