1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
|
=encoding utf-8
=head1 About
C<ngx_postgres> is an upstream module that allows C<nginx> to communicate directly
with C<PostgreSQL> database.
Response is generated in C<rds> format, so it's compatible with C<ngx_rds_json>
and C<ngx_drizzle> modules.
=head1 Status
This module is production-ready and it's compatible with following nginx
releases:
=over
=item *
0.7.x (tested with 0.7.60 to 0.7.69),
=item *
0.8.x (tested with 0.8.0 to 0.8.55),
=item *
0.9.x (tested with 0.9.0 to 0.9.7),
=item *
1.0.x (tested with 1.0.0 to 1.0.11),
=item *
1.1.x (tested with 1.1.0 to 1.1.12).
=item *
1.2.x (tested with 1.2.3 to 1.2.3).
=item *
1.3.x (tested with 1.3.4 to 1.3.4).
=back
=head1 Configuration directives
=head2 postgres_server
=over
=item *
B<syntax>: C<postgres_server ip[:port] dbname=dbname user=user password=pass>
=item *
B<default>: C<none>
=item *
B<context>: C<upstream>
=back
Set details about the database server.
=head2 postgres_keepalive
=over
=item *
B<syntax>: C<postgres_keepalive off | max=count [mode=single|multi] [overflow=ignore|reject]>
=item *
B<default>: C<max=10 mode=single overflow=ignore>
=item *
B<context>: C<upstream>
=back
Configure keepalive parameters:
=over
=item *
C<max> - maximum number of keepalive connections (per worker process),
=item *
C<mode> - backend matching mode,
=item *
C<overflow> - either C<ignore> the fact that keepalive connection pool is full
and allow request, but close connection afterwards or C<reject> request with
C<503 Service Unavailable> response.
=back
=head2 postgres_pass
=over
=item *
B<syntax>: C<postgres_pass upstream>
=item *
B<default>: C<none>
=item *
B<context>: C<location>, C<if location>
=back
Set name of an upstream block that will be used for the database connections
(it can include variables).
=head2 postgres_query
=over
=item *
B<syntax>: C<postgres_query [methods] query>
=item *
B<default>: C<none>
=item *
B<context>: C<http>, C<server>, C<location>, C<if location>
=back
Set query string (it can include variables). When methods are specified then
query is used only for them, otherwise it's used for all methods.
This directive can be used more than once within same context.
=head2 postgres_rewrite
=over
=item *
B<syntax>: C<postgres_rewrite [methods] condition [=]status_code>
=item *
B<default>: C<none>
=item *
B<context>: C<http>, C<server>, C<location>, C<if location>
=back
Rewrite response C<status_code> when given condition is met (first one wins!):
=over
=item *
C<no_changes> - no rows were affected by the query,
=item *
C<changes> - at least one row was affected by the query,
=item *
C<no_rows> - no rows were returned in the result-set,
=item *
C<rows> - at least one row was returned in the result-set.
=back
When C<status_code> is prefixed with C<=> sign then original response body is
send to the client instead of the default error page for given C<status_code>.
By design both C<no_changes> and C<changes> apply only to C<INSERT>,
C<UPDATE>, C<DELETE>, C<MOVE>, C<FETCH> and C<COPY> SQL queries.
This directive can be used more than once within same context.
=head2 postgres_output
=over
=item *
B<syntax>: C<postgres_output rds|text|value|binary_value|none>
=item *
B<default>: C<rds>
=item *
B<context>: C<http>, C<server>, C<location>, C<if location>
=back
Set output format:
=over
=item *
C<rds> - return all values from the result-set in C<rds> format
(with appropriate C<Content-Type>),
=item *
C<text> - return all values from the result-set in text format
(with default C<Content-Type>), values are separated by new line,
=item *
C<value> - return single value from the result-set in text format
(with default C<Content-Type>),
=item *
C<binary_value> - return single value from the result-set in binary format
(with default C<Content-Type>),
=item *
C<none> - don't return anything, this should be used only when
extracting values with C<postgres_set> for use with other modules (without
C<Content-Type>).
=back
=head2 postgres_set
=over
=item *
B<syntax>: C<postgres_set $variable row column [optional|required]>
=item *
B<default>: C<none>
=item *
B<context>: C<http>, C<server>, C<location>
=back
Get single value from the result-set and keep it in $variable.
When requirement level is set to C<required> and value is either out-of-range,
C<NULL> or zero-length, then nginx returns C<500 Internal Server Error> response.
Such condition is silently ignored when requirement level is set to C<optional>
(default).
Row and column numbers start at 0. Column name can be used instead of column
number.
This directive can be used more than once within same context.
=head2 postgres_escape
=over
=item *
B<syntax>: C<postgres_escape $escaped [[=]$unescaped]>
=item *
B<default>: C<none>
=item *
B<context>: C<http>, C<server>, C<location>
=back
Escape and quote C<$unescaped> string. Result is stored in C<$escaped> variable
which can be safely used in SQL queries.
Because nginx cannot tell the difference between empty and non-existing strings,
all empty strings are by default escaped to C<NULL> value. This behavior can be
disabled by prefixing C<$unescaped> string with C<=> sign.
=head2 postgres_connect_timeout
=over
=item *
B<syntax>: C<postgres_connect_timeout timeout>
=item *
B<default>: C<10s>
=item *
B<context>: C<http>, C<server>, C<location>
=back
Set timeout for connecting to the database.
=head2 postgres_result_timeout
=over
=item *
B<syntax>: C<postgres_result_timeout timeout>
=item *
B<default>: C<30s>
=item *
B<context>: C<http>, C<server>, C<location>
=back
Set timeout for receiving result from the database.
=head1 Configuration variables
=head2 $postgres_columns
Number of columns in received result-set.
=head2 $postgres_rows
Number of rows in received result-set.
=head2 $postgres_affected
Number of rows affected by C<INSERT>, C<UPDATE>, C<DELETE>, C<MOVE>, C<FETCH>
or C<COPY> SQL query.
=head2 $postgres_query
SQL query, as seen by C<PostgreSQL> database.
=head1 Sample configurations
=head2 Sample configuration #1
Return content of table C<cats> (in C<rds> format).
http {
upstream database {
postgres_server 127.0.0.1 dbname=test
user=test password=test;
}
server {
location / {
postgres_pass database;
postgres_query "SELECT * FROM cats";
}
}
}
=head2 Sample configuration #2
Return only those rows from table C<sites> that match C<host> filter which
is evaluated for each request based on its C<$http_host> variable.
http {
upstream database {
postgres_server 127.0.0.1 dbname=test
user=test password=test;
}
server {
location / {
postgres_pass database;
postgres_query SELECT * FROM sites WHERE host='$http_host'";
}
}
}
=head2 Sample configuration #3
Pass request to the backend selected from the database (traffic router).
http {
upstream database {
postgres_server 127.0.0.1 dbname=test
user=test password=test;
}
server {
location / {
eval_subrequest_in_memory off;
eval $backend {
postgres_pass database;
postgres_query "SELECT * FROM backends LIMIT 1";
postgres_output value 0 0;
}
proxy_pass $backend;
}
}
}
Required modules (other than C<ngx_postgres>):
=over
=item *
L<nginx-eval-module (agentzh's fork)|http://github.com/agentzh/nginx-eval-module>,
=back
=head2 Sample configuration #4
Restrict access to local files by authenticating against C<PostgreSQL> database.
http {
upstream database {
postgres_server 127.0.0.1 dbname=test
user=test password=test;
}
server {
location = /auth {
internal;
postgres_escape $user $remote_user;
postgres_escape $pass $remote_passwd;
postgres_pass database;
postgres_query "SELECT login FROM users WHERE login=$user AND pass=$pass";
postgres_rewrite no_rows 403;
postgres_output none;
}
location / {
auth_request /auth;
root /files;
}
}
}
Required modules (other than C<ngx_postgres>):
=over
=item *
L<ngx_http_auth_request_module|http://mdounin.ru/hg/ngx_http_auth_request_module/>,
=item *
L<ngx_coolkit|http://github.com/FRiCKLE/ngx_coolkit>.
=back
=head2 Sample configuration #5
Simple RESTful webservice returning JSON responses with appropriate HTTP status
codes.
http {
upstream database {
postgres_server 127.0.0.1 dbname=test
user=test password=test;
}
server {
set $random 123;
location = /numbers/ {
postgres_pass database;
rds_json on;
postgres_query HEAD GET "SELECT * FROM numbers";
postgres_query POST "INSERT INTO numbers VALUES('$random') RETURNING *";
postgres_rewrite POST changes 201;
postgres_query DELETE "DELETE FROM numbers";
postgres_rewrite DELETE no_changes 204;
postgres_rewrite DELETE changes 204;
}
location ~ /numbers/(?<num>\d+) {
postgres_pass database;
rds_json on;
postgres_query HEAD GET "SELECT * FROM numbers WHERE number='$num'";
postgres_rewrite HEAD GET no_rows 410;
postgres_query PUT "UPDATE numbers SET number='$num' WHERE number='$num' RETURNING *";
postgres_rewrite PUT no_changes 410;
postgres_query DELETE "DELETE FROM numbers WHERE number='$num'";
postgres_rewrite DELETE no_changes 410;
postgres_rewrite DELETE changes 204;
}
}
}
Required modules (other than C<ngx_postgres>):
=over
=item *
L<ngx_rds_json|http://github.com/agentzh/rds-json-nginx-module>.
=back
=head2 Sample configuration #6
Use GET parameter in SQL query.
location /quotes {
set_unescape_uri $txt $arg_txt;
postgres_escape $txt;
postgres_pass database;
postgres_query "SELECT * FROM quotes WHERE quote=$txt";
}
Required modules (other than C<ngx_postgres>):
=over
=item *
L<ngx_set_misc|http://github.com/agentzh/set-misc-nginx-module>.
=back
=head1 Testing
C<ngx_postgres> comes with complete test suite based on L<Test::Nginx|http://github.com/agentzh/test-nginx>.
You can test core functionality by running:
C<$ TEST_NGINX_IGNORE_MISSING_DIRECTIVES=1 prove>
You can also test interoperability with following modules:
=over
=item *
L<ngx_coolkit|http://github.com/FRiCKLE/ngx_coolkit>,
=item *
L<ngx_echo|github.com/agentzh/echo-nginx-module>,
=item *
L<ngx_form_input|http://github.com/calio/form-input-nginx-module>,
=item *
L<ngx_set_misc|http://github.com/agentzh/set-misc-nginx-module>,
=item *
L<ngx_http_auth_request_module|http://mdounin.ru/hg/ngx_http_auth_request_module/>,
=item *
L<nginx-eval-module (agentzh's fork)|http://github.com/agentzh/nginx-eval-module>,
=item *
L<ngx_rds_json|http://github.com/agentzh/rds-json-nginx-module>.
=back
by running:
C<$ prove>
=head1 License
Copyright (c) 2010-2017, FRiCKLE Piotr Sikora <info@frickle.com>
Copyright (c) 2009-2017, Xiaozhe Wang <chaoslawful@gmail.com>
Copyright (c) 2009-2017, Yichun Zhang <agentzh@gmail.com>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This software includes also parts of the code from:
=over
=item *
C<nginx> (copyrighted by B<Igor Sysoev> under BSD license),
=item *
C<ngx_http_upstream_keepalive> module (copyrighted by B<Maxim Dounin>
under BSD license).
=back
=head1 See also
=over
=item *
L<ngx_rds_json|http://github.com/agentzh/rds-json-nginx-module>,
=item *
L<ngx_drizzle|http://github.com/chaoslawful/drizzle-nginx-module>,
=item *
L<ngx_lua|http://github.com/chaoslawful/lua-nginx-module>,
=item *
L<nginx-eval-module (agentzh's fork)|http://github.com/agentzh/nginx-eval-module>.
=back
|