11= Nginx directive execution order (01) =
22
3- It can be really frustrated for many Nginx users, that
4- if multiple Nginx module's commands are written within
5- one C<location> directive, the execution order can be
6- very different from the order they were written. For those
7- impatient who choose "try out possibilities before everything
8- else", the directive commands can be scattered like a hell.
9- This series is to uncover the mysteries and help you better
10- understand the execution ordering behind the scene.
3+ When there are multiple Nginx module commands in a
4+ C<location> directive, the execution order can be
5+ different from what you expect. Busy Nginx users who
6+ attempt to configure Nginx by "trial and error" may be
7+ very confused by this behavior. This series is to
8+ uncover the mysteries and help you better
9+ understand the execution ordering behind the scenes.
1110
1211We start with a confused example:
1312
@@ -22,33 +21,31 @@ We start with a confused example:
2221
2322Clearly, we'd expect to output C<32>, followed by C<56>. Because
2423variable C<$a> has been reset after command L<ngx_echo/echo> "is
25- executed". Really? you are welcomed to the reality:
24+ executed". Really? the reality is :
2625
2726 :bash
2827 $ curl 'http://localhost:8080/test
2928 56
3029 56
3130
3231Wow, statement C<set $a 56> must have had been executed before
33- the first C<echo $a> command, but why? Is it a Nginx bug ?
32+ the first C<echo $a> command, but why? Is it a Nginx bug?
3433
35- There ain't any Nginx bug here, or we'd rather rephrase it as a
36- feature, and it's a long story. When Nginx handles every request,
34+ No, this is not an Nginx bug. When Nginx handles every request,
3735the execution follows a few predefined phases.
3836
3937There can be altogether 11 phases when Nginx handles a request, let's
4038start with three most common ones: C<rewrite>, C<access> and C<content>
41- (later on the other phases will be addressed)
39+ (The other phases will be addressed later. )
4240
43- Usually a Nginx module and its commands register their execution
41+ Usually an Nginx module and its commands register their execution
4442in only one of those phases. For example command L<ngx_rewrite/set> runs
4543in phase C<rewrite>, and command L<ngx_echo/echo> runs in phase C<content>.
4644Since phase C<rewrite> occurs before phase C<content> for every request
4745processing, its commands are executed earlier as well. Therefore,
4846command L<ngx_rewrite/set> always gets executed before command L<ngx_echo/echo>
4947within one C<location> directive, regardless of their statement ordering
50- in the
51- configuration.
48+ in the configuration.
5249
5350Back to our example:
5451
@@ -69,18 +66,19 @@ The actual execution ordering is:
6966
7067It's clear now, two commands L<ngx_rewrite/set> are executed in phase
7168C<rewrite>, two commands L<ngx_echo/echo> are executed afterwards in
72- phase C<content>. Commands belonging to different phases cannot be
73- executed back and forth.
74-
75- To prove ourselves and better uncover these points, We can
76- troubleshoot Nginx's "debug log".
77-
78- We've not checked Nginx "debug log" before, so let's briefly introduce
79- its usage."debug log" by default is disabled, because it has very
80- big runtime overheads and overall Nginx service is degraded. To enable
81- "debug log" we would need to reconfigure and recompile Nginx binary, by
82- giving C<--with-debug> option for the package's C<./configure> script.
83- The typical steps are as following when build under Linux or Mac OS X
69+ phase C<content>. Commands in different phases cannot be executed
70+ back and forth.
71+
72+ To prove this, we can troubleshoot Nginx's "debug log".
73+
74+ If you have not worked with Nginx "debug log" before, here is a brief
75+ introduction. The "debug log" by default is disabled, because it has very
76+ big runtime overheads and the overall Nginx performance is degraded when
77+ it is enabled.
78+
79+ To enable "debug log" we would need to reconfigure and recompile Nginx
80+ binary, by giving C<--with-debug> option for the package's C<./configure>
81+ script. The typical steps are as following when build under Linux or Mac OS X
8482from source:
8583
8684 :bash
240238execution ordering. Igor Sysoev, the author of Nginx, has made the statements
241239a few times publicly, that Nginx mini language in its configuration is
242240"declarative" not "procedural".
243-
0 commit comments