Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_perl_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Igor Sysoev
5
Copyright (C) Nginx, Inc.
6
-->
7
8
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
9
10
<module name="Module ngx_http_perl_module"
11
link="/en/docs/http/ngx_http_perl_module.html"
12
lang="en"
13
rev="7">
14
15
<section id="summary">
16
17
<para>
18
The <literal>ngx_http_perl_module</literal> module is used to implement
19
location and variable handlers in Perl and insert Perl calls into SSI.
20
</para>
21
22
<para>
23
This module is not built by default, it should be enabled with the
24
<literal>--with-http_perl_module</literal>
25
configuration parameter.
26
<note>
27
This module requires
28
<link url="https://www.perl.org/get.html">Perl</link> version 5.6.1 or higher.
29
The C compiler should be compatible with the one used to build Perl.
30
</note>
31
</para>
32
33
</section>
34
35
36
<section id="issues" name="Known Issues">
37
38
<para>
39
The module is experimental, caveat emptor applies.
40
</para>
41
42
<para>
43
In order for Perl to recompile the modified modules during
44
reconfiguration, it should be built with the
45
<literal>-Dusemultiplicity=yes</literal> or
46
<literal>-Dusethreads=yes</literal> parameters.
47
Also, to make Perl leak less memory at run time,
48
it should be built with the
49
<literal>-Dusemymalloc=no</literal> parameter.
50
To check the values of these parameters in an already built
51
Perl (preferred values are specified in the example), run:
52
<example>
53
$ perl -V:usemultiplicity -V:usemymalloc
54
usemultiplicity='define';
55
usemymalloc='n';
56
</example>
57
</para>
58
59
<para>
60
Note that after rebuilding Perl with the new
61
<literal>-Dusemultiplicity=yes</literal> or
62
<literal>-Dusethreads=yes</literal> parameters,
63
all binary Perl modules will have to be rebuilt as well —
64
they will just stop working with the new Perl.
65
</para>
66
67
<para>
68
There is a possibility that the main process and then worker processes will
69
grow in size after every reconfiguration.
70
If the main process grows to an unacceptable size, the
71
<link doc="../control.xml" id="upgrade">live upgrade</link>
72
procedure can be applied without changing the executable file.
73
</para>
74
75
<para>
76
While the Perl module is performing a long-running operation, such as
77
resolving a domain name, connecting to another server, or querying a database,
78
other requests assigned to the current worker process will not be processed.
79
It is thus recommended to perform only such operations
80
that have predictable and short execution time, such as
81
accessing the local file system.
82
</para>
83
84
</section>
85
86
87
<section id="example" name="Example Configuration">
88
89
<para>
90
<example>
91
http {
92
93
perl_modules perl/lib;
94
perl_require hello.pm;
95
96
perl_set $msie6 '
97
98
sub {
99
my $r = shift;
100
my $ua = $r->header_in("User-Agent");
101
102
return "" if $ua =~ /Opera/;
103
return "1" if $ua =~ / MSIE [6-9]\.\d+/;
104
return "";
105
}
106
107
';
108
109
server {
110
location / {
111
perl hello::handler;
112
}
113
}
114
</example>
115
</para>
116
117
<para>
118
The <path>perl/lib/hello.pm</path> module:
119
<example>
120
package hello;
121
122
use nginx;
123
124
sub handler {
125
my $r = shift;
126
127
$r->send_http_header("text/html");
128
return OK if $r->header_only;
129
130
$r->print("hello!\n&lt;br/&gt;");
131
132
if (-f $r->filename or -d _) {
133
$r->print($r->uri, " exists!\n");
134
}
135
136
return OK;
137
}
138
139
1;
140
__END__
141
</example>
142
</para>
143
144
</section>
145
146
147
<section id="directives" name="Directives">
148
149
<directive name="perl">
150
<syntax><value>module</value>::<value>function</value>|'sub { ... }'</syntax>
151
<default/>
152
<context>location</context>
153
<context>limit_except</context>
154
155
<para>
156
Sets a Perl handler for the given location.
157
</para>
158
159
</directive>
160
161
162
<directive name="perl_modules">
163
<syntax><value>path</value></syntax>
164
<default/>
165
<context>http</context>
166
167
<para>
168
Sets an additional path for Perl modules.
169
</para>
170
171
</directive>
172
173
174
<directive name="perl_require">
175
<syntax><value>module</value></syntax>
176
<default/>
177
<context>http</context>
178
179
<para>
180
Defines the name of a module that will be loaded during each
181
reconfiguration.
182
Several <literal>perl_require</literal> directives can be present.
183
</para>
184
185
</directive>
186
187
188
<directive name="perl_set">
189
<syntax>
190
<value>$variable</value>
191
<value>module</value>::<value>function</value>|'sub { ... }'</syntax>
192
<default/>
193
<context>http</context>
194
195
<para>
196
Installs a Perl handler for the specified variable.
197
</para>
198
199
</directive>
200
201
</section>
202
203
204
<section id="ssi" name="Calling Perl from SSI">
205
206
<para>
207
An SSI command calling Perl has the following format:
208
<example>
209
&lt;!--# perl sub="<value>module</value>::<value>function</value>" arg="<value>parameter1</value>" arg="<value>parameter2</value>" ...
210
--&gt;
211
</example>
212
</para>
213
214
</section>
215
216
217
<section id="methods" name="The $r Request Object Methods">
218
219
<para>
220
<list type="tag">
221
222
<tag-name><literal>$r->args</literal></tag-name>
223
<tag-desc>
224
returns request arguments.
225
</tag-desc>
226
227
<tag-name><literal>$r->filename</literal></tag-name>
228
<tag-desc>
229
returns a filename corresponding to the request URI.
230
</tag-desc>
231
232
<tag-name>
233
<literal>$r->has_request_body(<value>handler</value>)</literal>
234
</tag-name>
235
<tag-desc>
236
returns 0 if there is no body in a request.
237
If there is a body, the specified handler is set for the request
238
and 1 is returned.
239
After reading the request body, nginx will call the specified handler.
240
Note that the handler function should be passed by reference.
241
Example:
242
<example>
243
package hello;
244
245
use nginx;
246
247
sub handler {
248
my $r = shift;
249
250
if ($r->request_method ne "POST") {
251
return DECLINED;
252
}
253
254
if ($r->has_request_body(<emphasis>\&amp;post</emphasis>)) {
255
return OK;
256
}
257
258
return HTTP_BAD_REQUEST;
259
}
260
261
sub <emphasis>post</emphasis> {
262
my $r = shift;
263
264
$r->send_http_header;
265
266
$r->print("request_body: \"", $r->request_body, "\"&lt;br/&gt;");
267
$r->print("request_body_file: \"", $r->request_body_file, "\"&lt;br/&gt;\n");
268
269
return OK;
270
}
271
272
1;
273
274
__END__
275
</example>
276
</tag-desc>
277
278
<tag-name><literal>$r->allow_ranges</literal></tag-name>
279
<tag-desc>
280
enables the use of byte ranges when sending responses.
281
</tag-desc>
282
283
<tag-name><literal>$r->discard_request_body</literal></tag-name>
284
<tag-desc>
285
instructs nginx to discard the request body.
286
</tag-desc>
287
288
<tag-name><literal>$r->header_in(<value>field</value>)</literal></tag-name>
289
<tag-desc>
290
returns the value of the specified client request header field.
291
</tag-desc>
292
293
<tag-name><literal>$r->header_only</literal></tag-name>
294
<tag-desc>
295
determines whether the whole response or only its header should be sent to
296
the client.
297
</tag-desc>
298
299
<tag-name>
300
<literal>$r->header_out(<value>field</value>,
301
<value>value</value>)</literal>
302
</tag-name>
303
<tag-desc>
304
sets a value for the specified response header field.
305
</tag-desc>
306
307
<tag-name id="r_internal_redirect">
308
<literal>$r->internal_redirect(<value>uri</value>)</literal>
309
</tag-name>
310
<tag-desc>
311
does an internal redirect to the specified <value>uri</value>.
312
An actual redirect happens after the Perl handler execution is completed.
313
<note>
314
Since version 1.17.2, the method accepts escaped URIs and
315
supports redirections to named locations.
316
</note>
317
</tag-desc>
318
319
<tag-name><literal>$r->log_error(<value>errno</value>,
320
<value>message</value>)</literal></tag-name>
321
<tag-desc>
322
writes the specified <value>message</value> into the
323
<link doc="../ngx_core_module.xml" id="error_log"/>.
324
If <value>errno</value> is non-zero, an error code and its description
325
will be appended to the message.
326
</tag-desc>
327
328
<tag-name><literal>$r->print(<value>text</value>, ...)</literal></tag-name>
329
<tag-desc>
330
passes data to a client.
331
</tag-desc>
332
333
<tag-name><literal>$r->request_body</literal></tag-name>
334
<tag-desc>
335
returns the client request body if it has not been
336
written to a temporary file.
337
To ensure that the client request body is in memory,
338
its size should be limited by
339
<link doc="ngx_http_core_module.xml" id="client_max_body_size"/>,
340
and a sufficient buffer size should be set using
341
<link doc="ngx_http_core_module.xml" id="client_body_buffer_size"/>.
342
</tag-desc>
343
344
<tag-name><literal>$r->request_body_file</literal></tag-name>
345
<tag-desc>
346
returns the name of the file with the client request body.
347
After the processing, the file should be removed.
348
To always write a request body to a file,
349
<link doc="ngx_http_core_module.xml" id="client_body_in_file_only"/>
350
should be enabled.
351
</tag-desc>
352
353
<tag-name><literal>$r->request_method</literal></tag-name>
354
<tag-desc>
355
returns the client request HTTP method.
356
</tag-desc>
357
358
<tag-name><literal>$r->remote_addr</literal></tag-name>
359
<tag-desc>
360
returns the client IP address.
361
</tag-desc>
362
363
<tag-name><literal>$r->flush</literal></tag-name>
364
<tag-desc>
365
immediately sends data to the client.
366
</tag-desc>
367
368
<tag-name>
369
<literal>$r->sendfile(<value>name</value>[,
370
<value>offset</value>[,
371
<value>length</value>]])</literal>
372
</tag-name>
373
<tag-desc>
374
sends the specified file content to the client.
375
Optional parameters
376
specify the initial offset and length of the data to be transmitted.
377
The actual data transmission happens after the Perl handler
378
has completed.
379
</tag-desc>
380
381
<tag-name>
382
<literal>$r->send_http_header([<value>type</value>])</literal>
383
</tag-name>
384
<tag-desc>
385
sends the response header to the client.
386
The optional <value>type</value> parameter sets the value of
387
the <header>Content-Type</header> response header field.
388
If the value is an empty string, the <header>Content-Type</header>
389
header field will not be sent.
390
</tag-desc>
391
392
<tag-name><literal>$r->status(<value>code</value>)</literal></tag-name>
393
<tag-desc>
394
sets a response code.
395
</tag-desc>
396
397
<tag-name>
398
<literal>$r->sleep(<value>milliseconds</value>,
399
<value>handler</value>)</literal>
400
</tag-name>
401
<tag-desc>
402
sets the specified handler
403
and stops request processing for the specified time.
404
In the meantime, nginx continues to process other requests.
405
After the specified time has elapsed, nginx will call the installed handler.
406
Note that the handler function should be passed by reference.
407
In order to pass data between handlers,
408
<literal>$r->variable()</literal> should be used.
409
Example:
410
<example>
411
package hello;
412
413
use nginx;
414
415
sub handler {
416
my $r = shift;
417
418
$r->discard_request_body;
419
$r->variable("var", "OK");
420
$r->sleep(1000, <emphasis>\&amp;next</emphasis>);
421
422
return OK;
423
}
424
425
sub <emphasis>next</emphasis> {
426
my $r = shift;
427
428
$r->send_http_header;
429
$r->print($r->variable("var"));
430
431
return OK;
432
}
433
434
1;
435
436
__END__
437
</example>
438
</tag-desc>
439
440
<tag-name><literal>$r->unescape(<value>text</value>)</literal></tag-name>
441
<tag-desc>
442
decodes a text encoded in the “%XX” form.
443
</tag-desc>
444
445
<tag-name><literal>$r->uri</literal></tag-name>
446
<tag-desc>
447
returns a request URI.
448
</tag-desc>
449
450
<tag-name>
451
<literal>$r->variable(<value>name</value>[,
452
<value>value</value>])</literal>
453
</tag-name>
454
<tag-desc>
455
returns or sets the value of the specified variable.
456
Variables are local to each request.
457
</tag-desc>
458
459
</list>
460
</para>
461
462
</section>
463
464
</module>
465
466