~ubuntu-branches/ubuntu/trusty/nginx/trusty-updates

« back to all changes in this revision

Viewing changes to debian/modules/nginx-lua/doc/HttpLuaModule.wiki

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry, Cyril Lavier, Michael Lustfield, Kartik Mistry
  • Date: 2012-05-14 11:15:00 UTC
  • mfrom: (4.2.49 sid)
  • Revision ID: package-import@ubuntu.com-20120514111500-1y9ij7zulu9xnmry
Tags: 1.2.0-1
[Cyril Lavier]
* New upstream release. (Closes: #670306)
  + 1.2.x is stable release now.
* debian/modules/chunkin-nginx-module:
  + Updated chunkin-nginx-module to v0.23rc2-3-g85eca98.
* debian/modules/headers-more-module:
  + Updated headers-more-module to v0.17rc1-4-g33a82ed.
* debian/modules/nginx-development-kit:
  + Updated nginx-development-kit to v0.2.17-7-g24202b4.
* debian/modules/nginx-echo:
  + Updated nginx-echo to v0.38rc2-7-g080c0a1.
* debian/modules/nginx-lua:
  + Updated nginx-lua to v0.5.0rc25-5-g8d28785.
* debian/modules/nginx-upstream-fair:
  + Updated nginx-upstream-fair to a18b409.
* debian/modules/nginx-upload-progress:
  + Updated nginx-upload-progress to v0.9.0-0-ga788dea.
* debian/modules/naxsi:
  + Updated naxsi to 0.46
* debian/modules/README.Modules-versions:
  + Updated versions and URLs for modules.
* debian/naxsi-ui-extract, debian/naxsi-ui-intercept,
  debian/nginx-naxsi-ui.*, debian/naxsi-ui-extract.1,
  debian/naxsi-ui-intercept.1, debian/rules:
  + Added nginx-naxsi-ui package containing the learning daemon
    and the WebUI.
* debian/nginx-common.nginx.default, debian/nginx-common.nginx.init:
  + Renamed files to be compliant with the nginx-naxsi-ui package.
* debian/po:
  + Added needed files for using po-debconf.
  + Added French translation.
* debian/control:
  + Applied the modifications given after the review by Justin Rye.

[Michael Lustfield]
* debian/conf/uwsgi_params:
  + Added UWSGI_SCHEME to uwsgi_params. (Closes: #664878)
* debian/conf/sites-available/default:
  + Added allow directive for ipv6 localhost. (Closes: #664271)

[Kartik Mistry]
* debian/control:
  + wrap-and-sort.
* debian/copyright:
  + Added missing copyrights, minor formatting fixes.
* debian/nginx-common.nginx.init:
  + Added ulimit for restarts, Thanks to Daniel Roschka
    <danielroschka@phoenitydawn.de> for patch. (Closes: #673580)
* debian/conf/sites-available/default:
  + Added patch to fix deprecated "listen" directive, Thanks to
    Guillaume Plessis <gui@dotdeb.org> for patch. (Closes: #672632)

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 
11
11
= Version =
12
12
 
13
 
This document describes ngx_lua [https://github.com/chaoslawful/lua-nginx-module/tags v0.4.1] released on 1 February 2012.
 
13
This document describes ngx_lua [https://github.com/chaoslawful/lua-nginx-module/tags v0.5.0rc24] released on 18 April 2012.
14
14
 
15
15
= Synopsis =
16
16
<geshi lang="nginx">
56
56
            # MIME type determined by default_type:
57
57
            default_type 'text/plain';
58
58
 
59
 
            content_by_lua "ngx.say('Hello,world!')"
 
59
            content_by_lua "ngx.say('Hello,world!')";
60
60
        }
61
61
 
62
62
         location /nginx_var {
368
368
 
369
369
'''phase:''' ''rewrite tail''
370
370
 
371
 
Acts as a rewrite phase handler and executes lua code string specified in <code><lua-script-str></code> for every request.
 
371
Acts as a rewrite phase handler and executes Lua code string specified in <code><lua-script-str></code> for every request.
372
372
The Lua code may make [[#Nginx API for Lua|API calls]] and is executed as a new spawned coroutine in an independent global environment (i.e. a sandbox).
373
373
 
374
374
Note that this handler always runs ''after'' the standard [[HttpRewriteModule]]. So the following will work as expected:
638
638
 
639
639
'''default:''' ''no''
640
640
 
641
 
'''context:''' ''main''
 
641
'''context:''' ''http''
642
642
 
643
643
'''phase:''' ''depends on usage''
644
644
 
657
657
 
658
658
This directive was first introduced in the <code>v0.3.1rc22</code> release.
659
659
 
 
660
== lua_socket_connect_timeout ==
 
661
 
 
662
'''syntax:''' ''lua_socket_connect_timeout <time>''
 
663
 
 
664
'''default:''' ''lua_socket_connect_timeout 60s''
 
665
 
 
666
'''context:''' ''http, server, location''
 
667
 
 
668
This directive controls the default timeout value used in TCP/unix-domain socket object's [[#tcpsock:connect|connect]] method and can be overridden by the [[#tcpsock:settimeout|settimeout]] method.
 
669
 
 
670
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, i.e., "second". The default setting is <code>60s</code>.
 
671
 
 
672
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
673
 
 
674
== lua_socket_send_timeout ==
 
675
 
 
676
'''syntax:''' ''lua_socket_send_timeout <time>''
 
677
 
 
678
'''default:''' ''lua_socket_send_timeout 60s''
 
679
 
 
680
'''context:''' ''http, server, location''
 
681
 
 
682
Controls the default timeout value used in TCP/unix-domain socket object's [[#tcpsock:send|send]] method and can be overridden by the [[#tcpsock:settimeout|settimeout]] method.
 
683
 
 
684
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, i.e., "second". The default setting is <code>60s</code>.
 
685
 
 
686
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
687
 
 
688
== lua_socket_send_lowat ==
 
689
 
 
690
'''syntax:''' ''lua_socket_send_lowat <size>''
 
691
 
 
692
'''default:''' ''lua_socket_send_lowat 0''
 
693
 
 
694
'''context:''' ''http, server, location''
 
695
 
 
696
Controls the <code>lowat</code> (low water) value for the cosocket send buffer.
 
697
 
 
698
== lua_socket_read_timeout ==
 
699
 
 
700
'''syntax:''' ''lua_socket_read_timeout <time>''
 
701
 
 
702
'''default:''' ''lua_socket_read_timeout 60s''
 
703
 
 
704
'''context:''' ''http, server, location''
 
705
 
 
706
'''phase:''' ''depends on usage''
 
707
 
 
708
This directive controls the default timeout value used in TCP/unix-domain socket object's [[#tcpsock:receive|receive]] method and iterator functions returned by the [[#tcpsock:receiveuntil|receiveuntil]] method. This setting can be overridden by the [[#tcpsock:settimeout|settimeout]] method.
 
709
 
 
710
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, i.e., "second". The default setting is <code>60s</code>.
 
711
 
 
712
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
713
 
 
714
== lua_socket_buffer_size ==
 
715
 
 
716
'''syntax:''' ''lua_socket_buffer_size <size>''
 
717
 
 
718
'''default:''' ''lua_socket_buffer_size 4k/8k''
 
719
 
 
720
'''context:''' ''http, server, location''
 
721
 
 
722
Specifies the buffer size used by cosocket reading operations.
 
723
 
 
724
This buffer does not have to be that big to hold everything at the same time because cosocket supports 100% non-buffered reading and parsing. So even <code>1</code> byte buffer size should still work everywhere but the performance could be bad.
 
725
 
 
726
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
727
 
 
728
== lua_socket_pool_size ==
 
729
 
 
730
'''syntax:''' ''lua_socket_pool_size <size>''
 
731
 
 
732
'''default:''' ''lua_socket_pool_size 30''
 
733
 
 
734
'''context:''' ''http, server, location''
 
735
 
 
736
Specifies the size limit (in terms of connection count) for every cosocket connection pool associated with every remote server (i.e., identified by either the host-port pair or the unix domain socket file path).
 
737
 
 
738
Default to 30 connections for every pool.
 
739
 
 
740
When the connection pool is exceeding the size limit, the least recently used (idle) connection already in the pool will be closed automatically to make room for the current connection. 
 
741
 
 
742
Note that the cosocket connection pool is per nginx worker process rather than per nginx server instance, so so size limit specified here also applies to every single nginx worker process.
 
743
 
 
744
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
745
 
 
746
== lua_socket_keepalive_timeout ==
 
747
 
 
748
'''syntax:''' ''lua_socket_keepalive_timeout <time>''
 
749
 
 
750
'''default:''' ''lua_socket_keepalive_timeout 60s''
 
751
 
 
752
'''context:''' ''http, server, location''
 
753
 
 
754
This directive controls the default maximal idle time of the connections in the cosocket built-in connection pool. When this timeout reaches, idle connections will be closed automatically and removed from the pool. This setting can be overridden by cosocket objects' [[#tcpsock:setkeepalive|setkeepalive]] method.
 
755
 
 
756
The <code><time></code> argument can be an integer, with an optional time unit, like <code>s</code> (second), <code>ms</code> (millisecond), <code>m</code> (minute). The default time unit is <code>s</code>, ie, "second". The default setting is <code>60s</code>.
 
757
 
 
758
This directive was first introduced in the <code>v0.5.0rc1</code> release.
 
759
 
 
760
== lua_http10_buffering ==
 
761
 
 
762
'''syntax:''' ''lua_http10_buffering on|off''
 
763
 
 
764
'''default:''' ''lua_http10_buffering on''
 
765
 
 
766
'''context:''' ''http, server, location, location-if''
 
767
 
 
768
Enables or disables the automatic response caching for HTTP 1.0 (or older) requests. This buffering mechanism is mainly used for HTTP 1.0 keep-alive which replies on a proper <code>Content-Length</code> response header.
 
769
 
 
770
If the Lua code explicitly sets a <code>Content-Length</code> response header before sending the headers (either explicity via [[#ngx.send_headers|ngx.send_headers]] or implicitly via the first [[#ngx.say|ngx.say]] or [[#ngx.print|ngx.print]] call).
 
771
 
 
772
If you want to output huge response data in a streaming fashion (via the [[#ngx.flush|ngx.flush]] call, for example), then you MUST turn off this directive to prevent memory footprint boost.
 
773
 
 
774
This directive is turned <code>on</code> by default.
 
775
 
 
776
THis directive was first introduced in the <code>v0.5.0rc19</code> release.
 
777
 
660
778
= Nginx API for Lua =
661
779
== Introduction ==
662
780
The various <code>*_by_lua</code> and <code>*_by_lua_file</code> configuration directives serve as gateways to the Lua API within the <code>nginx.conf</code> file. The Nginx Lua API described below can only be called within the user Lua code run in the context of these configuration directives.
766
884
  ngx.ERROR (-1)
767
885
  ngx.AGAIN (-2)
768
886
  ngx.DONE (-4)
769
 
</geshi>
770
 
 
771
 
Note that only two of these constants are utilized by the [[#Nginx API for Lua|Nginx API for Lua]] (i.e., [[#ngx.exit|ngx.exit]] accepts <code>NGX_OK</code> and <code>NGX_ERROR</code> as input).
 
887
  ngx.DECLINED (-5)
 
888
</geshi>
 
889
 
 
890
Note that only three of these constants are utilized by the [[#Nginx API for Lua|Nginx API for Lua]] (i.e., [[#ngx.exit|ngx.exit]] accepts <code>NGX_OK</code>, <code>NGX_ERROR</code>, and <code>NGX_DECLINED</code> as input).
 
891
 
 
892
<geshi lang="lua">
 
893
  ngx.null
 
894
</geshi>
 
895
 
 
896
The <code>ngx.null</code> constant is a <code>NULL</code> light userdata which is usually used to represent nil values in Lua tables and etc. It is identical with the [http://www.kyne.com.au/~mark/software/lua-cjson.php lua-cjson] library's <code>cjson.null</code> constant. This constant was first introduced in the <code>v0.5.0rc5</code> release.
 
897
 
 
898
The <code>ngx.DECLINED</code> constant was first introduced in the <code>v0.5.0rc19</code> release.
772
899
 
773
900
== HTTP method constants ==
774
901
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
775
902
 
776
 
<geshi lang="lua">
 
903
<geshi lang="text">
777
904
  ngx.HTTP_GET
778
905
  ngx.HTTP_HEAD
779
906
  ngx.HTTP_PUT
780
907
  ngx.HTTP_POST
781
908
  ngx.HTTP_DELETE
 
909
  ngx.HTTP_OPTIONS   (first introduced in the v0.5.0rc24 release)
782
910
</geshi>
783
911
 
784
 
These constants are usually used in [[#ngx.location.capture|ngx.location.catpure]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] method calls.
 
912
These constants are usually used in [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] method calls.
785
913
 
786
914
== HTTP status constants ==
787
915
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
836
964
    ngx.log(ngx.NOTICE, 'lua print: ', a, b, ...)
837
965
</geshi>
838
966
 
839
 
Lua <code>nil</code> arguments are accepted and result in literal <code>"nil"</code> strings while Lua booleans result in literal <code>"true"</code> or <code>"false"</code> strings.
 
967
Lua <code>nil</code> arguments are accepted and result in literal <code>"nil"</code> strings while Lua booleans result in literal <code>"true"</code> or <code>"false"</code> strings. And the <code>ngx.null</code> constant will yield the <code>"null"</code> string output.
 
968
 
 
969
There is a hard-coded length limitation on the error messages in the Nginx core. It is <code>2048</code> bytes at most, including the trailing newlines and the leading timestamps. You can manually modify this limit by modifying the <code>NGX_MAX_ERROR_STR</code> macro definition in the <code>src/core/ngx_log.h</code> file in the Nginx source tree. If the message size exceeds this limit, the Nginx core will truncate the message text automatically.
840
970
 
841
971
== ngx.ctx ==
842
972
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
1189
1319
in gzipped responses that cannot be handled properly in Lua code. Original request headers should be ignored by setting 
1190
1320
[[HttpProxyModule#proxy_pass_request_headers|proxy_pass_request_headers]] to <code>off</code> in subrequest locations.
1191
1321
 
 
1322
There is a hard-coded upper limit on the number of concurrent subrequests every main request. In older versions of Nginx, the limit is <code>50</code>, which is then increased to <code>200</code> in recent Nginx <code>1.1.x</code> releases. You can manually edit this limit by modifying the definition of the <code>NGX_HTTP_MAX_SUBREQUESTS</code> macro in the <code>nginx/src/http/ngx_http_request.h</code> file in the Nginx source tree. When you are exceeding this limit, you will get the following error message in your <code>error.log</code> file:
 
1323
 
 
1324
<geshi lang="text">
 
1325
    [error] 13983#0: *1 subrequests cycle while processing "/uri"
 
1326
</geshi>
 
1327
 
1192
1328
Please also refer to restrictions on [[#Locations_With_HttpEchoModule_Directives|capturing locations that include Echo Module directives]].
1193
1329
 
1194
1330
== ngx.location.capture_multi ==
1322
1458
 
1323
1459
Underscores (<code>_</code>) in the header names will also be replaced by dashes (<code>-</code>) and the header names will be matched case-insensitively. If the response header is not present at all, <code>nil</code> will be returned.
1324
1460
 
1325
 
This is particularly useful in the context of [[#filter_header_by_lua|filter_header_by_lua]] and [[#filter_header_by_lua_file|filter_header_by_lua_file]], for example,
 
1461
This is particularly useful in the context of [[#header_filter_by_lua|header_filter_by_lua]] and [[#header_filter_by_lua_file|header_filter_by_lua_file]], for example,
1326
1462
 
1327
1463
<geshi lang="nginx">
1328
1464
    location /test {
1464
1600
 
1465
1601
where in the latter case, this method will automatically escape argument keys and values according to the URI escaping rule.
1466
1602
 
 
1603
Multi-value arguments are also supported:
 
1604
 
 
1605
<geshi lang="lua">
 
1606
    ngx.req.set_uri_args({ a = 3, b = {5, 6} })
 
1607
</geshi>
 
1608
 
 
1609
which will result in a querystring like <code>a=3&b=5&b=6</code>.
 
1610
 
1467
1611
This interface was first introduced in the <code>v0.3.1rc13</code> release.
1468
1612
 
1469
1613
See also [[#ngx.req.set_uri|ngx.req.set_uri]].
1555
1699
== ngx.req.get_post_args ==
1556
1700
'''syntax:''' ''ngx.req.get_post_args(count_limit?)''
1557
1701
 
1558
 
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
 
1702
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
1559
1703
 
1560
1704
Returns a Lua table holding all the current request POST query arguments (of the MIME type <code>application/x-www-form-urlencoded</code>). Call [[#ngx.req.read_body|ngx.req.read_body]] to read the request body first or turn on the [[#lua_need_request_body|lua_need_request_body]] directive to avoid Lua exception errors.
1561
1705
 
1854
1998
 
1855
1999
See also [[#ngx.req.set_body_data|ngx.req.set_body_data]].
1856
2000
 
 
2001
== ngx.req.socket ==
 
2002
'''syntax:''' ''tcpsock, err = ngx.req.socket()''
 
2003
 
 
2004
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
2005
 
 
2006
Returns a read-only cosocket object that wraps the downstream connection. Only [[#tcpsock:receive|receive]] and [[#tcpsock:receiveuntil|receiveuntil]] methods are supported on this object.
 
2007
 
 
2008
In case of error, <code>nil</code> will be returned as well as a string describing the error.
 
2009
 
 
2010
The socket object returned by this method is usually used to read the current request's body in a streaming fashion. Do not turn on the [[#lua_need_request_body|lua_need_request_body]] directive, and do not mix this call with [[#ngx.req.read_body|ngx.req.read_body]] and [[#ngx.req.discard_body|ngx.req.discard_body]].
 
2011
 
 
2012
If there is any request body data that has been preread into the Nginx core's request header buffer, the resulting cosocket object will take care of that automatically. So there will not be any data loss due to potential body data prereading.
 
2013
 
 
2014
This function was first introduced in the <code>v0.5.0rc1</code> release.
 
2015
 
1857
2016
== ngx.req.clear_header ==
1858
2017
'''syntax:''' ''ngx.req.clear_header(header_name)''
1859
2018
 
2003
2162
 
2004
2163
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
2005
2164
 
2006
 
Emit arguments concatenated to the HTTP client (as response body). If response headers have not been sent, this function will send headers out first and then output body data.
 
2165
Emits arguments concatenated to the HTTP client (as response body). If response headers have not been sent, this function will send headers out first and then output body data.
2007
2166
 
2008
2167
Lua <code>nil</code> values will output <code>"nil"</code> strings and Lua boolean values will output <code>"true"</code> and <code>"false"</code> literal strings respectively.
2009
2168
 
2026
2185
 
2027
2186
Non-array table arguments will cause a Lua exception to be thrown.
2028
2187
 
 
2188
The <code>ngx.null</code> constant will yield the <code>"null"</code> string output.
 
2189
 
2029
2190
This is an asynchronous call and will return immediately without waiting for all the data to be written into the system send buffer. To run in synchronous mode, call <code>ngx.flush(true)</code> after calling <code>ngx.print</code>. This can be particularly useful for streaming output. See [[#ngx.flush|ngx.flush]] for more details.
2030
2191
 
2031
2192
== ngx.say ==
2042
2203
 
2043
2204
Log arguments concatenated to error.log with the given logging level.
2044
2205
 
2045
 
Lua <code>nil</code> arguments are accepted and result in literal <code>"nil"</code> string while Lua booleans result in literal <code>"true"</code> or <code>"false"</code> string outputs.
 
2206
Lua <code>nil</code> arguments are accepted and result in literal <code>"nil"</code> string while Lua booleans result in literal <code>"true"</code> or <code>"false"</code> string outputs. And the <code>ngx.null</code> constant will yield the <code>"null"</code> string output.
2046
2207
 
2047
2208
The <code>log_level</code> argument can take constants like <code>ngx.ERR</code> and <code>ngx.WARN</code>. Check out [[#Nginx log level constants|Nginx log level constants]] for details.
2048
2209
 
 
2210
There is a hard-coded length limitation on the error messages in the Nginx core. It is <code>2048</code> bytes at most, including the trailing newlines and the leading timestamps. You can manually modify this limit by modifying the <code>NGX_MAX_ERROR_STR</code> macro definition in the <code>src/core/ngx_log.h</code> file in the Nginx source tree. If the message size exceeds this limit, the Nginx core will truncate the message text automatically.
 
2211
 
2049
2212
== ngx.flush ==
2050
2213
'''syntax:''' ''ngx.flush(wait?)''
2051
2214
 
2290
2453
 
2291
2454
See [[#ngx.md5|ngx.md5]] if the hexadecimal form of the MD5 digest is required.
2292
2455
 
 
2456
== ngx.sha1_bin ==
 
2457
'''syntax:''' ''digest = ngx.sha1_bin(str)''
 
2458
 
 
2459
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
 
2460
 
 
2461
Returns the binary form of the SHA-1 digest of the <code>str</code> argument.
 
2462
 
 
2463
This function requires enabling SHA-1 support in your Nginx build (usually you just need to install OpenSSL to your system while building Nginx).
 
2464
 
 
2465
This function was first introduced in the <code>v0.5.0rc6</code>.
 
2466
 
2293
2467
== ngx.today ==
2294
2468
'''syntax:''' ''str = ngx.today()''
2295
2469
 
2628
2802
* [[#ngx.shared.DICT.replace|replace]]
2629
2803
* [[#ngx.shared.DICT.incr|incr]]
2630
2804
* [[#ngx.shared.DICT.delete|delete]]
 
2805
* [[#ngx.shared.DICT.flush_all|flush_all]]
2631
2806
 
2632
2807
Here is an example:
2633
2808
 
2674
2849
This feature was first introduced in the <code>v0.3.1rc22</code> release.
2675
2850
 
2676
2851
== ngx.shared.DICT.get ==
2677
 
'''syntax:''' ''value = ngx.shared.DICT:get(key)''
 
2852
'''syntax:''' ''value, flags = ngx.shared.DICT:get(key)''
2678
2853
 
2679
2854
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
2680
2855
 
2686
2861
 
2687
2862
<geshi lang="lua">
2688
2863
    local cats = ngx.shared.cats
2689
 
    local value = cats.get(cats, "Marry")
 
2864
    local value, flags = cats.get(cats, "Marry")
2690
2865
</geshi>
2691
2866
 
2692
2867
or use Lua's syntactic sugar for method calls:
2693
2868
 
2694
2869
<geshi lang="lua">
2695
2870
    local cats = ngx.shared.cats
2696
 
    local value = cats:get("Marry")
 
2871
    local value, flags = cats:get("Marry")
2697
2872
</geshi>
2698
2873
 
2699
2874
These two forms are fundamentally equivalent.
2700
2875
 
 
2876
If the user flags is <code>0</code> (the default), then no flags value will be returned.
 
2877
 
2701
2878
This feature was first introduced in the <code>v0.3.1rc22</code> release.
2702
2879
 
2703
2880
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
2704
2881
 
2705
2882
== ngx.shared.DICT.set ==
2706
 
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:set(key, value, exptime?)''
 
2883
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:set(key, value, exptime?, flags?)''
2707
2884
 
2708
2885
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
2709
2886
 
2717
2894
 
2718
2895
The optional <code>exptime</code> argument specifies expiration time (in seconds) for the inserted key-value pair. The time resolution is <code>0.001</code> seconds. If the <code>exptime</code> takes the value <code>0</code> (which is the default), then the item will never be expired.
2719
2896
 
 
2897
The optional <code>flags</code> argument specifies a user flags value associated with the entry to be stored. It can also be retrieved later with the value. The user flags is stored as an unsigned 32-bit integer internally. Defaults to <code>0</code>. The user flags argument was first introduced in the <code>v0.5.0rc2</code> release.
 
2898
 
2720
2899
When it fails to allocate memory for the current key-value item, then <code>set</code> will try removing existing items in the storage according to the Least-Recently Used (LRU) algorithm. Note that, LRU takes priority over expiration time here. If up to tens of existing items have been removed and the storage left is still insufficient (either due to the total capacity limit specified by [[#lua_shared_dict|lua_shared_dict]] or memory segmentation), then the <code>err</code> return value will be <code>no memory</code> and <code>success</code> will be <code>false</code>.
2721
2900
 
2722
2901
If this method succeeds in storing the current item by forcibly removing other not-yet-expired items in the dictionary via LRU, the <code>forcible</code> return value will be <code>true</code>. If it stores the item without forcibly removing other valid items, then the return value <code>forcible</code> will be <code>false</code>.
2742
2921
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
2743
2922
 
2744
2923
== ngx.shared.DICT.add ==
2745
 
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:add(key, value, exptime?)''
 
2924
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:add(key, value, exptime?, flags?)''
2746
2925
 
2747
2926
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
2748
2927
 
2755
2934
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
2756
2935
 
2757
2936
== ngx.shared.DICT.replace ==
2758
 
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:replace(key, value, exptime?)''
 
2937
'''syntax:''' ''success, err, forcible = ngx.shared.DICT:replace(key, value, exptime?, flags?)''
2759
2938
 
2760
2939
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
2761
2940
 
2797
2976
 
2798
2977
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
2799
2978
 
 
2979
== ngx.shared.DICT.flush_all ==
 
2980
'''syntax:''' ''ngx.shared.DICT:flush_all()''
 
2981
 
 
2982
'''context:''' ''set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*''
 
2983
 
 
2984
Flushes out all the items in the dictionary.
 
2985
 
 
2986
This feature was first introduced in the <code>v0.5.0rc17</code> release.
 
2987
 
 
2988
See also [[#ngx.shared.DICT|ngx.shared.DICT]].
 
2989
 
 
2990
== ngx.socket.tcp ==
 
2991
'''syntax:''' ''tcpsock = ngx.socket.tcp()''
 
2992
 
 
2993
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
2994
 
 
2995
Creates and returns a TCP (or Unix Domain) socket object (also known as the "cosocket" object). The following methods are supported on this object:
 
2996
 
 
2997
* [[#tcpsock:connect|connect]]
 
2998
* [[#tcpsock:send|send]]
 
2999
* [[#tcpsock:receive|receive]]
 
3000
* [[#tcpsock:close|close]]
 
3001
* [[#tcpsock:settimeout|settimeout]]
 
3002
* [[#tcpsock:setoption|setoption]]
 
3003
* [[#tcpsock:receiveuntil|receiveuntil]]
 
3004
* [[#tcpsock:setkeepalive|setkeepalive]]
 
3005
* [[#tcpsock:getreusedtimes|getreusedtimes]]
 
3006
 
 
3007
It is intended to be compatible with the TCP API of the [http://w3.impa.br/~diego/software/luasocket/tcp.html LuaSocket] library but is 100% nonblocking out of the box. Also, we introduce some new APIs to provide more functionalities.
 
3008
 
 
3009
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3010
 
 
3011
== tcpsock:connect ==
 
3012
'''syntax:''' ''ok, err = tcpsock:connect(host, port)''
 
3013
 
 
3014
'''syntax:''' ''ok, err = tcpsock:connect("unix:/path/to/unix-domain.socket")''
 
3015
 
 
3016
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3017
 
 
3018
Attempts to connect a TCP socket object to a remote server or to a unix domain socket file nonblockingly.
 
3019
 
 
3020
Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method (or the [[#ngx.socket.connect|ngx.socket.connect]] function).
 
3021
 
 
3022
Both IP addresses and domain names can be specified as the <code>host</code> argument. In case of domain names, this method will use Nginx core's dynamic resolver to parse the domain name nonblockingly and it is required to configure the [[HttpCoreModule#resolver|resolver]] directive in your <code>nginx.conf</code> file like this:
 
3023
 
 
3024
<geshi lang="nginx">
 
3025
    resolver 8.8.8.8;  # use Google's public DNS nameserver
 
3026
</geshi>
 
3027
 
 
3028
If the nameserver returns multiple IP addresses for the host name, this method will pick up one randomly.
 
3029
 
 
3030
In case of error, the method returns <code>nil</code> followed by a string describing the error. In case of success, the method returns <code>1</code>.
 
3031
 
 
3032
Here is an example for connecting to a TCP server:
 
3033
 
 
3034
<geshi lang="nginx">
 
3035
    location /test {
 
3036
        resolver 8.8.8.8;
 
3037
 
 
3038
        content_by_lua '
 
3039
            local sock = ngx.socket.tcp()
 
3040
            local ok, err = sock:connect("www.google.com", 80)
 
3041
            if not ok then
 
3042
                ngx.say("failed to connect to google: ", err)
 
3043
                return
 
3044
            end
 
3045
            ngx.say("successfully connected to google!")
 
3046
            sock:close()
 
3047
        ';
 
3048
    }
 
3049
</geshi>
 
3050
 
 
3051
Connecting to a Unix Domain Socket file is also possible:
 
3052
 
 
3053
<geshi lang="lua">
 
3054
    local sock = ngx.socket.tcp()
 
3055
    local ok, err = sock:connect("unix:/tmp/memcached.sock")
 
3056
    if not ok then
 
3057
        ngx.say("failed to connect to the memcached unix domain socket: ", err)
 
3058
        return
 
3059
    end
 
3060
</geshi>
 
3061
 
 
3062
assuming that your memcached (or something else) is listening on the unix domain socket file <code>/tmp/memcached.sock</code>.
 
3063
 
 
3064
Timeout for the connecting operation is controlled by the [[#lua_socket_connect_timeout|lua_socket_connect_timeout]] config directive and the [[#tcpsock:settimeout|settimeout]] method. And the latter takes priority. For example:
 
3065
 
 
3066
<geshi lang="lua">
 
3067
    local sock = ngx.socket.tcp()
 
3068
    sock:settimeout(1000)  -- one second timeout
 
3069
    local ok, err = sock:connect(host, port)
 
3070
</geshi>
 
3071
 
 
3072
It is important here to call the [[#tcpsock:settimeout|settimeout]] method ''before'' calling this method.
 
3073
 
 
3074
Calling this method on an already connected socket object will cause the original connection to be closed first.
 
3075
 
 
3076
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3077
 
 
3078
== tcpsock:send ==
 
3079
'''syntax:''' ''bytes, err = tcpsock:send(data)''
 
3080
 
 
3081
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3082
 
 
3083
Sends data nonblockingly on the current TCP or Unix Domain Socket connection.
 
3084
 
 
3085
This method is a synchronous operation that will not return until ''all'' the data has been flushed into the system socket send buffer or an error occurs.
 
3086
 
 
3087
In case of success, it returns the total number of bytes that have been sent. Otherwise, it returns <code>nil</code> and a string describing the error.
 
3088
 
 
3089
The input argument `data` can either be a Lua string or a (nested) Lua table holding string fragments. In case of table arguments, this method will automatically copy all the string elements piece by piece to the underlying Nginx socket send buffers, which is usually optimal than doing string concatenation operations on the Lua land.
 
3090
 
 
3091
Timeout for the sending operation is controlled by the [[#lua_socket_send_timeout|lua_socket_send_timeout]] config directive and the [[#tcpsock:settimeout|settimeout]] method. And the latter takes priority. For example:
 
3092
 
 
3093
<geshi lang="lua">
 
3094
    sock:settimeout(1000)  -- one second timeout
 
3095
    local bytes, err = sock:send(request)
 
3096
</geshi>
 
3097
 
 
3098
It is important here to call the [[#tcpsock:settimeout|settimeout]] method ''before'' calling this method.
 
3099
 
 
3100
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3101
 
 
3102
== tcpsock:receive ==
 
3103
'''syntax:''' ''data, err, partial = tcpsock:receive(size)''
 
3104
 
 
3105
'''syntax:''' ''data, err, partial = tcpsock:receive(pattern?)''
 
3106
 
 
3107
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3108
 
 
3109
Receives data from the connected socket according to the reading pattern or size.
 
3110
 
 
3111
This method is a synchronous operation just like the [[#tcpsock:send|send]] method and is 100% nonblocking.
 
3112
 
 
3113
In case of success, it returns the data received; in case of error, it returns <code>nil</code> with a string describing the error and the partial data received so far.
 
3114
 
 
3115
If a number-like argument is specified (including strings that look like numbers), then it is interpreted as a size. This method will not return until it reads exactly this size of data or an error occurs.
 
3116
 
 
3117
If a non-number-like string argument is specified, then it is interpreted as a "pattern". The following patterns are supported:
 
3118
 
 
3119
* <code>'*a'</code>: reads from the socket until the connection is closed. No end-of-line translation is performed;
 
3120
* <code>'*l'</code>: reads a line of text from the socket. The line is terminated by a <code>Line Feed</code> (LF) character (ASCII 10), optionally preceded by a <code>Carriage Return</code> (CR) character (ASCII 13). The CR and LF characters are not included in the returned line. In fact, all CR characters are ignored by the pattern.
 
3121
 
 
3122
If no argument is specified, then it is assumed to be the pattern <code>'*l'</code>, that is, the line reading pattern.
 
3123
 
 
3124
Timeout for the reading operation is controlled by the [[#lua_socket_read_timeout|lua_socket_read_timeout]] config directive and the [[#tcpsock:settimeout|settimeout]] method. And the latter takes priority. For example:
 
3125
 
 
3126
<geshi lang="lua">
 
3127
    sock:settimeout(1000)  -- one second timeout
 
3128
    local line, err, partial = sock:receive()
 
3129
    if not line then
 
3130
        ngx.say("failed to read a line: ", err)
 
3131
        return
 
3132
    end
 
3133
    ngx.say("successfully read a line: ", line)
 
3134
</geshi>
 
3135
 
 
3136
It is important here to call the [[#tcpsock:settimeout|settimeout]] method ''before'' calling this method.
 
3137
 
 
3138
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3139
 
 
3140
== tcpsock:receiveuntil ==
 
3141
'''syntax:''' ''iterator = tcpsock:receiveuntil(pattern)''
 
3142
 
 
3143
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3144
 
 
3145
This method returns an iterator Lua function that can be called to read the data stream until it sees the specified pattern or an error occurs.
 
3146
 
 
3147
Here is an example for using this method to read a data stream with the boundary sequence <code>--abcedhb</code>:
 
3148
 
 
3149
<geshi lang="lua">
 
3150
    local reader = sock:receiveuntil("\r\n--abcedhb")
 
3151
    local data, err, partial = reader()
 
3152
    if not data then
 
3153
        ngx.say("failed to read the data stream: ", err)
 
3154
    end
 
3155
    ngx.say("read the data stream: ", data)
 
3156
</geshi>
 
3157
 
 
3158
When called without any argument, the iterator function returns the received data right ''before'' the specified pattern string in the incoming data stream. So for the example above, if the incoming data stream is <code>'hello, world! -agentzh\r\n--abcedhb blah blah'</code>, then the string <code>'hello, world! -agentzh'</code> will be returned.
 
3159
 
 
3160
In case of error, the iterator function will return <code>nil</code> along with a string describing the error and the partial data bytes that have been read so far.
 
3161
 
 
3162
The iterator function can be called multiple times and can be mixed safely with other cosocket method calls or other iterator function calls.
 
3163
 
 
3164
The iterator function behaves differently (i.e., like a real iterator) when it is called with a <code>size</code> argument. That is, it will read that <code>size</code> of data at earch invocation and will return <code>nil</code> at the last invocation (either sees the boundary pattern or meets an error). For the last successful invocation of the iterator function, the <code>err</code> return value will be <code>nil</code> too. The iterator function will automatically reset after its last successful invocation that returns <code>nil</code> data and <code>nil</code> error. Consider the following example:
 
3165
 
 
3166
<geshi lang="lua">
 
3167
    local reader = sock:receiveuntil("\r\n--abcedhb")
 
3168
 
 
3169
    while true then
 
3170
        local data, err, partial = reader(4)
 
3171
        if not data then
 
3172
            if err then
 
3173
                ngx.say("failed to read the data stream: ", err)
 
3174
                break
 
3175
            end
 
3176
 
 
3177
            ngx.say("read done")
 
3178
            break
 
3179
        end
 
3180
        ngx.say("read chunk: [", data, "]")
 
3181
    end
 
3182
</geshi>
 
3183
 
 
3184
Then for the incoming data stream <code>'hello, world! -agentzh\r\n--abcedhb blah blah'</code>, we shall get the following output from the sample code above:
 
3185
 
 
3186
<geshi lang="text">
 
3187
    read chunk: [hell]
 
3188
    read chunk: [o, w]
 
3189
    read chunk: [orld]
 
3190
    read chunk: [! -a]
 
3191
    read chunk: [gent]
 
3192
    read chunk: [zh]
 
3193
    read done
 
3194
</geshi>
 
3195
 
 
3196
Note that, the actual data returned ''might'' be a little longer than the size limit specified by the <code>size</code> argument when your boundary pattern has ambiguity for streaming parsing. Near the boundary of the data stream, the data string actually returned could also be shorter than the size limit.
 
3197
 
 
3198
Timeout for the iterator function's reading operation is controlled by the [[#lua_socket_read_timeout|lua_socket_read_timeout]] config directive and the [[#tcpsock:settimeout|settimeout]] method. And the latter takes priority. For example:
 
3199
 
 
3200
<geshi lang="lua">
 
3201
    local readline = sock:receiveuntil("\r\n")
 
3202
 
 
3203
    sock:settimeout(1000)  -- one second timeout
 
3204
    line, err, partial = readline()
 
3205
    if not line then
 
3206
        ngx.say("failed to read a line: ", err)
 
3207
        return
 
3208
    end
 
3209
    ngx.say("successfully read a line: ", line)
 
3210
</geshi>
 
3211
 
 
3212
It is important here to call the [[#tcpsock:settimeout|settimeout]] method ''before'' calling the iterator function (note that the <code>receiveuntil</code> call is irrelevant here).
 
3213
 
 
3214
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3215
 
 
3216
== tcpsock:close ==
 
3217
'''syntax:''' ''ok, err = tcpsock:close()''
 
3218
 
 
3219
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3220
 
 
3221
Closes the current TCP or Unix Domain socket. It returns the <code>1</code> in case of success and returns <code>nil</code> with a string describing the error otherwise.
 
3222
 
 
3223
For socket objects that have invoked the [[#tcpsock:setkeepalive|setkeepalive]] method, there is no need to call this method on it because the socket object is already closed (and the current connection is saved into the builtin connection pool).
 
3224
 
 
3225
For socket objects that have not invoked [[#tcpsock:setkeepalive|setkeepalive]], they (and their connections) will be automatically closed when the socket object is released by the Lua GC (Garbage Collector) or the current client HTTP request finishes processing.
 
3226
 
 
3227
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3228
 
 
3229
== tcpsock:settimeout ==
 
3230
'''syntax:''' ''tcpsock:settimeout(time)''
 
3231
 
 
3232
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3233
 
 
3234
Set the timeout value in milliseconds for subsequent socket operations ([[#tcpsock:connect|connect]], [[#tcpsock:receive|receive]], and iterators returned from [[#tcpsock:receiveuntil|receiveuntil]]).
 
3235
 
 
3236
Settings done by this method takes priority over those config directives, i.e., [[#lua_socket_connect_timeout|lua_socket_connect_timeout]], [[#lua_socket_send_timeout|lua_socket_send_timeout]], and [[#lua_socket_read_timeout|lua_socket_read_timeout]].
 
3237
 
 
3238
Note that this method does ''not'' affect the [[#lua_socket_keepalive_timeout|lua_socket_keepalive_timeout]] setting; the <code>timeout</code> argument to the [[#tcpsock:setkeepalive|setkeepalive]] method should be used for this purpose instead.
 
3239
 
 
3240
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3241
 
 
3242
== tcpsock:setoption ==
 
3243
'''syntax:''' ''tcpsock:setoption(option, value?)''
 
3244
 
 
3245
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3246
 
 
3247
This function is added for [http://w3.impa.br/~diego/software/luasocket/tcp.html LuaSocket] API compatibility and does nothing for now. Its functionality will be implemented in future.
 
3248
 
 
3249
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3250
 
 
3251
== tcpsock:setkeepalive ==
 
3252
'''syntax:''' ''ok, err = tcpsock:setkeepalive(timeout?, size?)''
 
3253
 
 
3254
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3255
 
 
3256
Puts the current socket's connection into the cosocket built-in connection pool and keep it alive until other [[#tcpsock:connect|connect]] method calls request it or the associated maximal idle timeout is expired.
 
3257
 
 
3258
The first optional argument, <code>timeout</code>, can be used to specify the maximal idle timeout (in milliseconds) for the current connection. If omitted, the default setting in the [[#lua_socket_keepalive_timeout|lua_socket_keepalive_timeout]] config directive will be used. If the <code>0</code> value is given, then the timeout interval is unlimited.
 
3259
 
 
3260
The second optional argument, <code>size</code>, can be used to specify the maximal number of connections allowed in the connection pool for the current server (i.e., the current host-port pair or the unix domain socket file path). Note that the size of the connection pool cannot be changed once the pool is created. When this argument is omitted, the default setting in the [[#lua_socket_pool_size|lua_socket_pool_size]] config directive will be used.
 
3261
 
 
3262
When the connection pool is exceeding the size limit, the least recently used (idle) connection already in the pool will be closed automatically to make room for the current connection.
 
3263
 
 
3264
Note that the cosocket connection pool is per Nginx worker process rather than per Nginx server instance, so the size limit specified here also applies to every single Nginx worker process.
 
3265
 
 
3266
Idle connections in the pool will be monitored for any exceptional events like connection abortion or unexpected incoming data on the line, in which cases the connection in question will be closed and removed from the pool.
 
3267
 
 
3268
In case of success, this method returns <code>1</code>; otherwise, it returns <code>nil</code> and a string describing the error.
 
3269
 
 
3270
This method also makes the current cosocket object enter the "closed" state, so you do not need to manually call the [[#tcpsock:close|close]] method on it afterwards.
 
3271
 
 
3272
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3273
 
 
3274
== tcpsock:getreusedtimes ==
 
3275
'''syntax:''' ''count, err = tcpsock:getreusedtimes()''
 
3276
 
 
3277
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3278
 
 
3279
This method returns the (successfully) reused times for the current connection. In case of error, it returns <code>nil</code> and a string describing the error.
 
3280
 
 
3281
If the current connection does not come from the built-in connection pool, then this method always returns <code>0</code>, that is, the connection has never been reused (yet). If the connection comes from the connection pool, then the return value is always non-zero. So this method can also be used to determine if the current connection comes from the pool.
 
3282
 
 
3283
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3284
 
 
3285
== ngx.socket.connect ==
 
3286
'''syntax:''' ''tcpsock, err = ngx.socket.connect(host, port)''
 
3287
 
 
3288
'''syntax:''' ''tcpsock, err = ngx.socket.connect("unix:/path/to/unix-domain.socket")''
 
3289
 
 
3290
'''context:''' ''rewrite_by_lua*, access_by_lua*, content_by_lua*''
 
3291
 
 
3292
This function is a shortcut for combining [[#ngx.socket.tcp|ngx.socket.tcp()]] and the [[#tcpsock:connect|connect()]] method call in a single operation. It is actually implemented like this:
 
3293
 
 
3294
<geshi lang="lua">
 
3295
    local sock = ngx.socket.tcp()
 
3296
    local ok, err = sock:connect(...)
 
3297
    if not ok then
 
3298
        return nil, err
 
3299
    end
 
3300
    return sock
 
3301
</geshi>
 
3302
 
 
3303
There is no way to use the [[#tcpsock:settimeout|settimeout]] method to specify connecting timeout for this method. You have to use the [[#lua_socket_connect_timeout|lua_socket_connect_timeout]] directive at configure time instead.
 
3304
 
 
3305
This feature was first introduced in the <code>v0.5.0rc1</code> release.
 
3306
 
2800
3307
== ndk.set_var.DIRECTIVE ==
2801
3308
'''syntax:''' ''res = ndk.set_var.DIRECTIVE_NAME''
2802
3309
 
2837
3344
= HTTP 1.0 support =
2838
3345
 
2839
3346
The HTTP 1.0 protocol does not support chunked outputs and always requires an
2840
 
explicit <code>Content-Length</code> header when the response body is non-empty. So when
2841
 
an HTTP 1.0 request is present, This module will automatically buffer all the
 
3347
explicit <code>Content-Length</code> header when the response body is non-empty in order to support the HTTP 1.0 keep-alive (as required by the ApacheBench (ab) tool). So when
 
3348
an HTTP 1.0 request is present and the [[#lua_http10_buffering|lua_http10_buffering]] directive is turned <code>on</code>, this module will automatically buffer all the
2842
3349
outputs of user calls of [[#ngx.say|ngx.say]] and [[#ngx.print|ngx.print]] and
2843
3350
postpone sending response headers until it sees all the outputs in the response
2844
3351
body, and at that time ngx_lua can calculate the total length of the body and
2845
3352
construct a proper <code>Content-Length</code> header for the HTTP 1.0 client.
2846
3353
 
 
3354
If the user Lua code sets the <code>Content-Length</code> response header itself, then the automatic buffering will be disabled even if the [[#lua_http10_buffering|lua_http10_buffering]] directive is turned <code>on</code>.
 
3355
 
 
3356
For big responses' streaming outputs, it's important to disable the [[#lua_http10_buffering|lua_http10_buffering]] directive, otherwise the memory usage will grow very quickly.
 
3357
 
2847
3358
Note that, common HTTP benchmark tools like <code>ab</code> and <code>http_load</code> always issue
2848
3359
HTTP 1.0 requests by default. To force <code>curl</code> to send HTTP 1.0 requests, use
2849
3360
the <code>-0</code> option.
2932
3443
 
2933
3444
Assuming the current Lua module is named <code>foo.bar</code>, this will guarantee that local variables in module <code>foo.bar</code> functions have been declared as "local". It prevents undesirable race conditions while accessing such variables. See [[#Data_Sharing_within_an_Nginx_Worker|Data Sharing within an Nginx Worker]] for the reasons behind this.
2934
3445
 
2935
 
== Locations With [[HttpEchoModule]] Directives ==
 
3446
== Locations Configured by Subrequest Directives of Other Modules ==
2936
3447
The [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] directives cannot capture locations that include the [[HttpEchoModule#echo_location|echo_location]], [[HttpEchoModule#echo_location_async|echo_location_async]], [[HttpEchoModule#echo_subrequest|echo_subrequest]], or [[HttpEchoModule#echo_subrequest_async|echo_subrequest_async]] directives.
2937
3448
 
2938
3449
<geshi lang="nginx">
3064
3575
The module is compatible with the following versions of Nginx:
3065
3576
 
3066
3577
*   1.1.x (last tested: 1.1.5)
3067
 
*   1.0.x (last tested: 1.0.11)
 
3578
*   1.0.x (last tested: 1.0.15)
3068
3579
*   0.9.x (last tested: 0.9.4)
3069
3580
*   0.8.x >= 0.8.54 (last tested: 0.8.54)
3070
3581
 
3078
3589
 
3079
3590
Alternatively, <code>ngx_lua</code> can be manually compiled into Nginx:
3080
3591
 
3081
 
# Install LuaJIT 2.0 (Recommended) or Lua 5.1. Lua can be obtained free from the [http://luajit.org/download.html the LuaJIT download page] or [http://www.lua.org/ the standard Lua homepage].  Some distribution package managers also distribute Lua and LuaJIT.
 
3592
# Install LuaJIT 2.0 (Recommended) or Lua 5.1 (Lua 5.2 is ''not'' supported yet). Lua can be obtained free from the [http://luajit.org/download.html the LuaJIT download page] or [http://www.lua.org/ the standard Lua homepage].  Some distribution package managers also distribute Lua and LuaJIT.
3082
3593
# Download the latest version of the ngx_devel_kit (NDK) module [http://github.com/simpl/ngx_devel_kit/tags HERE].
3083
3594
# Download the latest version of this module [http://github.com/chaoslawful/lua-nginx-module/tags HERE].
3084
3595
# Download the latest version of Nginx [http://nginx.org/ HERE] (See [[#Nginx Compatibility|Nginx Compatibility]])
3086
3597
Build the source with this module:
3087
3598
 
3088
3599
<geshi lang="bash">
3089
 
    wget 'http://nginx.org/download/nginx-1.0.11.tar.gz'
3090
 
    tar -xzvf nginx-1.0.11.tar.gz
3091
 
    cd nginx-1.0.11/
 
3600
    wget 'http://nginx.org/download/nginx-1.0.15.tar.gz'
 
3601
    tar -xzvf nginx-1.0.15.tar.gz
 
3602
    cd nginx-1.0.15/
3092
3603
 
3093
3604
    # tell nginx's build system where to find lua:
3094
3605
    export LUA_LIB=/path/to/lua/lib
3117
3628
= TODO =
3118
3629
 
3119
3630
== Short Term ==
3120
 
* add <code>ignore_resp_headers</code>, <code>ignore_resp_body</code>, and <code>ignore_resp</code> options to [[#ngx.location.capture|ngx.location.capture]] and ngx.location.capture_multi` methods, to allow micro performance tuning on the user side.
3121
 
* add directives to run lua codes when nginx stops/reloads.
 
3631
* implement the <code>ngx.sleep(time)</code> Lua API. (For now, use [[#ngx.location.capture|ngx.location.capture]] with [[HttpEchoModule]]'s [[HttpEchoModule#echo_sleep|echo_sleep]] config directive instead.)
 
3632
* implement the <code>ngx.worker.get_pid()</code> Lua API. (For now, use <code>ngx.var.pid</code> directly.)
 
3633
* implement [http://w3.impa.br/~diego/software/luasocket/udp.html LuaSocket UDP API] in our cosocket API.
 
3634
* implement the SSL cosocket API.
 
3635
* implement the <code>ngx.re.split</code> method.
 
3636
* use <code>ngx_hash_t</code> to optimize the built-in header look-up process for [[#ngx.req.set_header|ngx.req.set_header]], [[#ngx.header.HEADER|ngx.header.HEADER]], and etc.
 
3637
* fix HTTP 1.0 support: we should by default close the current HTTP 1.0 connection right away if no <code>Content-Length</code> response header is set. the current automatic full buffering bahvior is way too expensive.
 
3638
* add configure options for different strategies of handling the cosocket connection exceeding in the pools.
 
3639
* add directives to run Lua codes when nginx stops/reloads.
3122
3640
* deal with TCP 3-second delay problem under great connection harness.
3123
 
* add options to [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] in order to share and copy a particular set of nginx variables with subrequests, specified by the user.
3124
 
* add an option to [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] so as to specify the [[#ngx.ctx|ngx.ctx]] table for subrequests.
3125
 
* add support for multi-value arguments to [[#ngx.req.set_uri_args]] if its <code>args</code> argument is a Lua table.
3126
3641
* add APIs to access cookies as key/value pairs.
 
3642
* add <code>ignore_resp_headers</code>, <code>ignore_resp_body</code>, and <code>ignore_resp</code> options to [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] methods, to allow micro performance tuning on the user side.
3127
3643
 
3128
3644
== Longer Term ==
3129
3645
* add the <code>lua_require</code> directive to load module into main thread's globals.
3130
 
* add the "cosocket" mechamism to emulate a common Lua socket API set that will provide transparent nonblocking capability out of the box and that avoids Nginx subrequest overheads.
3131
3646
* add Lua code automatic time slicing support by yielding and resuming the Lua VM actively via Lua's debug hooks.
3132
 
* make set_by_lua use the same mechanism as content_by_lua.
3133
 
* add coroutine API back to Lua.
3134
 
* add <code>stat</code> mode similar to <code>mod_lua</code>.
 
3647
* make [[#set_by_lua|set_by_lua]], [[#header_filter_by_lua|header_filter_by_lua]], and their variants use the same mechanism as [[#content_by_lua|content_by_lua]], [[#rewrite_by_lua|rewrite_by_lua]], [[#access_by_lua|access_by_lua]], and their variants.
 
3648
* add coroutine API back to the Lua user land.
 
3649
* add <code>stat</code> mode similar to [http://httpd.apache.org/docs/2.3/mod/mod_lua.html mod_lua].
3135
3650
 
3136
3651
= Changes =
3137
3652
 
3269
3784
 
3270
3785
= Copyright and License =
3271
3786
 
3272
 
This module is licenced under the BSD license.
3273
 
 
3274
 
Copyright (C) 2009-2011, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
3275
 
 
3276
 
Copyright (C) 2009-2011, by Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>.
 
3787
This module is licensed under the BSD license.
 
3788
 
 
3789
Copyright (C) 2009-2012, by Xiaozhe Wang (chaoslawful) <chaoslawful@gmail.com>.
 
3790
 
 
3791
Copyright (C) 2009-2012, by Zhang "agentzh" Yichun (章亦春) <agentzh@gmail.com>.
3277
3792
 
3278
3793
All rights reserved.
3279
3794
 
3287
3802
 
3288
3803
= See Also =
3289
3804
 
 
3805
* [http://github.com/agentzh/lua-resty-memcached lua-resty-memcached] library based on ngx_lua cosocket.
 
3806
* [http://github.com/agentzh/lua-resty-redis lua-resty-redis] library based on ngx_lua cosocket.
 
3807
* [http://github.com/agentzh/lua-resty-mysql lua-resty-mysql] library based on ngx_lua cosocket.
 
3808
* [http://github.com/agentzh/lua-resty-upload lua-resty-upload] library based on ngx_lua cosocket.
 
3809
* [http://github.com/agentzh/lua-resty-string lua-resty-string] library based on [http://luajit.org/ext_ffi.html LuaJIT FFI].
3290
3810
* [http://openresty.org/#RoutingMySQLQueriesBasedOnURIArgs Routing requests to different MySQL queries based on URI arguments]
3291
3811
* [http://openresty.org/#DynamicRoutingBasedOnRedis Dynamic Routing Based on Redis and Lua]
3292
3812
* [http://openresty.org/#UsingLuaRocks Using LuaRocks with ngx_lua]
3298
3818
* [[HttpMemcModule]]
3299
3819
* [http://openresty.org The ngx_openresty bundle]
3300
3820
 
 
3821
= Translations =
 
3822
* [[HttpLuaModuleZh|Chinese]]
 
3823