1
1
= Nginx directive execution order (01) =
2
2
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.
11
10
12
11
We start with a confused example:
13
12
@@ -22,33 +21,31 @@ We start with a confused example:
22
21
23
22
Clearly, we'd expect to output C<32>, followed by C<56>. Because
24
23
variable 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 :
26
25
27
26
:bash
28
27
$ curl 'http://localhost:8080/test
29
28
56
30
29
56
31
30
32
31
Wow, 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?
34
33
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,
37
35
the execution follows a few predefined phases.
38
36
39
37
There can be altogether 11 phases when Nginx handles a request, let's
40
38
start 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. )
42
40
43
- Usually a Nginx module and its commands register their execution
41
+ Usually an Nginx module and its commands register their execution
44
42
in only one of those phases. For example command L<ngx_rewrite/set> runs
45
43
in phase C<rewrite>, and command L<ngx_echo/echo> runs in phase C<content>.
46
44
Since phase C<rewrite> occurs before phase C<content> for every request
47
45
processing, its commands are executed earlier as well. Therefore,
48
46
command L<ngx_rewrite/set> always gets executed before command L<ngx_echo/echo>
49
47
within one C<location> directive, regardless of their statement ordering
50
- in the
51
- configuration.
48
+ in the configuration.
52
49
53
50
Back to our example:
54
51
@@ -69,19 +66,17 @@ The actual execution ordering is:
69
66
70
67
It's clear now, two commands L<ngx_rewrite/set> are executed in phase
71
68
C<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.
69
+ phase C<content>. Commands in different phases cannot be executed
70
+ back and forth.
74
71
75
- To prove ourselves and better uncover these points, We can
76
- troubleshoot Nginx's "debug log".
72
+ To prove this, we can enable Nginx's "debug log".
77
73
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
84
- from source:
74
+ If you have not worked with Nginx "debug log" before, here is a brief
75
+ introduction. The "debug log" is disabled by default because
76
+ performance is degraded when it is enabled. To enable "debug log"
77
+ you must reconfigure and recompile Nginx, and set the
78
+ C<--with-debug> option for the package's C<./configure> script.
79
+ When building under Linux or Mac OS X from source:
85
80
86
81
:bash
87
82
tar xvf nginx-1.0.10.tar.gz
240
235
execution ordering. Igor Sysoev, the author of Nginx, has made the statements
241
236
a few times publicly, that Nginx mini language in its configuration is
242
237
"declarative" not "procedural".
243
-
0 commit comments