comparison xml/en/docs/dev/development_guide.xml @ 2007:c559be34257b

DevGuide: language and style fixes, second part.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 05 Jul 2017 14:58:38 +0300
parents 1b086de6a05b
children 1f2f9fa97065
comparison
equal deleted inserted replaced
2006:b13d6412b335 2007:c559be34257b
2734 2734
2735 <section name="Processes" id="processes"> 2735 <section name="Processes" id="processes">
2736 2736
2737 <para> 2737 <para>
2738 There are several types of processes in nginx. 2738 There are several types of processes in nginx.
2739 The type of current process is kept in the <literal>ngx_process</literal> 2739 The type of a process is kept in the <literal>ngx_process</literal>
2740 global variable: 2740 global variable, and is one of the following:
2741 </para> 2741 </para>
2742 2742
2743 <list type="bullet"> 2743 <list type="bullet">
2744 2744
2745 <listitem> 2745 <listitem>
2746 2746
2747 <para> 2747 <para>
2748 <literal>NGX_PROCESS_MASTER</literal> — the master process runs the 2748 <literal>NGX_PROCESS_MASTER</literal> — The master process, which reads the
2749 <literal>ngx_master_process_cycle()</literal> function. 2749 NGINX configuration, creates cycles, and starts and controls child processes.
2750 Master process does not have any I/O and responds only to signals. 2750 It does not perform any I/O and responds only to signals.
2751 It reads configuration, creates cycles, starts and controls child processes 2751 Its cycle function is <literal>ngx_master_process_cycle()</literal>.
2752 </para> 2752 </para>
2753 2753
2754 2754
2755 </listitem> 2755 </listitem>
2756 2756
2757 <listitem> 2757 <listitem>
2758 2758
2759 <para> 2759 <para>
2760 <literal>NGX_PROCESS_WORKER</literal> — the worker process runs the 2760 <literal>NGX_PROCESS_WORKER</literal> — The worker process, which handles client
2761 <literal>ngx_worker_process_cycle()</literal> function. 2761 connections.
2762 Worker processes are started by master and handle client connections. 2762 It is started by the master process and responds to its signals and channel
2763 They also respond to signals and channel commands, sent from master 2763 commands as well.
2764 </para> 2764 Its cycle function is <literal>ngx_worker_process_cycle()</literal>.
2765 2765 There can be multiple worker processes, as configured by the
2766 2766 <literal>worker_processes</literal> directive.
2767 </listitem> 2767 </para>
2768 2768
2769 <listitem> 2769 </listitem>
2770 2770
2771 <para> 2771 <listitem>
2772 <literal>NGX_PROCESS_SINGLE</literal> — single process is the only type 2772
2773 of processes which exist in the <literal>master_process off</literal> mode. 2773 <para>
2774 The cycle function for this process is 2774 <literal>NGX_PROCESS_SINGLE</literal> — The single process, which exists only in
2775 <literal>ngx_single_process_cycle()</literal>. 2775 <literal>master_process off</literal> mode, and is the only process running in
2776 This process creates cycles and handles client connections 2776 that mode.
2777 </para> 2777 It creates cycles (like the master process does) and handles client connections
2778 2778 (like the worker process does).
2779 2779 Its cycle function is <literal>ngx_single_process_cycle()</literal>.
2780 </listitem> 2780 </para>
2781 2781
2782 <listitem> 2782 </listitem>
2783 2783
2784 <para> 2784 <listitem>
2785 <literal>NGX_PROCESS_HELPER</literal> — currently, there are two types of 2785
2786 helper processes: cache manager and cache loader. 2786 <para>
2787 Both of them share the same cycle function 2787 <literal>NGX_PROCESS_HELPER</literal> — The helper process, of which currently
2788 there are two types: cache manager and cache loader.
2789 The cycle function for both is
2788 <literal>ngx_cache_manager_process_cycle()</literal>. 2790 <literal>ngx_cache_manager_process_cycle()</literal>.
2789 </para> 2791 </para>
2790 2792
2791 2793 </listitem>
2792 </listitem> 2794
2793 2795 </list>
2794 </list> 2796
2795 2797 <para>
2796 <para> 2798 The nginx processes handle the following signals:
2797 All nginx processes handle the following signals: 2799 </para>
2798 </para> 2800
2799 2801 <list type="bullet">
2800 <list type="bullet"> 2802
2801 2803 <listitem>
2802 <listitem> 2804
2803 2805 <para>
2804 <para> 2806 <literal>NGX_SHUTDOWN_SIGNAL</literal> (<literal>SIGQUIT</literal> on most
2805 <literal>NGX_SHUTDOWN_SIGNAL</literal> (<literal>SIGQUIT</literal>) — graceful 2807 systems) — Gracefully shutdown.
2806 shutdown. 2808 Upon receiving this signal, the master process sends a shutdown signal to all
2807 Upon receiving this signal master process sends shutdown signal to all child 2809 child processes.
2808 processes. 2810 When no child processes are left, the master destroys the cycle pool and exits.
2809 When no child processes are left, master destroys cycle pool and exits. 2811 When a worker process receives this signal, it closes all listening sockets and
2810 A worker process which received this signal, closes all listening sockets and 2812 waits until there are no non-cancelable events scheduled, then destroys the
2811 waits until timeout tree becomes empty, then destroys cycle pool and exits. 2813 cycle pool and exits.
2812 A cache manager process exits right after receiving this signal. 2814 When the cache manager or the cache loader process receives this signal, it
2813 The variable <literal>ngx_quit</literal> is set to one after receiving this 2815 exits immediately.
2814 signal and immediately reset after being processed. 2816 The <literal>ngx_quit</literal> variable is set to <literal>1</literal> when a
2815 The variable <literal>ngx_exiting</literal> is set to one when worker process 2817 process receives this signal, and is immediately reset after being processed.
2816 is in shutdown state 2818 The <literal>ngx_exiting</literal> variable is set to <literal>1</literal> while
2817 </para> 2819 a worker process is in the shutdown state.
2818 2820 </para>
2819 2821
2820 </listitem> 2822 </listitem>
2821 2823
2822 <listitem> 2824 <listitem>
2823 2825
2824 <para> 2826 <para>
2825 <literal>NGX_TERMINATE_SIGNAL</literal> (<literal>SIGTERM</literal>) - 2827 <literal>NGX_TERMINATE_SIGNAL</literal> (<literal>SIGTERM</literal> on most
2826 terminate. 2828 systems) — Terminate.
2827 Upon receiving this signal master process sends terminate signal to all child 2829 Upon receiving this signal, the master process sends a terminate signal to all
2828 processes. 2830 child processes.
2829 If child processes do not exit in 1 second, they are killed with the 2831 If a child process does not exit within 1 second, the master process sends the
2830 <literal>SIGKILL</literal> signal. 2832 <literal>SIGKILL</literal> signal to kill it.
2831 When no child processes are left, master process destroys cycle pool and exits. 2833 When no child processes are left, the master process destroys the cycle pool and
2832 A worker or cache manager process which received this signal destroys cycle 2834 exits.
2833 pool and exits. 2835 When a worker process, the cache manager process or the cache loader process
2834 The variable <literal>ngx_terminate</literal> is set to one after receiving 2836 receives this signal, it destroys the cycle pool and exits.
2835 this signal 2837 The variable <literal>ngx_terminate</literal> is set to <literal>1</literal>
2836 </para> 2838 when this signal is received.
2837 2839 </para>
2838 2840
2839 </listitem> 2841 </listitem>
2840 2842
2841 <listitem> 2843 <listitem>
2842 2844
2843 <para> 2845 <para>
2844 <literal>NGX_NOACCEPT_SIGNAL</literal> (<literal>SIGWINCH</literal>) 2846 <literal>NGX_NOACCEPT_SIGNAL</literal> (<literal>SIGWINCH</literal> on most
2845 - gracefully shut down worker processes 2847 systems) - Shut down all worker and helper processes.
2846 </para> 2848 Upon receiving this signal, the master process shuts down its child processes.
2847 2849 If a previously started new nginx binary exits, the child processes of the old
2848 2850 master are started again.
2849 </listitem> 2851 When a worker process receives this signal, it shuts down in debug mode
2850 2852 set by the <literal>debug_points</literal> directive.
2851 <listitem> 2853 </para>
2852 2854
2853 <para> 2855
2854 <literal>NGX_RECONFIGURE_SIGNAL</literal> (<literal>SIGHUP</literal>) - 2856 </listitem>
2855 reconfigure. 2857
2856 Upon receiving this signal master process creates a new cycle from 2858 <listitem>
2857 configuration file. 2859
2858 If the new cycle was created successfully, the old cycle is deleted and new 2860 <para>
2861 <literal>NGX_RECONFIGURE_SIGNAL</literal> (<literal>SIGHUP</literal> on most
2862 systems) - Reconfigure.
2863 Upon receiving this signal, the master process re-reads the configuration and
2864 creates a new cycle based on it.
2865 If the new cycle is created successfully, the old cycle is deleted and new
2859 child processes are started. 2866 child processes are started.
2860 Meanwhile, the old processes receive the shutdown signal. 2867 Meanwhile, the old child processes receive the
2861 In single-process mode, nginx creates a new cycle as well, but keeps the old 2868 <literal>NGX_SHUTDOWN_SIGNAL</literal> signal.
2862 one until all clients, tied to the old cycle, are gone. 2869 In single-process mode, nginx creates a new cycle, but keeps the old one until
2863 Worker and helper processes ignore this signal 2870 there are no longer clients with active connections tied to it.
2864 </para> 2871 The worker and helper processes ignore this signal.
2865 2872 </para>
2866 2873
2867 </listitem> 2874 </listitem>
2868 2875
2869 <listitem> 2876 <listitem>
2870 2877
2871 <para> 2878 <para>
2872 <literal>NGX_REOPEN_SIGNAL</literal> (<literal>SIGUSR1</literal>) — reopen 2879 <literal>NGX_REOPEN_SIGNAL</literal> (<literal>SIGUSR1</literal> on most
2873 files. 2880 systems) — Reopen files.
2874 Master process passes this signal to workers. 2881 The master process sends this signal to workers, which reopen all
2875 Worker processes reopen all <literal>open_files</literal> from the cycle 2882 <literal>open_files</literal> related to the cycle.
2876 </para> 2883 </para>
2877 2884
2878 2885 </listitem>
2879 </listitem> 2886
2880 2887 <listitem>
2881 <listitem> 2888
2882 2889 <para>
2883 <para> 2890 <literal>NGX_CHANGEBIN_SIGNAL</literal> (<literal>SIGUSR2</literal> on most
2884 <literal>NGX_CHANGEBIN_SIGNAL</literal> (<literal>SIGUSR2</literal>) — change 2891 systems) — Change the nginx binary.
2885 nginx binary. 2892 The master process starts a new nginx binary and passes in a list of all listen
2886 Master process starts a new nginx binary and passes there a list of all listen
2887 sockets. 2893 sockets.
2888 The list is passed in the environment variable <literal>“NGINX”</literal> in 2894 The text-format list, passed in the <literal>“NGINX”</literal> environment
2889 text format, where descriptor numbers separated with semicolons. 2895 variable, consists of descriptor numbers separated with semicolons.
2890 A new nginx instance reads that variable and adds the sockets to its init 2896 The new nginx binary reads the <literal>“NGINX”</literal> variable and adds the
2891 cycle. 2897 sockets to its init cycle.
2892 Other processes ignore this signal 2898 Other processes ignore this signal.
2893 </para> 2899 </para>
2894
2895 2900
2896 </listitem> 2901 </listitem>
2897 2902
2898 </list> 2903 </list>
2899 2904
2900 <para> 2905 <para>
2901 While all nginx worker processes are able to receive and properly handle POSIX 2906 While all nginx worker processes are able to receive and properly handle POSIX
2902 signals, master process normally does not pass any signals to workers and 2907 signals, the master process does not use the standard <literal>kill()</literal>
2903 helpers with the standard <literal>kill()</literal> syscall. 2908 syscall to pass signals to workers and helpers.
2904 Instead, nginx uses inter-process channels which allow sending messages between 2909 Instead, nginx uses inter-process socket pairs which allow sending messages
2905 all nginx processes. 2910 between all nginx processes.
2906 Currently, however, messages are only sent from master to its children. 2911 Currently, however, messages are only sent from the master to its children.
2907 Those messages carry the same signals. 2912 The messages carry the standard signals.
2908 The channels are socketpairs with their ends in different processes.
2909 </para>
2910
2911 <para>
2912 When running nginx binary, several values can be specified next to
2913 <literal>-s</literal> parameter.
2914 Those values are <literal>stop</literal>, <literal>quit</literal>,
2915 <literal>reopen</literal>, <literal>reload</literal>.
2916 They are converted to signals <literal>NGX_TERMINATE_SIGNAL</literal>,
2917 <literal>NGX_SHUTDOWN_SIGNAL</literal>, <literal>NGX_REOPEN_SIGNAL</literal>
2918 and <literal>NGX_RECONFIGURE_SIGNAL</literal> and sent to the nginx master
2919 process, whose pid is read from nginx pid file.
2920 </para> 2913 </para>
2921 2914
2922 </section> 2915 </section>
2923 2916
2924 <section name="Threads" id="threads"> 2917 <section name="Threads" id="threads">
2925 2918
2926 <para> 2919 <para>
2927 It is possible to offload tasks that would otherwise block nginx worker process 2920 It is possible to offload into a separate thread tasks that would otherwise
2928 into a separate thread. 2921 block the nginx worker process.
2929 For example, nginx may be configured to use threads to perform 2922 For example, nginx can be configured to use threads to perform
2930 <link doc="../http/ngx_http_core_module.xml" id="aio">file I/O</link>. 2923 <link doc="../http/ngx_http_core_module.xml" id="aio">file I/O</link>.
2931 Another example is using a library that doesn't have asynchronous interface 2924 Another use case is a library that doesn't have asynchronous interface
2932 and thus cannot be normally used with nginx. 2925 and thus cannot be normally used with nginx.
2933 Keep in mind that threads interface is a helper for existing asynchronous 2926 Keep in mind that the threads interface is a helper for the existing
2934 approach in processing client connections, and by no means a replacement. 2927 asynchronous approach to processing client connections, and by no means
2935 </para> 2928 intended as a replacement.
2936 2929 </para>
2937 <para> 2930
2938 To deal with synchronization the following wrappers over pthreads primitives 2931 <para>
2939 are available: 2932 To deal with synchronization, the following wrappers over
2940 <programlisting> 2933 <literal>pthreads</literal> primitives are available:
2941 typedef pthread_mutex_t ngx_thread_mutex_t; 2934
2942 2935 <list type="bullet">
2943 ngx_int_t ngx_thread_mutex_create(ngx_thread_mutex_t *mtx, ngx_log_t *log); 2936
2944 ngx_int_t ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx, ngx_log_t *log); 2937 <listitem>
2945 ngx_int_t ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx, ngx_log_t *log); 2938 <literal>typedef pthread_mutex_t ngx_thread_mutex_t;</literal>
2946 ngx_int_t ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx, ngx_log_t *log); 2939
2947 2940 <list type="bullet">
2948 typedef pthread_cond_t ngx_thread_cond_t; 2941
2949 2942 <listitem>
2950 ngx_int_t ngx_thread_cond_create(ngx_thread_cond_t *cond, ngx_log_t *log); 2943 <literal>ngx_int_t
2951 ngx_int_t ngx_thread_cond_destroy(ngx_thread_cond_t *cond, ngx_log_t *log); 2944 ngx_thread_mutex_create(ngx_thread_mutex_t *mtx, ngx_log_t *log);</literal>
2952 ngx_int_t ngx_thread_cond_signal(ngx_thread_cond_t *cond, ngx_log_t *log); 2945 </listitem>
2953 ngx_int_t ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx, 2946
2954 ngx_log_t *log); 2947 <listitem>
2955 </programlisting> 2948 <literal>ngx_int_t
2949 ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx, ngx_log_t *log);</literal>
2950 </listitem>
2951
2952 <listitem>
2953 <literal>ngx_int_t
2954 ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx, ngx_log_t *log);</literal>
2955 </listitem>
2956
2957 <listitem>
2958 <literal>ngx_int_t
2959 ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx, ngx_log_t *log);</literal>
2960 </listitem>
2961
2962 </list>
2963
2964 </listitem>
2965
2966 <listitem>
2967 <literal>typedef pthread_cond_t ngx_thread_cond_t;</literal>
2968
2969 <list type="bullet">
2970
2971 <listitem>
2972 <literal>ngx_int_t
2973 ngx_thread_cond_create(ngx_thread_cond_t *cond, ngx_log_t *log);</literal>
2974 </listitem>
2975
2976 <listitem>
2977 <literal>ngx_int_t
2978 ngx_thread_cond_destroy(ngx_thread_cond_t *cond, ngx_log_t *log);</literal>
2979 </listitem>
2980
2981 <listitem>
2982 <literal>ngx_int_t
2983 ngx_thread_cond_signal(ngx_thread_cond_t *cond, ngx_log_t *log);</literal>
2984 </listitem>
2985
2986 <listitem>
2987 <literal>ngx_int_t
2988 ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx,
2989 ngx_log_t *log);</literal>
2990 </listitem>
2991
2992 </list>
2993
2994 </listitem>
2995
2996 </list>
2997
2956 </para> 2998 </para>
2957 2999
2958 <para> 3000 <para>
2959 Instead of creating a new thread for each task, nginx implements 3001 Instead of creating a new thread for each task, nginx implements
2960 a <link doc="../ngx_core_module.xml" id="thread_pool"/> strategy. 3002 a <link doc="../ngx_core_module.xml" id="thread_pool"/> strategy.
2961 Multiple thread pools may be configured intended for different purposes 3003 Multiple thread pools may be configured for different purposes
2962 (for example, performing I/O on different sets of disks). 3004 (for example, performing I/O on different sets of disks).
2963 Each thread pool is created on start and contains a limited number of threads 3005 Each thread pool is created at startup and contains a limited number of threads
2964 that process a queue of tasks. 3006 that process a queue of tasks.
2965 When a task is completed, a predefined completion handler is called. 3007 When a task is completed, a predefined completion handler is called.
2966 </para> 3008 </para>
2967 3009
2968 <para> 3010 <para>
2969 The <literal>src/core/ngx_thread_pool.h</literal> header file contains 3011 The <literal>src/core/ngx_thread_pool.h</literal> header file contains
2970 corresponding definitions: 3012 relevant definitions:
2971 <programlisting> 3013 <programlisting>
2972 struct ngx_thread_task_s { 3014 struct ngx_thread_task_s {
2973 ngx_thread_task_t *next; 3015 ngx_thread_task_t *next;
2974 ngx_uint_t id; 3016 ngx_uint_t id;
2975 void *ctx; 3017 void *ctx;
2985 ngx_thread_task_t *ngx_thread_task_alloc(ngx_pool_t *pool, size_t size); 3027 ngx_thread_task_t *ngx_thread_task_alloc(ngx_pool_t *pool, size_t size);
2986 ngx_int_t ngx_thread_task_post(ngx_thread_pool_t *tp, ngx_thread_task_t *task); 3028 ngx_int_t ngx_thread_task_post(ngx_thread_pool_t *tp, ngx_thread_task_t *task);
2987 3029
2988 </programlisting> 3030 </programlisting>
2989 At configuration time, a module willing to use threads has to obtain a 3031 At configuration time, a module willing to use threads has to obtain a
2990 reference to thread pool by calling 3032 reference to a thread pool by calling
2991 <literal>ngx_thread_pool_add(cf, name)</literal> which will either create a 3033 <literal>ngx_thread_pool_add(cf, name)</literal>, which either creates a
2992 new thread pool with given <literal>name</literal> or return a reference 3034 new thread pool with the given <literal>name</literal> or returns a reference
2993 to an existing one if a pool with such name already exists. 3035 to the pool with that name if it already exists.
2994 </para> 3036 </para>
2995 3037
2996 <para> 3038 <para>
2997 At runtime, the <literal>ngx_thread_task_post(tp, task)</literal> function 3039 To add a <literal>task</literal> into a queue of a specified thread pool
2998 is used to add a <literal>task</literal> into a queue of a thread pool 3040 <literal>tp</literal> at runtime, use the
2999 <literal>tp</literal>. 3041 <literal>ngx_thread_task_post(tp, task)</literal> function.
3000 3042
3001 The <literal>ngx_thread_task_t</literal> structure contains all necessary 3043 To execute a function in a thread, pass parameters and setup a completion
3002 to execute user function in thread, pass parameters and setup completion 3044 handler using the <literal>ngx_thread_task_t</literal> structure:
3003 handler:
3004 <programlisting> 3045 <programlisting>
3005 typedef struct { 3046 typedef struct {
3006 int foo; 3047 int foo;
3007 } my_thread_ctx_t; 3048 } my_thread_ctx_t;
3008 3049
3059 3100
3060 <section name="Modules" id="Modules"> 3101 <section name="Modules" id="Modules">
3061 3102
3062 <section name="Adding new modules" id="adding_new_modules"> 3103 <section name="Adding new modules" id="adding_new_modules">
3063 <para> 3104 <para>
3064 The standalone nginx module resides in a separate directory that contains 3105 Each standalone nginx module resides in a separate directory that contains
3065 at least two files: 3106 at least two files:
3066 <literal>config</literal> and a file with the module source. 3107 <literal>config</literal> and a file with the module source code.
3067 The first file contains all information needed for nginx to integrate 3108 The <literal>config</literal> file contains all information needed for nginx to
3068 the module, for example: 3109 integrate the module, for example:
3069 <programlisting> 3110 <programlisting>
3070 ngx_module_type=CORE 3111 ngx_module_type=CORE
3071 ngx_module_name=ngx_foo_module 3112 ngx_module_name=ngx_foo_module
3072 ngx_module_srcs="$ngx_addon_dir/ngx_foo_module.c" 3113 ngx_module_srcs="$ngx_addon_dir/ngx_foo_module.c"
3073 3114
3074 . auto/module 3115 . auto/module
3075 3116
3076 ngx_addon_name=$ngx_module_name 3117 ngx_addon_name=$ngx_module_name
3077 </programlisting> 3118 </programlisting>
3078 The file is a POSIX shell script and it can set (or access) the 3119 The <literal>config</literal> file is a POSIX shell script that can set
3079 following variables: 3120 and access the following variables:
3080 <list type="bullet"> 3121 <list type="bullet">
3081 3122
3082 <listitem> 3123 <listitem>
3083 <literal>ngx_module_type</literal> — the type of module to build. 3124 <literal>ngx_module_type</literal> — Type of module to build.
3084 Possible options are <literal>CORE</literal>, <literal>HTTP</literal>, 3125 Possible values are <literal>CORE</literal>, <literal>HTTP</literal>,
3085 <literal>HTTP_FILTER</literal>, <literal>HTTP_INIT_FILTER</literal>, 3126 <literal>HTTP_FILTER</literal>, <literal>HTTP_INIT_FILTER</literal>,
3086 <literal>HTTP_AUX_FILTER</literal>, <literal>MAIL</literal>, 3127 <literal>HTTP_AUX_FILTER</literal>, <literal>MAIL</literal>,
3087 <literal>STREAM</literal>, or <literal>MISC</literal> 3128 <literal>STREAM</literal>, or <literal>MISC</literal>.
3088 </listitem> 3129 </listitem>
3089 3130
3090 <listitem> 3131 <listitem>
3091 <literal>ngx_module_name</literal> — the name of the module. 3132 <literal>ngx_module_name</literal> — Module names.
3092 A whitespace separated values list is accepted and may be used to build 3133 To build multiple modules from a set of source files, specify a
3093 multiple modules from a single set of source files. 3134 whitespace-separated list of names.
3094 The first name indicates the name of the output binary for a dynamic module. 3135 The first name indicates the name of the output binary for the dynamic module.
3095 The names in this list should match the names used in the module. 3136 The names in the list must match the names used in the source code.
3096 </listitem> 3137 </listitem>
3097 3138
3098 <listitem> 3139 <listitem>
3099 <literal>ngx_addon_name</literal> — supplies the name of the module in the 3140 <literal>ngx_addon_name</literal> — Name of the module as it appears in output
3100 console output text of the configure script. 3141 on the console from the configure script.
3101 </listitem> 3142 </listitem>
3102 3143
3103 <listitem> 3144 <listitem>
3104 <literal>ngx_module_srcs</literal> — a whitespace separated list of source 3145 <literal>ngx_module_srcs</literal> — Whitespace-separated list of source
3105 files used to compile the module. 3146 files used to compile the module.
3106 The $ngx_addon_dir variable can be used as a placeholder for the path of the 3147 The <literal>$ngx_addon_dir</literal> variable can be used to represent the path
3107 module source. 3148 to the module directory.
3108 </listitem> 3149 </listitem>
3109 3150
3110 <listitem> 3151 <listitem>
3111 <literal>ngx_module_incs</literal> — include paths required to build the module 3152 <literal>ngx_module_incs</literal> — Include paths required to build the module
3112 </listitem> 3153 </listitem>
3113 3154
3114 <listitem> 3155 <listitem>
3115 <literal>ngx_module_deps</literal> — a list of module's header files. 3156 <literal>ngx_module_deps</literal> — Whitespace-separated list of the module's
3116 </listitem> 3157 dependencies.
3117 3158 Usually, it is the list of header files.
3118 <listitem> 3159 </listitem>
3119 <literal>ngx_module_libs</literal> — a list of libraries to link with the 3160
3120 module. 3161 <listitem>
3121 For example, libpthread would be linked using 3162 <literal>ngx_module_libs</literal> — Whitespace-separated list of libraries to
3122 <literal>ngx_module_libs=-lpthread</literal>. 3163 link with the module.
3164 For example, use <literal>ngx_module_libs=-lpthread</literal> to link
3165 <literal>libpthread</literal> library.
3123 The following macros can be used to link against the same libraries as 3166 The following macros can be used to link against the same libraries as
3124 nginx: 3167 nginx:
3125 <literal>LIBXSLT</literal>, <literal>LIBGD</literal>, <literal>GEOIP</literal>, 3168 <literal>LIBXSLT</literal>, <literal>LIBGD</literal>, <literal>GEOIP</literal>,
3126 <literal>PCRE</literal>, <literal>OPENSSL</literal>, <literal>MD5</literal>, 3169 <literal>PCRE</literal>, <literal>OPENSSL</literal>, <literal>MD5</literal>,
3127 <literal>SHA1</literal>, <literal>ZLIB</literal>, and <literal>PERL</literal> 3170 <literal>SHA1</literal>, <literal>ZLIB</literal>, and <literal>PERL</literal>.
3128 </listitem> 3171 </listitem>
3129 3172
3130 <listitem> 3173 <listitem>
3131 <literal>ngx_module_link</literal> — set by the build system to 3174 <literal>ngx_module_link</literal> — Variable set by the build system to
3132 <literal>DYNAMIC</literal> for a dynamic module or <literal>ADDON</literal> 3175 <literal>DYNAMIC</literal> for a dynamic module or <literal>ADDON</literal>
3133 for a static module and used to perform different actions depending on 3176 for a static module and used to determine different actions to perform
3134 linking type. 3177 depending on linking type.
3135 </listitem> 3178 </listitem>
3136 3179
3137 <listitem> 3180 <listitem>
3138 <literal>ngx_module_order</literal> — sets the load order for the module which 3181 <literal>ngx_module_order</literal> — Load order for the module;
3139 is useful for <literal>HTTP_FILTER</literal> and 3182 useful for the <literal>HTTP_FILTER</literal> and
3140 <literal>HTTP_AUX_FILTER</literal> module types. 3183 <literal>HTTP_AUX_FILTER</literal> module types.
3141 The order is stored in a reverse list. 3184 The format for this option is a whitespace-separated list of modules.
3142 3185 All modules in the list following the current module's name end up after it in
3143 <para> 3186 the global list of modules, which sets up the order for modules initialization.
3144 The <literal>ngx_http_copy_filter_module</literal> is near the bottom of the 3187 For filter modules later initialization means earlier execution.
3145 list so is one of the first to be executed. 3188
3146 This reads the data for other filters. 3189 <para>
3147 Near the top of the list is <literal>ngx_http_write_filter_module</literal> 3190 The following modules are typically used as references.
3148 which writes the data out and is one of the last to be executed. 3191 The <literal>ngx_http_copy_filter_module</literal> reads the data for other
3149 </para> 3192 filter modules and is placed near the bottom of the list so that it is one of
3150 3193 the first to be executed.
3151 <para> 3194 The <literal>ngx_http_write_filter_module</literal> writes the data to the
3152 The format for this option is typically the current module’s name followed by 3195 client socket and is placed near the top of the list, and is the last to be
3153 a whitespace separated list of modules to insert before, and therefore execute 3196 executed.
3154 after. 3197 </para>
3155 The module will be inserted before the last module in the list that is found 3198
3156 to be currently loaded. 3199 <para>
3157 </para> 3200 By default, filter modules are placed before the
3158 3201 <literal>ngx_http_copy_filter</literal> in the module list so that the filter
3159 <para> 3202 handler is executed after the copy filter handler.
3160 By default for filter modules this is set to 3203 For other module types the default is the empty string.
3161 “<literal>ngx_http_copy_filter</literal>” which will insert the module before 3204 </para>
3162 the copy filter in the list and therefore will execute after the copy filter. 3205
3163 For other module types the default is empty. 3206 </listitem>
3164 </para> 3207
3165 3208 </list>
3166 </listitem> 3209
3167 3210 To compile a module into nginx statically, use the
3168 </list> 3211 <literal>--add-module=/path/to/module</literal> argument to the configure
3169 3212 script.
3170 A module can be added to nginx by means of the configure script using 3213 To compile a module for later dynamic loading into nginx, use the
3171 <literal>--add-module=/path/to/module</literal> for static compilation and 3214 <literal>--add-dynamic-module=/path/to/module</literal> argument.
3172 <literal>--add-dynamic-module=/path/to/module</literal> for dynamic compilation.
3173 </para> 3215 </para>
3174 3216
3175 </section> 3217 </section>
3176 3218
3177 3219
3178 <section name="Core modules" id="core_modules"> 3220 <section name="Core Modules" id="core_modules">
3179 3221
3180 <para> 3222 <para>
3181 Modules are building blocks of nginx, and most of its functionality is 3223 Modules are the building blocks of nginx, and most of its functionality is
3182 implemented as modules. 3224 implemented as modules.
3183 The module source file must contain a global variable of 3225 The module source file must contain a global variable of type
3184 <literal>ngx_module_t</literal> type which is defined as follows: 3226 <literal>ngx_module_t</literal>, which is defined as follows:
3185 <programlisting> 3227 <programlisting>
3186 struct ngx_module_s { 3228 struct ngx_module_s {
3187 3229
3188 /* private part is omitted */ 3230 /* private part is omitted */
3189 3231
3203 void (*exit_master)(ngx_cycle_t *cycle); 3245 void (*exit_master)(ngx_cycle_t *cycle);
3204 3246
3205 /* stubs for future extensions are omitted */ 3247 /* stubs for future extensions are omitted */
3206 }; 3248 };
3207 </programlisting> 3249 </programlisting>
3208 The omitted private part includes module version, signature and is filled 3250 The omitted private part includes the module version and a signature and is
3209 using the predefined macro <literal>NGX_MODULE_V1</literal>. 3251 filled using the predefined macro <literal>NGX_MODULE_V1</literal>.
3210 </para> 3252 </para>
3211 3253
3212 <para> 3254 <para>
3213 Each module keeps its private data in the <literal>ctx</literal> field, 3255 Each module keeps its private data in the <literal>ctx</literal> field,
3214 recognizes specific configuration directives, specified in the 3256 recognizes the configuration directives, specified in the
3215 <literal>commands</literal> array, and may be invoked at certain stages of 3257 <literal>commands</literal> array, and can be invoked at certain stages of
3216 nginx lifecycle. 3258 nginx lifecycle.
3217 The module lifecycle consists of the following events: 3259 The module lifecycle consists of the following events:
3218 3260
3219 <list type="bullet"> 3261 <list type="bullet">
3220 3262
3221 <listitem> 3263 <listitem>
3222 Configuration directive handlers are called as they appear 3264 Configuration directive handlers are called as they appear
3223 in configuration files in the context of the master process 3265 in configuration files in the context of the master process.
3224 </listitem> 3266 </listitem>
3225 3267
3226 <listitem> 3268 <listitem>
3227 The <literal>init_module</literal> handler is called in the context of 3269 After the configuration is parsed successfully, <literal>init_module</literal>
3228 the master process after the configuration is parsed successfully 3270 handler is called in the context of the master process.
3229 </listitem> 3271 The <literal>init_module</literal> handler is called in the master process each
3230 3272 time a configuration is loaded.
3231 <listitem> 3273 </listitem>
3232 The master process creates worker process(es) and 3274
3233 <literal>init_process</literal> handler is called in each of them 3275 <listitem>
3234 </listitem> 3276 The master process creates one or more worker processes and the
3235 3277 <literal>init_process</literal> handler is called in each of them.
3236 <listitem> 3278 </listitem>
3237 When a worker process receives the shutdown command from master, it invokes 3279
3238 the <literal>exit_process</literal> handler 3280 <listitem>
3281 When a worker process receives the shutdown or terminate command from the
3282 master, it invokes the <literal>exit_process</literal> handler.
3239 </listitem> 3283 </listitem>
3240 3284
3241 <listitem> 3285 <listitem>
3242 The master process calls the <literal>exit_master</literal> handler before 3286 The master process calls the <literal>exit_master</literal> handler before
3243 exiting. 3287 exiting.
3244 </listitem> 3288 </listitem>
3245 3289
3246 </list> 3290 </list>
3247 3291
3248 <note> 3292 Because threads are used in nginx only as a supplementary I/O facility with its
3249 <literal>init_module</literal> handler may be called multiple times 3293 own API, <literal>init_thread</literal> and <literal>exit_thread</literal>
3250 in the master process if the configuration reload is requested. 3294 handlers are not currently called.
3251 </note> 3295 There is also no <literal>init_master</literal> handler, because it would be
3252 3296 unnecessary overhead.
3253 The <literal>init_master</literal>, <literal>init_thread</literal> and 3297 </para>
3254 <literal>exit_thread</literal> handlers are not implemented at the moment; 3298
3255 Threads in nginx are only used as supplementary I/O facility with its own 3299 <para>
3256 API and <literal>init_master</literal> handler looks unnecessary. 3300 The module <literal>type</literal> defines exactly what is stored in the
3257 </para>
3258
3259 <para>
3260 The module <literal>type</literal> defines what exactly is stored in the
3261 <literal>ctx</literal> field. 3301 <literal>ctx</literal> field.
3262 There are several types of modules: 3302 Its value is one of the following types:
3263 <list type="bullet"> 3303 <list type="bullet">
3264 <listitem><literal>NGX_CORE_MODULE</literal></listitem> 3304 <listitem><literal>NGX_CORE_MODULE</literal></listitem>
3265 <listitem><literal>NGX_EVENT_MODULE</literal></listitem> 3305 <listitem><literal>NGX_EVENT_MODULE</literal></listitem>
3266 <listitem><literal>NGX_HTTP_MODULE</literal></listitem> 3306 <listitem><literal>NGX_HTTP_MODULE</literal></listitem>
3267 <listitem><literal>NGX_MAIL_MODULE</literal></listitem> 3307 <listitem><literal>NGX_MAIL_MODULE</literal></listitem>
3268 <listitem><literal>NGX_STREAM_MODULE</literal></listitem> 3308 <listitem><literal>NGX_STREAM_MODULE</literal></listitem>
3269 </list> 3309 </list>
3270 The <literal>NGX_CORE_MODULE</literal> is the most basic and thus the most 3310 The <literal>NGX_CORE_MODULE</literal> is the most basic and thus the most
3271 generic and most low-level type of module. Other module types are implemented 3311 generic and most low-level type of module.
3272 on top of it and provide more convenient way to deal with corresponding 3312 The other module types are implemented on top of it and provide a more
3273 problem domains, like handling events or http requests. 3313 convenient way to deal with corresponding domains, like handling events or HTTP
3274 </para> 3314 requests.
3275 3315 </para>
3276 <para> 3316
3277 The examples of core modules are <literal>ngx_core_module</literal>, 3317 <para>
3318 The set of core modules includes <literal>ngx_core_module</literal>,
3278 <literal>ngx_errlog_module</literal>, <literal>ngx_regex_module</literal>, 3319 <literal>ngx_errlog_module</literal>, <literal>ngx_regex_module</literal>,
3279 <literal>ngx_thread_pool_module</literal>, 3320 <literal>ngx_thread_pool_module</literal> and
3280 <literal>ngx_openssl_module</literal> 3321 <literal>ngx_openssl_module</literal> modules.
3281 modules and, of course, http, stream, mail and event modules itself. 3322 The HTTP module, the stream module, the mail module and event modules are core
3323 modules too.
3282 The context of a core module is defined as: 3324 The context of a core module is defined as:
3283 <programlisting> 3325 <programlisting>
3284 typedef struct { 3326 typedef struct {
3285 ngx_str_t name; 3327 ngx_str_t name;
3286 void *(*create_conf)(ngx_cycle_t *cycle); 3328 void *(*create_conf)(ngx_cycle_t *cycle);
3287 char *(*init_conf)(ngx_cycle_t *cycle, void *conf); 3329 char *(*init_conf)(ngx_cycle_t *cycle, void *conf);
3288 } ngx_core_module_t; 3330 } ngx_core_module_t;
3289 </programlisting> 3331 </programlisting>
3290 where the <literal>name</literal> is a string with a module name for 3332 where the <literal>name</literal> is a module name string,
3291 convenience, <literal>create_conf</literal> and <literal>init_conf</literal> 3333 <literal>create_conf</literal> and <literal>init_conf</literal>
3292 are pointers to functions that create and initialize module configuration 3334 are pointers to functions that create and initialize module configuration
3293 correspondingly. 3335 respectively.
3294 For core modules, nginx will call <literal>create_conf</literal> before parsing 3336 For core modules, nginx calls <literal>create_conf</literal> before parsing
3295 a new configuration and <literal>init_conf</literal> after all configuration 3337 a new configuration and <literal>init_conf</literal> after all configuration
3296 was parsed successfully. 3338 is parsed successfully.
3297 The typical <literal>create_conf</literal> function allocates memory for the 3339 The typical <literal>create_conf</literal> function allocates memory for the
3298 configuration and sets default values. 3340 configuration and sets default values.
3299 The <literal>init_conf</literal> deals with known configuration and thus may 3341 </para>
3300 perform sanity checks and complete initialization. 3342
3301 </para> 3343 <para>
3302 3344 For example, a simplistic module called <literal>ngx_foo_module</literal> might
3303 <para> 3345 look like this:
3304 For example, the simplistic <literal>ngx_foo_module</literal> can look like
3305 this:
3306 <programlisting> 3346 <programlisting>
3307 /* 3347 /*
3308 * Copyright (C) Author. 3348 * Copyright (C) Author.
3309 */ 3349 */
3310 3350
3405 </para> 3445 </para>
3406 3446
3407 </section> 3447 </section>
3408 3448
3409 3449
3410 <section name="Configuration directives" id="config_directives"> 3450 <section name="Configuration Directives" id="config_directives">
3411 3451
3412 <para> 3452 <para>
3413 The <literal>ngx_command_t</literal> describes single configuration directive. 3453 The <literal>ngx_command_t</literal> type defines a single configuration
3414 Each module, supporting configuration, provides an array of such specifications 3454 directive.
3455 Each module that supports configuration provides an array of such structures
3415 that describe how to process arguments and what handlers to call: 3456 that describe how to process arguments and what handlers to call:
3416 <programlisting> 3457 <programlisting>
3458 typedef struct ngx_command_s ngx_command_t;
3459
3417 struct ngx_command_s { 3460 struct ngx_command_s {
3418 ngx_str_t name; 3461 ngx_str_t name;
3419 ngx_uint_t type; 3462 ngx_uint_t type;
3420 char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); 3463 char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
3421 ngx_uint_t conf; 3464 ngx_uint_t conf;
3422 ngx_uint_t offset; 3465 ngx_uint_t offset;
3423 void *post; 3466 void *post;
3424 }; 3467 };
3425 </programlisting> 3468 </programlisting>
3426 The array should be terminated by a special value 3469 Terminate the array with the special value <literal>ngx_null_command</literal>.
3427 “<literal>ngx_null_command</literal>”. 3470 The <literal>name</literal> is the name of a directive as it appears
3428 The <literal>name</literal> is the literal name of a directive, as it appears 3471 in the configuration file, for example "worker_processes" or "listen".
3429 in configuration file, for example “<literal>worker_processes</literal>” or 3472 The <literal>type</literal> is a bit-field of flags that specify the number of
3430 “<literal>listen</literal>”. 3473 arguments the directive takes, its type, and the context in which it appears.
3431 The <literal>type</literal> is a bitfield that controls number of arguments, 3474 The flags are:
3432 command type and other properties using corresponding flags. 3475
3433 Arguments flags: 3476 <list type="bullet">
3434 3477
3435 <list type="bullet"> 3478 <listitem>
3436 3479 <literal>NGX_CONF_NOARGS</literal> — Directive takes no arguments.
3437 <listitem> 3480 </listitem>
3438 <literal>NGX_CONF_NOARGS</literal> — directive without arguments 3481
3439 </listitem> 3482 <listitem>
3440 3483 <literal>NGX_CONF_1MORE</literal> — Directive takes one or more arguments.
3441 <listitem><literal>NGX_CONF_1MORE</literal> — one required argument</listitem> 3484 </listitem>
3442 3485
3443 <listitem><literal>NGX_CONF_2MORE</literal> — two required arguments</listitem> 3486 <listitem>
3444 3487 <literal>NGX_CONF_2MORE</literal> — Directive takes two or more arguments.
3445 <listitem> 3488 </listitem>
3446 <literal>NGX_CONF_TAKE1..7</literal> — exactly 1..7 arguments 3489
3447 </listitem> 3490 <listitem>
3448 3491 <literal>NGX_CONF_TAKE1</literal>..<literal>NGX_CONF_TAKE7</literal> —
3449 <listitem> 3492 Directive takes exactly the indicated number of arguments.
3450 <literal>NGX_CONF_TAKE12, 13, 23, 123, 1234</literal> — one or two arguments, 3493 </listitem>
3451 or other combinations 3494
3452 </listitem> 3495 <listitem>
3453 3496 <literal>NGX_CONF_TAKE12</literal>, <literal>NGX_CONF_TAKE13</literal>,
3454 </list> 3497 <literal>NGX_CONF_TAKE23</literal>, <literal>NGX_CONF_TAKE123</literal>,
3455 3498 <literal>NGX_CONF_TAKE1234</literal> — Directive may take different number of
3456 Directive types: 3499 arguments.
3457 3500 Options are limited to the given numbers.
3458 <list type="bullet"> 3501 For example, <literal>NGX_CONF_TAKE12</literal> means it takes one or two
3459 3502 arguments.
3460 <listitem> 3503 </listitem>
3461 <literal>NGX_CONF_BLOCK</literal> — the directive is a block, i.e. it may 3504
3462 contain other directives in curly braces, or even implement its own parser 3505 </list>
3463 to handle contents inside. 3506
3464 </listitem> 3507 The flags for directive types are:
3465 3508
3466 <listitem> 3509 <list type="bullet">
3467 <literal>NGX_CONF_FLAG</literal> — the directive value is a flag, a boolean 3510
3468 value represented by “<literal>on</literal>” or “<literal>off</literal>” 3511 <listitem>
3469 strings. 3512 <literal>NGX_CONF_BLOCK</literal> — Directive is a block, that is, it can
3470 </listitem> 3513 contain other directives within its opening and closing curly braces, or even
3471 </list> 3514 implement its own parser to handle contents inside.
3472 3515 </listitem>
3473 Context of a directive defines where in the configuration it may appear 3516
3474 and how to access module context to store corresponding values: 3517 <listitem>
3475 <list type="bullet"> 3518 <literal>NGX_CONF_FLAG</literal> — Directive takes a boolean value, either
3476 3519 <literal>on</literal> or <literal>off</literal>.
3477 <listitem> 3520 </listitem>
3478 <literal>NGX_MAIN_CONF</literal> — top level configuration 3521
3479 </listitem> 3522 </list>
3480 3523
3481 <listitem> 3524 A directive's context defines where it may appear in the configuration:
3482 <literal>NGX_HTTP_MAIN_CONF</literal> — in the http block 3525
3483 </listitem> 3526 <list type="bullet">
3484 3527
3485 <listitem> 3528 <listitem>
3486 <literal>NGX_HTTP_SRV_CONF</literal> — in the http server block 3529 <literal>NGX_MAIN_CONF</literal> — In the top level context.
3487 </listitem> 3530 </listitem>
3488 3531
3489 <listitem> 3532 <listitem>
3490 <literal>NGX_HTTP_LOC_CONF</literal> — in the http location 3533 <literal>NGX_HTTP_MAIN_CONF</literal> — In the <literal>http</literal> block.
3491 </listitem> 3534 </listitem>
3492 3535
3493 <listitem> 3536 <listitem>
3494 <literal>NGX_HTTP_UPS_CONF</literal> — in the http upstream block 3537 <literal>NGX_HTTP_SRV_CONF</literal> — In a <literal>server</literal> block
3495 </listitem> 3538 within the <literal>http</literal> block.
3496 3539 </listitem>
3497 <listitem> 3540
3498 <literal>NGX_HTTP_SIF_CONF</literal> — in the http server “if” 3541 <listitem>
3499 </listitem> 3542 <literal>NGX_HTTP_LOC_CONF</literal> — In a <literal>location</literal> block
3500 3543 within the <literal>http</literal> block.
3501 <listitem> 3544 </listitem>
3502 <literal>NGX_HTTP_LIF_CONF</literal> — in the http location “if” 3545
3503 </listitem> 3546 <listitem>
3504 3547 <literal>NGX_HTTP_UPS_CONF</literal> — In an <literal>upstream</literal> block
3505 <listitem> 3548 within the <literal>http</literal> block.
3506 <literal>NGX_HTTP_LMT_CONF</literal> — in the http “limit_except” 3549 </listitem>
3507 </listitem> 3550
3508 3551 <listitem>
3509 <listitem> 3552 <literal>NGX_HTTP_SIF_CONF</literal> — In an <literal>if</literal> block within
3510 <literal>NGX_STREAM_MAIN_CONF</literal> — in the stream block 3553 a <literal>server</literal> block in the <literal>http</literal> block.
3511 </listitem> 3554 </listitem>
3512 3555
3513 <listitem> 3556 <listitem>
3514 <literal>NGX_STREAM_SRV_CONF</literal> — in the stream server block 3557 <literal>NGX_HTTP_LIF_CONF</literal> — In an <literal>if</literal> block within
3515 </listitem> 3558 a <literal>location</literal> block in the <literal>http</literal> block.
3516 3559 </listitem>
3517 <listitem> 3560
3518 <literal>NGX_STREAM_UPS_CONF</literal> — in the stream upstream block 3561 <listitem>
3519 </listitem> 3562 <literal>NGX_HTTP_LMT_CONF</literal> — In a <literal>limit_except</literal>
3520 3563 block within the <literal>http</literal> block.
3521 <listitem> 3564 </listitem>
3522 <literal>NGX_MAIL_MAIN_CONF</literal> — in the the mail block 3565
3523 </listitem> 3566 <listitem>
3524 3567 <literal>NGX_STREAM_MAIN_CONF</literal> — In the <literal>stream</literal>
3525 <listitem> 3568 block.
3526 <literal>NGX_MAIL_SRV_CONF</literal> — in the mail server block 3569 </listitem>
3527 </listitem> 3570
3528 3571 <listitem>
3529 <listitem> 3572 <literal>NGX_STREAM_SRV_CONF</literal> — In a <literal>server</literal> block
3530 <literal>NGX_EVENT_CONF</literal> — in the event block 3573 within the <literal>stream</literal> block.
3531 </listitem> 3574 </listitem>
3532 3575
3533 <listitem> 3576 <listitem>
3534 <literal>NGX_DIRECT_CONF</literal> — used by modules that don't 3577 <literal>NGX_STREAM_UPS_CONF</literal> — In an <literal>upstream</literal> block
3535 create a hierarchy of contexts and store module configuration directly in ctx 3578 within the <literal>stream</literal> block.
3536 </listitem> 3579 </listitem>
3537 </list> 3580
3538 The configuration parser uses this flags to throw an error in case of 3581 <listitem>
3582 <literal>NGX_MAIL_MAIN_CONF</literal> — In the <literal>mail</literal> block.
3583 </listitem>
3584
3585 <listitem>
3586 <literal>NGX_MAIL_SRV_CONF</literal> — In a <literal>server</literal> block
3587 within the <literal>mail</literal> block.
3588 </listitem>
3589
3590 <listitem>
3591 <literal>NGX_EVENT_CONF</literal> — In the <literal>event</literal> block.
3592 </listitem>
3593
3594 <listitem>
3595 <literal>NGX_DIRECT_CONF</literal> — Used by modules that don't
3596 create a hierarchy of contexts and only have one global configuration.
3597 This configuration is passed to the handler as the <literal>conf</literal>
3598 argument.
3599 </listitem>
3600 </list>
3601
3602 The configuration parser uses these flags to throw an error in case of
3539 a misplaced directive and calls directive handlers supplied with a proper 3603 a misplaced directive and calls directive handlers supplied with a proper
3540 configuration pointer, so that same directives in different locations could 3604 configuration pointer, so that the same directives in different locations can
3541 store their values in distinct places. 3605 store their values in distinct places.
3542 </para> 3606 </para>
3543 3607
3544 <para> 3608 <para>
3545 The <literal>set</literal> field defines a handler that processes a directive 3609 The <literal>set</literal> field defines a handler that processes a directive
3546 and stores parsed values into corresponding configuration. 3610 and stores parsed values into the corresponding configuration.
3547 Nginx offers a convenient set of functions that perform common conversions: 3611 There's a number of functions that perform common conversions:
3548 3612
3549 <list type="bullet"> 3613 <list type="bullet">
3550 3614
3551 <listitem> 3615 <listitem>
3552 <literal>ngx_conf_set_flag_slot</literal> — converts literal 3616 <literal>ngx_conf_set_flag_slot</literal> — Converts the literal strings
3553 “<literal>on</literal>” or “<literal>off</literal>” strings into 3617 <literal>on</literal> and <literal>off</literal> into an
3554 <literal>ngx_flag_t</literal> type with values 1 or 0 3618 <literal>ngx_flag_t</literal> value with values 1 or 0, respectively.
3555 </listitem> 3619 </listitem>
3556 3620
3557 <listitem> 3621 <listitem>
3558 <literal>ngx_conf_set_str_slot</literal> — stores string as a value of the 3622 <literal>ngx_conf_set_str_slot</literal> — Stores a string as a value of the
3559 <literal>ngx_str_t</literal> type 3623 <literal>ngx_str_t</literal> type.
3560 </listitem> 3624 </listitem>
3561 3625
3562 <listitem> 3626 <listitem>
3563 <literal>ngx_conf_set_str_array_slot</literal> — appends 3627 <literal>ngx_conf_set_str_array_slot</literal> — Appends a value to an array
3564 <literal>ngx_array_t</literal> of <literal>ngx_str_t</literal> with a new value. 3628 <literal>ngx_array_t</literal> of strings <literal>ngx_str_t</literal>.
3565 The array is created if not yet exists 3629 The array is created if does not already exist.
3566 </listitem> 3630 </listitem>
3567 3631
3568 <listitem> 3632 <listitem>
3569 <literal>ngx_conf_set_keyval_slot</literal> — appends 3633 <literal>ngx_conf_set_keyval_slot</literal> — Appends a key-value pair to an
3570 <literal>ngx_array_t</literal> of <literal>ngx_keyval_t</literal> with 3634 array <literal>ngx_array_t</literal> of key-value pairs
3571 a new value, where key is the first string and value is second. 3635 <literal>ngx_keyval_t</literal>.
3572 The array is created if not yet exists 3636 The first string becomes the key and the second the value.
3573 </listitem> 3637 The array is created if it does not already exist.
3574 3638 </listitem>
3575 <listitem> 3639
3576 <literal>ngx_conf_set_num_slot</literal> — converts directive argument 3640 <listitem>
3577 to a <literal>ngx_int_t</literal> value 3641 <literal>ngx_conf_set_num_slot</literal> — Converts a directive's argument
3578 </listitem> 3642 to an <literal>ngx_int_t</literal> value.
3579 3643 </listitem>
3580 <listitem> 3644
3581 <literal>ngx_conf_set_size_slot</literal> — converts 3645 <listitem>
3582 <link doc="../syntax.xml">size</link> to <literal>size_t</literal> value 3646 <literal>ngx_conf_set_size_slot</literal> — Converts a
3583 in bytes 3647 <link doc="../syntax.xml">size</link> to a <literal>size_t</literal> value
3584 </listitem> 3648 expressed in bytes.
3585 3649 </listitem>
3586 <listitem> 3650
3587 <literal>ngx_conf_set_off_slot</literal> — converts 3651 <listitem>
3588 <link doc="../syntax.xml">offset</link> to <literal>off_t</literal> value 3652 <literal>ngx_conf_set_off_slot</literal> — Converts an
3589 in bytes 3653 <link doc="../syntax.xml">offset</link> to an <literal>off_t</literal> value
3590 </listitem> 3654 expressed in bytes.
3591 3655 </listitem>
3592 <listitem> 3656
3593 <literal>ngx_conf_set_msec_slot</literal> — converts 3657 <listitem>
3594 <link doc="../syntax.xml">time</link> to <literal>ngx_msec_t</literal> value 3658 <literal>ngx_conf_set_msec_slot</literal> — Converts a
3595 in milliseconds 3659 <link doc="../syntax.xml">time</link> to an <literal>ngx_msec_t</literal> value
3596 </listitem> 3660 expressed in milliseconds.
3597 3661 </listitem>
3598 <listitem> 3662
3599 <literal>ngx_conf_set_sec_slot</literal> — converts 3663 <listitem>
3600 <link doc="../syntax.xml">time</link> to <literal>time_t</literal> value 3664 <literal>ngx_conf_set_sec_slot</literal> — Converts a
3601 in seconds 3665 <link doc="../syntax.xml">time</link> to a <literal>time_t</literal> value
3602 </listitem> 3666 expressed in in seconds.
3603 3667 </listitem>
3604 <listitem> 3668
3605 <literal>ngx_conf_set_bufs_slot</literal> — converts two arguments 3669 <listitem>
3606 into <literal>ngx_bufs_t</literal> that holds <literal>ngx_int_t</literal> 3670 <literal>ngx_conf_set_bufs_slot</literal> — Converts the two supplied arguments
3607 number and <link doc="../syntax.xml">size</link> of buffers 3671 into an <literal>ngx_bufs_t</literal> object that holds the number and
3608 </listitem> 3672 <link doc="../syntax.xml">size</link> of buffers.
3609 3673 </listitem>
3610 <listitem> 3674
3611 <literal>ngx_conf_set_enum_slot</literal> — converts argument 3675 <listitem>
3612 into <literal>ngx_uint_t</literal> value. 3676 <literal>ngx_conf_set_enum_slot</literal> — Converts the supplied argument
3677 into an <literal>ngx_uint_t</literal> value.
3613 The null-terminated array of <literal>ngx_conf_enum_t</literal> passed in the 3678 The null-terminated array of <literal>ngx_conf_enum_t</literal> passed in the
3614 <literal>post</literal> field defines acceptable strings and corresponding 3679 <literal>post</literal> field defines the acceptable strings and corresponding
3615 integer values 3680 integer values.
3616 </listitem> 3681 </listitem>
3617 3682
3618 <listitem> 3683 <listitem>
3619 <literal>ngx_conf_set_bitmask_slot</literal> — arguments are converted to 3684 <literal>ngx_conf_set_bitmask_slot</literal> — Converts the supplied arguments
3620 <literal>ngx_uint_t</literal> value and OR'ed with the resulting value, 3685 into an <literal>ngx_uint_t</literal> value.
3621 forming a bitmask. 3686 The mask values for each argument are ORed producing the result.
3622 The null-terminated array of <literal>ngx_conf_bitmask_t</literal> passed in 3687 The null-terminated array of <literal>ngx_conf_bitmask_t</literal> passed in the
3623 the <literal>post</literal> field defines acceptable strings and corresponding 3688 <literal>post</literal> field defines the acceptable strings and corresponding
3624 mask values 3689 mask values.
3625 </listitem> 3690 </listitem>
3626 3691
3627 <listitem> 3692 <listitem>
3628 <literal>set_path_slot</literal> — converts arguments to 3693 <literal>set_path_slot</literal> — Converts the supplied arguments to an
3629 <literal>ngx_path_t</literal> type and performs all required initializations. 3694 <literal>ngx_path_t</literal> value and performs all required initializations.
3630 See the 3695 For details, see the documentation for the
3631 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_temp_path">proxy_temp_path</link> 3696 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_temp_path">
3632 directive description for details 3697 proxy_temp_path</link> directive.
3633 </listitem> 3698 </listitem>
3634 3699
3635 <listitem> 3700 <listitem>
3636 <literal>set_access_slot</literal> — converts arguments to file permissions 3701 <literal>set_access_slot</literal> — Converts the supplied arguments to a file
3637 mask. 3702 permissions mask.
3638 See the 3703 For details, see the documentation for the
3639 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_store_access">proxy_store_access</link> 3704 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_store_access">
3640 directive description for details 3705 proxy_store_access</link> directive.
3641 </listitem> 3706 </listitem>
3642 3707
3643 </list> 3708 </list>
3644 3709
3645 </para> 3710 </para>
3646 3711
3647 <para> 3712 <para>
3648 The <literal>conf</literal> field defines which context is used to store 3713 The <literal>conf</literal> field defines which configuration structure is
3649 the value of the directive, or zero if contexts are not used. 3714 passed to the directory handler.
3650 Only simple core modules use configuration without context and set 3715 Core modules only have the global configuration and set
3651 <literal>NGX_DIRECT_CONF</literal> flag. 3716 <literal>NGX_DIRECT_CONF</literal> flag to access it.
3652 In real life, such modules like http or stream require more sophisticated 3717 Modules like HTTP, Stream or Mail create hierarchies of configurations.
3653 configuration that can be applied per-server or per-location, or even more 3718 For example, a module's configuration is created for <literal>server</literal>,
3654 precisely, in the context of the “<literal>if</literal>” directive or 3719 <literal>location</literal> and <literal>if</literal> scopes.
3655 some limit. 3720
3656 In this modules, configuration structure is more complex. 3721 <list type="bullet">
3657 Please refer to corresponding modules description to understand how 3722 <listitem>
3658 they manage their configuration. 3723 <literal>NGX_HTTP_MAIN_CONF_OFFSET</literal> — Configuration for the
3659 3724 <literal>http</literal> block.
3660 <list type="bullet"> 3725 </listitem>
3661 <listitem> 3726
3662 <literal>NGX_HTTP_MAIN_CONF_OFFSET</literal> — http block configuration 3727 <listitem>
3663 </listitem> 3728 <literal>NGX_HTTP_SRV_CONF_OFFSET</literal> — Configuration for a
3664 3729 <literal>server</literal> block within the <literal>http</literal> block.
3665 <listitem> 3730 </listitem>
3666 <literal>NGX_HTTP_SRV_CONF_OFFSET</literal> — http server configuration 3731
3667 </listitem> 3732 <listitem>
3668 3733 <literal>NGX_HTTP_LOC_CONF_OFFSET</literal> — Configuration for a
3669 <listitem> 3734 <literal>location</literal> block within the <literal>http</literal>.
3670 <literal>NGX_HTTP_LOC_CONF_OFFSET</literal> — http location configuration 3735 </listitem>
3671 </listitem> 3736
3672 3737 <listitem>
3673 <listitem> 3738 <literal>NGX_STREAM_MAIN_CONF_OFFSET</literal> — Configuration for the
3674 <literal>NGX_STREAM_MAIN_CONF_OFFSET</literal> — stream block configuration 3739 <literal>stream</literal> block.
3675 </listitem> 3740 </listitem>
3676 3741
3677 <listitem> 3742 <listitem>
3678 <literal>NGX_STREAM_SRV_CONF_OFFSET</literal> — stream server configuration 3743 <literal>NGX_STREAM_SRV_CONF_OFFSET</literal> — Configuration for a
3679 </listitem> 3744 <literal>server</literal> block within the <literal>stream</literal> block.
3680 3745 </listitem>
3681 <listitem> 3746
3682 <literal>NGX_MAIL_MAIN_CONF_OFFSET</literal> — mail block configuration 3747 <listitem>
3683 </listitem> 3748 <literal>NGX_MAIL_MAIN_CONF_OFFSET</literal> — Configuration for the
3684 3749 <literal>mail</literal> block.
3685 <listitem> 3750 </listitem>
3686 <literal>NGX_MAIL_SRV_CONF_OFFSET</literal> — mail server configuration 3751
3687 </listitem> 3752 <listitem>
3688 3753 <literal>NGX_MAIL_SRV_CONF_OFFSET</literal> — Configuration for a
3689 </list> 3754 <literal>server</literal> block within the <literal>mail</literal> block.
3690 3755 </listitem>
3691 </para> 3756
3692 3757 </list>
3693 <para> 3758
3694 The <literal>offset</literal> defines an offset of a field in a module 3759 </para>
3695 configuration structure that holds values of this particular directive. 3760
3696 The typical use is to employ <literal>offsetof()</literal> macro. 3761 <para>
3697 </para> 3762 The <literal>offset</literal> defines the offset of a field in a module
3698 3763 configuration structure that holds values for this particular directive.
3699 <para> 3764 The typical use is to employ the <literal>offsetof()</literal> macro.
3700 The <literal>post</literal> is a twofold field: it may be used to define 3765 </para>
3701 a handler to be called after main handler completed or to pass additional 3766
3702 data to the main handler. 3767 <para>
3703 In the first case, <literal>ngx_conf_post_t</literal> structure needs to 3768 The <literal>post</literal> field has two purposes: it may be used to define
3704 be initialized with a pointer to handler, for example: 3769 a handler to be called after the main handler has completed, or to pass
3770 additional data to the main handler.
3771 In the first case, the <literal>ngx_conf_post_t</literal> structure needs to
3772 be initialized with a pointer to the handler, for example:
3705 <programlisting> 3773 <programlisting>
3706 static char *ngx_do_foo(ngx_conf_t *cf, void *post, void *data); 3774 static char *ngx_do_foo(ngx_conf_t *cf, void *post, void *data);
3707 static ngx_conf_post_t ngx_foo_post = { ngx_do_foo }; 3775 static ngx_conf_post_t ngx_foo_post = { ngx_do_foo };
3708 </programlisting> 3776 </programlisting>
3709 The <literal>post</literal> argument is the <literal>ngx_conf_post_t</literal> 3777 The <literal>post</literal> argument is the <literal>ngx_conf_post_t</literal>
3710 object itself, and the <literal>data</literal> is a pointer to value, 3778 object itself, and the <literal>data</literal> is a pointer to the value,
3711 converted from arguments by the main handler with the appropriate type. 3779 converted from arguments by the main handler with the appropriate type.
3712 </para> 3780 </para>
3713 3781
3714 </section> 3782 </section>
3715 3783
3720 3788
3721 3789
3722 <section name="Connection" id="http_connection"> 3790 <section name="Connection" id="http_connection">
3723 3791
3724 <para> 3792 <para>
3725 Each client HTTP connection runs through the following stages: 3793 Each HTTP client connection runs through the following stages:
3726 </para> 3794 </para>
3727 3795
3728 <list type="bullet"> 3796 <list type="bullet">
3729 3797
3730 <listitem> 3798 <listitem>
3731 <literal>ngx_event_accept()</literal> accepts a client TCP connection. 3799 <literal>ngx_event_accept()</literal> accepts a client TCP connection.
3732 This handler is called in response to a read notification on a listen socket. 3800 This handler is called in response to a read notification on a listen socket.
3733 A new <literal>ngx_connecton_t</literal> object is created at this stage. 3801 A new <literal>ngx_connecton_t</literal> object is created at this stage
3734 The object wraps the newly accepted client socket. 3802 to wrap the newly accepted client socket.
3735 Each nginx listener provides a handler to pass the new connection object to. 3803 Each nginx listener provides a handler to pass the new connection object to.
3736 For HTTP connections it's <literal>ngx_http_init_connection(c)</literal> 3804 For HTTP connections it's <literal>ngx_http_init_connection(c)</literal>.
3737 </listitem> 3805 </listitem>
3738 3806
3739 <listitem> 3807 <listitem>
3740 <literal>ngx_http_init_connection()</literal> performs early initialization of 3808 <literal>ngx_http_init_connection()</literal> performs early initialization of
3741 an HTTP connection. 3809 the HTTP connection.
3742 At this stage an <literal>ngx_http_connection_t</literal> object is created for 3810 At this stage an <literal>ngx_http_connection_t</literal> object is created for
3743 the connection and its reference is stored in connection's 3811 the connection and its reference is stored in the connection's
3744 <literal>data</literal> field. 3812 <literal>data</literal> field.
3745 Later it will be substituted with an HTTP request object. 3813 Later it will be replaced by an HTTP request object.
3746 PROXY protocol parser and SSL handshake are started at this stage as well 3814 A PROXY protocol parser and the SSL handshake are started at
3747 </listitem> 3815 this stage as well.
3748 3816 </listitem>
3749 <listitem> 3817
3750 <literal>ngx_http_wait_request_handler()</literal> is a read event handler, that 3818 <listitem>
3751 is called when data is available in the client socket. 3819 <literal>ngx_http_wait_request_handler()</literal> read event handler
3820 is called when data is available on the client socket.
3752 At this stage an HTTP request object <literal>ngx_http_request_t</literal> is 3821 At this stage an HTTP request object <literal>ngx_http_request_t</literal> is
3753 created and set to connection's <literal>data</literal> field 3822 created and set to the connection's <literal>data</literal> field.
3754 </listitem> 3823 </listitem>
3755 3824
3756 <listitem> 3825 <listitem>
3757 <literal>ngx_http_process_request_line()</literal> is a read event handler, 3826 <literal>ngx_http_process_request_line()</literal> read event handler
3758 which reads client request line. 3827 reads client request line.
3759 The handler is set by <literal>ngx_http_wait_request_handler()</literal>. 3828 The handler is set by <literal>ngx_http_wait_request_handler()</literal>.
3760 Reading is done into connection's <literal>buffer</literal>. 3829 The data is read into connection's <literal>buffer</literal>.
3761 The size of the buffer is initially set by the directive 3830 The size of the buffer is initially set by the directive
3762 <link doc="../http/ngx_http_core_module.xml" id="client_header_buffer_size"/>. 3831 <link doc="../http/ngx_http_core_module.xml" id="client_header_buffer_size"/>.
3763 The entire client header is supposed to fit the buffer. 3832 The entire client header is supposed to fit in the buffer.
3764 If the initial size is not enough, a bigger buffer is allocated, whose size is 3833 If the initial size is not sufficient, a bigger buffer is allocated,
3765 set by the <literal>large_client_header_buffers</literal> directive 3834 with the capacity set by the <literal>large_client_header_buffers</literal>
3766 </listitem> 3835 directive.
3767 3836 </listitem>
3768 <listitem> 3837
3769 <literal>ngx_http_process_request_headers()</literal> is a read event handler, 3838 <listitem>
3770 which is set after <literal>ngx_http_process_request_line()</literal> to read 3839 <literal>ngx_http_process_request_headers()</literal> read event handler,
3771 client request header 3840 is set after <literal>ngx_http_process_request_line()</literal> to read
3841 the client request header.
3772 </listitem> 3842 </listitem>
3773 3843
3774 <listitem> 3844 <listitem>
3775 <literal>ngx_http_core_run_phases()</literal> is called when the request header 3845 <literal>ngx_http_core_run_phases()</literal> is called when the request header
3776 is completely read and parsed. 3846 is completely read and parsed.
3777 This function runs request phases from 3847 This function runs request phases from
3778 <literal>NGX_HTTP_POST_READ_PHASE</literal> to 3848 <literal>NGX_HTTP_POST_READ_PHASE</literal> to
3779 <literal>NGX_HTTP_CONTENT_PHASE</literal>. 3849 <literal>NGX_HTTP_CONTENT_PHASE</literal>.
3780 The last phase is supposed to generate response and pass it along the filter 3850 The last phase is intended to generate a response and pass it along the filter
3781 chain. 3851 chain.
3782 The response is not necessarily sent to the client at this phase. 3852 The response is not necessarily sent to the client at this phase.
3783 It may remain buffered and will be sent at the finalization stage 3853 It might remain buffered and be sent at the finalization stage.
3784 </listitem> 3854 </listitem>
3785 3855
3786 <listitem> 3856 <listitem>
3787 <literal>ngx_http_finalize_request()</literal> is usually called when the 3857 <literal>ngx_http_finalize_request()</literal> is usually called when the
3788 request has generated all the output or produced an error. 3858 request has generated all the output or produced an error.
3789 In the latter case an appropriate error page is looked up and used as the 3859 In the latter case an appropriate error page is looked up and used as the
3790 response. 3860 response.
3791 If the response is not completely sent to the client by this point, an 3861 If the response is not completely sent to the client by this point, an
3792 HTTP writer <literal>ngx_http_writer()</literal> is activated to finish 3862 HTTP writer <literal>ngx_http_writer()</literal> is activated to finish
3793 sending outstanding data 3863 sending outstanding data.
3794 </listitem> 3864 </listitem>
3795 3865
3796 <listitem> 3866 <listitem>
3797 <literal>ngx_http_finalize_connection()</literal> is called when the response is 3867 <literal>ngx_http_finalize_connection()</literal> is called when the complete
3798 completely sent to the client and the request can be destroyed. 3868 response has been sent to the client and the request can be destroyed.
3799 If client connection keepalive feature is enabled, 3869 If the client connection keepalive feature is enabled,
3800 <literal>ngx_http_set_keepalive()</literal> is called, which destroys current 3870 <literal>ngx_http_set_keepalive()</literal> is called, which destroys the
3801 request and waits for the next request on the connection. 3871 current request and waits for the next request on the connection.
3802 Otherwise, <literal>ngx_http_close_request()</literal> destroys both the 3872 Otherwise, <literal>ngx_http_close_request()</literal> destroys both the
3803 request and the connection 3873 request and the connection.
3804 </listitem> 3874 </listitem>
3805 3875
3806 </list> 3876 </list>
3807 3877
3808 </section> 3878 </section>
3810 3880
3811 <section name="Request" id="http_request"> 3881 <section name="Request" id="http_request">
3812 3882
3813 <para> 3883 <para>
3814 For each client HTTP request the <literal>ngx_http_request_t</literal> object is 3884 For each client HTTP request the <literal>ngx_http_request_t</literal> object is
3815 created. Some of the fields of this object: 3885 created. Some of the fields of this object are:
3816 </para> 3886 </para>
3817 3887
3818 <list type="bullet"> 3888 <list type="bullet">
3819 3889
3820 <listitem> 3890 <listitem>
3821 3891
3822 <para> 3892 <para>
3823 <literal>connection</literal> — pointer to a <literal>ngx_connection_t</literal> 3893 <literal>connection</literal> — Pointer to a <literal>ngx_connection_t</literal>
3824 client connection object. 3894 client connection object.
3825 Several requests may reference the same connection object at the same time - 3895 Several requests can reference the same connection object at the same time -
3826 one main request and its subrequests. 3896 one main request and its subrequests.
3827 After a request is deleted, a new request may be created on the same connection. 3897 After a request is deleted, a new request can be created on the same connection.
3828 </para> 3898 </para>
3829 3899
3830 <para> 3900 <para>
3831 Note that for HTTP connections <literal>ngx_connection_t</literal>'s 3901 Note that for HTTP connections <literal>ngx_connection_t</literal>'s
3832 <literal>data</literal> field points back to the request. 3902 <literal>data</literal> field points back to the request.
3833 Such request is called active, as opposed to the other requests tied with the 3903 Such requests are called active, as opposed to the other requests tied to the
3834 connection. 3904 connection.
3835 Active request is used to handle client connection events and is allowed to 3905 An active request is used to handle client connection events and is allowed to
3836 output its response to the client. 3906 output its response to the client.
3837 Normally, each request becomes active at some point to be able to send its 3907 Normally, each request becomes active at some point so that it can send its
3838 output 3908 output.
3839 </para> 3909 </para>
3840 3910
3841 </listitem> 3911 </listitem>
3842 3912
3843 <listitem> 3913 <listitem>
3844 3914
3845 <para> 3915 <para>
3846 <literal>ctx</literal> — array of HTTP module contexts. 3916 <literal>ctx</literal> — Array of HTTP module contexts.
3847 Each module of type <literal>NGX_HTTP_MODULE</literal> can store any value 3917 Each module of type <literal>NGX_HTTP_MODULE</literal> can store any value
3848 (normally, a pointer to a structure) in the request. 3918 (normally, a pointer to a structure) in the request.
3849 The value is stored in the <literal>ctx</literal> array at the module's 3919 The value is stored in the <literal>ctx</literal> array at the module's
3850 <literal>ctx_index</literal> position. 3920 <literal>ctx_index</literal> position.
3851 The following macros provide a convenient way to get and set request contexts: 3921 The following macros provide a convenient way to get and set request contexts:
3852 </para> 3922 </para>
3853 3923
3854 <list type="bullet"> 3924 <list type="bullet">
3855 3925
3856 <listitem> 3926 <listitem>
3857 <literal>ngx_http_get_module_ctx(r, module)</literal> — returns 3927 <literal>ngx_http_get_module_ctx(r, module)</literal> — Returns
3858 <literal>module</literal>'s context 3928 the <literal>module</literal>'s context
3859 </listitem> 3929 </listitem>
3860 3930
3861 <listitem> 3931 <listitem>
3862 <literal>ngx_http_set_ctx(r, c, module)</literal> — sets <literal>c</literal> 3932 <literal>ngx_http_set_ctx(r, c, module)</literal> — Sets <literal>c</literal>
3863 as <literal>module</literal>'s context 3933 as the <literal>module</literal>'s context
3864 </listitem> 3934 </listitem>
3865 3935
3866 </list> 3936 </list>
3867 3937
3868 </listitem> 3938 </listitem>
3869 3939
3870 <listitem> 3940 <listitem>
3871 <literal>main_conf, srv_conf, loc_conf</literal> — arrays of current request 3941 <literal>main_conf</literal>, <literal>srv_conf</literal>,
3942 <literal>loc_conf</literal> — Arrays of current request
3872 configurations. 3943 configurations.
3873 Configurations are stored at module's <literal>ctx_index</literal> positions 3944 Configurations are stored at the module's <literal>ctx_index</literal>
3945 positions.
3874 </listitem> 3946 </listitem>
3875 3947
3876 <listitem> 3948 <listitem>
3877 <literal>read_event_handler</literal>, <literal>write_event_handler</literal> - 3949 <literal>read_event_handler</literal>, <literal>write_event_handler</literal> -
3878 read and write event handlers for the request. 3950 Read and write event handlers for the request.
3879 Normally, an HTTP connection has <literal>ngx_http_request_handler()</literal> 3951 Normally, both the read and write event handlers for an HTTP connection
3880 set as both read and write event handlers. 3952 are set to <literal>ngx_http_request_handler()</literal>.
3881 This function calls <literal>read_event_handler</literal> and 3953 This function calls the <literal>read_event_handler</literal> and
3882 <literal>write_event_handler</literal> handlers of the currently active request 3954 <literal>write_event_handler</literal> handlers for the currently
3883 </listitem> 3955 active request.
3884 3956 </listitem>
3885 <listitem> 3957
3886 <literal>cache</literal> — request cache object for caching upstream response 3958 <listitem>
3887 </listitem> 3959 <literal>cache</literal> — Request cache object for caching the
3888 3960 upstream response.
3889 <listitem> 3961 </listitem>
3890 <literal>upstream</literal> — request upstream object for proxying 3962
3891 </listitem> 3963 <listitem>
3892 3964 <literal>upstream</literal> — Request upstream object for proxying.
3893 <listitem> 3965 </listitem>
3894 <literal>pool</literal> — request pool. 3966
3895 This pool is destroyed when the request is deleted. 3967 <listitem>
3896 The request object itself is allocated in this pool. 3968 <literal>pool</literal> — Request pool.
3897 For allocations which should be available throughout the client connection's 3969 The request object itself is allocated in this pool, which is destroyed when
3898 lifetime, <literal>ngx_connection_t</literal>'s pool should be used instead 3970 the request is deleted.
3899 </listitem> 3971 For allocations that need to be available throughout the client connection's
3900 3972 lifetime, use <literal>ngx_connection_t</literal>'s pool instead.
3901 <listitem> 3973 </listitem>
3902 <literal>header_in</literal> — buffer where client HTTP request header in read 3974
3903 </listitem> 3975 <listitem>
3904 3976 <literal>header_in</literal> — Buffer into which the client HTTP request
3905 <listitem> 3977 header is read.
3906 <literal>headers_in, headers_out</literal> — input and output HTTP headers 3978 </listitem>
3907 objects. 3979
3980 <listitem>
3981 <literal>headers_in</literal>, <literal>headers_out</literal> — Input and
3982 output HTTP headers objects.
3908 Both objects contain the <literal>headers</literal> field of type 3983 Both objects contain the <literal>headers</literal> field of type
3909 <literal>ngx_list_t</literal> keeping the raw list of headers. 3984 <literal>ngx_list_t</literal> for keeping the raw list of headers.
3910 In addition to that, specific headers are available for getting and setting as 3985 In addition to that, specific headers are available for getting and setting as
3911 separate fields, for example <literal>content_length_n</literal>, 3986 separate fields, for example <literal>content_length_n</literal>,
3912 <literal>status</literal> etc 3987 <literal>status</literal> etc.
3913 </listitem> 3988 </listitem>
3914 3989
3915 <listitem> 3990 <listitem>
3916 <literal>request_body</literal> — client request body object 3991 <literal>request_body</literal> — Client request body object.
3917 </listitem> 3992 </listitem>
3918 3993
3919 <listitem> 3994 <listitem>
3920 <literal>start_sec, start_msec</literal> — time point when the request was 3995 <literal>start_sec</literal>, <literal>start_msec</literal> — Time point when
3921 created. 3996 the request was created, used for tracking request duration.
3922 Used for tracking request duration 3997 </listitem>
3923 </listitem> 3998
3924 3999 <listitem>
3925 <listitem> 4000 <literal>method</literal>, <literal>method_name</literal> — Numeric and text
3926 <literal>method, method_name</literal> — numeric and textual representation of 4001 representation of the client HTTP request method.
3927 client HTTP request method.
3928 Numeric values for methods are defined in 4002 Numeric values for methods are defined in
3929 <literal>src/http/ngx_http_request.h</literal> with macros 4003 <literal>src/http/ngx_http_request.h</literal> with the macros
3930 <literal>NGX_HTTP_GET, NGX_HTTP_HEAD, NGX_HTTP_POST</literal> etc 4004 <literal>NGX_HTTP_GET</literal>, <literal>NGX_HTTP_HEAD</literal>,
3931 </listitem> 4005 <literal>NGX_HTTP_POST</literal>, etc.
3932 4006 </listitem>
3933 <listitem> 4007
3934 <literal>http_protocol, http_version, http_major, http_minor</literal> - 4008 <listitem>
3935 client HTTP protocol version in its original textual form (“HTTP/1.0”, 4009 <literal>http_protocol</literal>  — Client HTTP protocol version in its
3936 “HTTP/1.1” etc), numeric form (<literal>NGX_HTTP_VERSION_10</literal>, 4010 original text form (“HTTP/1.0”, “HTTP/1.1” etc).
3937 <literal>NGX_HTTP_VERSION_11</literal> etc) and separate major and minor 4011 </listitem>
3938 versions 4012
3939 </listitem> 4013 <listitem>
3940 4014 <literal>http_version</literal>  — Client HTTP protocol version in
3941 <listitem> 4015 numeric form (<literal>NGX_HTTP_VERSION_10</literal>,
3942 <literal>request_line, unparsed_uri</literal> — client original request line 4016 <literal>NGX_HTTP_VERSION_11</literal>, etc.).
3943 and URI 4017 </listitem>
3944 </listitem> 4018
3945 4019 <listitem>
3946 <listitem> 4020 <literal>http_major</literal>, <literal>http_minor</literal>  — Client HTTP
3947 <literal>uri, args, exten</literal> — current request URI, arguments and file 4021 protocol version in numeric form split into major and minor parts.
3948 extention. 4022 </listitem>
4023
4024 <listitem>
4025 <literal>request_line</literal>, <literal>unparsed_uri</literal> — Request line
4026 and URI in the original client request.
4027 </listitem>
4028
4029 <listitem>
4030 <literal>uri</literal>, <literal>args</literal>, <literal>exten</literal> —
4031 URI, arguments and file extension for the current request.
3949 The URI value here might differ from the original URI sent by the client due to 4032 The URI value here might differ from the original URI sent by the client due to
3950 normalization. 4033 normalization.
3951 Throughout request processing, these value can change while performing internal 4034 Throughout request processing, these values can change as internal redirects
3952 redirects 4035 are performed.
3953 </listitem> 4036 </listitem>
3954 4037
3955 <listitem> 4038 <listitem>
3956 <literal>main</literal> — pointer to a main request object. 4039 <literal>main</literal> — Pointer to a main request object.
3957 This object is created to process client HTTP request, as opposed to 4040 This object is created to process a client HTTP request, as opposed to
3958 subrequests, created to perform a specific sub-task within the main request 4041 subrequests, which are created to perform a specific subtask within the main
3959 </listitem> 4042 request.
3960 4043 </listitem>
3961 <listitem> 4044
3962 <literal>parent</literal> — pointer to a parent request of a subrequest 4045 <listitem>
3963 </listitem> 4046 <literal>parent</literal> — Pointer to the parent request of a subrequest.
3964 4047 </listitem>
3965 <listitem> 4048
3966 <literal>postponed</literal> — list of output buffers and subrequests in the 4049 <listitem>
3967 order they are sent and created. 4050 <literal>postponed</literal> — List of output buffers and subrequests, in the
3968 The list is used by the postpone filter to provide consistent request output, 4051 order in which they are sent and created.
3969 when parts of it are created by subrequests 4052 The list is used by the postpone filter to provide consistent request output
3970 </listitem> 4053 when parts of it are created by subrequests.
3971 4054 </listitem>
3972 <listitem> 4055
3973 <literal>post_subrequest</literal> — pointer to a handler with context to be 4056 <listitem>
3974 called when a subrequest gets finalized. 4057 <literal>post_subrequest</literal> — Pointer to a handler with the context
3975 Unused for main requests 4058 to be called when a subrequest gets finalized.
3976 </listitem> 4059 Unused for main requests.
3977 4060 </listitem>
3978 <listitem> 4061
3979 4062 <listitem>
3980 <para> 4063
3981 <literal>posted_requests</literal> — list of requests to be started or 4064 <para>
3982 resumed. 4065 <literal>posted_requests</literal> — List of requests to be started or
3983 Starting or resuming is done by calling the request's 4066 resumed, which is done by calling the request's
3984 <literal>write_event_handler</literal>. 4067 <literal>write_event_handler</literal>.
3985 Normally, this handler holds the request main function, which at first runs 4068 Normally, this handler holds the request main function, which at first runs
3986 request phases and then produces the output. 4069 request phases and then produces the output.
3987 </para> 4070 </para>
3988 4071
3989 <para> 4072 <para>
3990 A request is usually posted by the 4073 A request is usually posted by the
3991 <literal>ngx_http_post_request(r, NULL)</literal> call. 4074 <literal>ngx_http_post_request(r, NULL)</literal> call.
3992 It is always posted to the main request <literal>posted_requests</literal> list. 4075 It is always posted to the main request <literal>posted_requests</literal> list.
3993 The function <literal>ngx_http_run_posted_requests(c)</literal> runs all 4076 The function <literal>ngx_http_run_posted_requests(c)</literal> runs all
3994 requests, posted in the main request of the passed connection's active request. 4077 requests that are posted in the main request of the passed
3995 This function should be called in all event handlers, which can lead to new 4078 connection's active request.
3996 posted requests. 4079 All event handlers call <literal>ngx_http_run_posted_requests</literal>,
3997 Normally, it's called always after invoking a request's read or write handler 4080 which can lead to new posted requests.
3998 </para> 4081 Normally, it is called after invoking a request's read or write handler.
3999 4082 </para>
4000 </listitem> 4083
4001 4084 </listitem>
4002 <listitem> 4085
4003 <literal>phase_handler</literal> — index of current request phase 4086 <listitem>
4004 </listitem> 4087 <literal>phase_handler</literal> — Index of current request phase.
4005 4088 </listitem>
4006 <listitem> 4089
4007 <literal>ncaptures, captures, captures_data</literal> — regex captures produced 4090 <listitem>
4091 <literal>ncaptures</literal>, <literal>captures</literal>,
4092 <literal>captures_data</literal> — Regex captures produced
4008 by the last regex match of the request. 4093 by the last regex match of the request.
4009 While processing a request, there's a number of places where a regex match can 4094 A regex match can occur at a number of places during request processing:
4010 happen: map lookup, server lookup by SNI or HTTP Host, rewrite, proxy_redirect 4095 map lookup, server lookup by SNI or HTTP Host, rewrite, proxy_redirect, etc.
4011 etc.
4012 Captures produced by a lookup are stored in the above mentioned fields. 4096 Captures produced by a lookup are stored in the above mentioned fields.
4013 The field <literal>ncaptures</literal> holds the number of captures, 4097 The field <literal>ncaptures</literal> holds the number of captures,
4014 <literal>captures</literal> holds captures boundaries, 4098 <literal>captures</literal> holds captures boundaries and
4015 <literal>captures_data</literal> holds a string, against which the regex was 4099 <literal>captures_data</literal> holds the string against which the regex was
4016 matched and which should be used to extract captures. 4100 matched and which is used to extract captures.
4017 After each new regex match request captures are reset to hold new values 4101 After each new regex match, request captures are reset to hold new values.
4018 </listitem> 4102 </listitem>
4019 4103
4020 <listitem> 4104 <listitem>
4021 <literal>count</literal> — request reference counter. 4105 <literal>count</literal> — Request reference counter.
4022 The field only makes sense for the main request. 4106 The field only makes sense for the main request.
4023 Increasing the counter is done by simple <literal>r->main->count++</literal>. 4107 Increasing the counter is done by simple <literal>r->main->count++</literal>.
4024 To decrease the counter <literal>ngx_http_finalize_request(r, rc)</literal> 4108 To decrease the counter, call
4025 should be called. 4109 <literal>ngx_http_finalize_request(r, rc)</literal>.
4026 Creation of a subrequest or running request body read process increase the 4110 Creating of a subrequest and running the request body read process both
4027 counter 4111 increment the counter.
4028 </listitem> 4112 </listitem>
4029 4113
4030 <listitem> 4114 <listitem>
4031 <literal>subrequests</literal> — current subrequest nesting level. 4115 <literal>subrequests</literal> — Current subrequest nesting level.
4032 Each subrequest gets the nesting level of its parent decreased by one. 4116 Each subrequest inherits its parent's nesting level, decreased by one.
4033 Once the value reaches zero an error is generated. 4117 An error is generated if the value reaches zero.
4034 The value for the main request is defined by the 4118 The value for the main request is defined by the
4035 <literal>NGX_HTTP_MAX_SUBREQUESTS</literal> constant 4119 <literal>NGX_HTTP_MAX_SUBREQUESTS</literal> constant.
4036 </listitem> 4120 </listitem>
4037 4121
4038 <listitem> 4122 <listitem>
4039 <literal>uri_changes</literal> — number of URI changes left for the request. 4123 <literal>uri_changes</literal> — Number of URI changes remaining for
4124 the request.
4040 The total number of times a request can change its URI is limited by the 4125 The total number of times a request can change its URI is limited by the
4041 <literal>NGX_HTTP_MAX_URI_CHANGES</literal> constant. 4126 <literal>NGX_HTTP_MAX_URI_CHANGES</literal> constant.
4042 With each change the value is decreased until it reaches zero. 4127 With each change the value is decremented until it reaches zero, at which time
4043 In the latter case an error is generated. 4128 an error is generated.
4044 The actions considered as URI changes are rewrites and internal redirects to 4129 Rewrites and internal redirects to normal or named locations are considered URI
4045 normal or named locations 4130 changes.
4046 </listitem> 4131 </listitem>
4047 4132
4048 <listitem> 4133 <listitem>
4049 <literal>blocked</literal> — counter of blocks held on the request. 4134 <literal>blocked</literal> — Counter of blocks held on the request.
4050 While this value is non-zero, request cannot be terminated. 4135 While this value is non-zero, the request cannot be terminated.
4051 Currently, this value is increased by pending AIO operations (POSIX AIO and 4136 Currently, this value is increased by pending AIO operations (POSIX AIO and
4052 thread operations) and active cache lock 4137 thread operations) and active cache lock.
4053 </listitem> 4138 </listitem>
4054 4139
4055 <listitem> 4140 <listitem>
4056 <literal>buffered</literal> — bitmask showing which modules have buffered the 4141 <literal>buffered</literal> — Bitmask showing which modules have buffered the
4057 output produced by the request. 4142 output produced by the request.
4058 A number of filters can buffer output, for example sub_filter can buffer data 4143 A number of filters can buffer output; for example, sub_filter can buffer data
4059 due to a partial string match, copy filter can buffer data because of the lack 4144 because of a partial string match, copy filter can buffer data because of the
4060 of free output_buffers etc. 4145 lack of free output buffers etc.
4061 As long as this value is non-zero, request is not finalized, expecting the flush 4146 As long as this value is non-zero, the request is not finalized
4062 </listitem> 4147 pending the flush.
4063 4148 </listitem>
4064 <listitem> 4149
4065 <literal>header_only</literal> — flag showing that output does not require body. 4150 <listitem>
4066 For example, this flag is used by HTTP HEAD requests 4151 <literal>header_only</literal> — Flag indicating that the output does not
4067 </listitem> 4152 require a body.
4068 4153 For example, this flag is used by HTTP HEAD requests.
4069 <listitem> 4154 </listitem>
4070 <para> 4155
4071 <literal>keepalive</literal> — flag showing if client connection keepalive is 4156 <listitem>
4072 supported. 4157 <para>
4073 The value is inferred from HTTP version and <header>Connection</header> header 4158 <literal>keepalive</literal> — Flag indicating whether client connection
4074 value 4159 keepalive is supported.
4075 </para> 4160 The value is inferred from the HTTP version and the value of the
4076 </listitem> 4161 <header>Connection</header> header.
4077 4162 </para>
4078 <listitem> 4163 </listitem>
4079 <literal>header_sent</literal> — flag showing that output header has already 4164
4080 been sent by the request 4165 <listitem>
4081 </listitem> 4166 <literal>header_sent</literal> — Flag indicating that the output header
4082 4167 has already been sent by the request.
4083 <listitem> 4168 </listitem>
4084 <literal>internal</literal> — flag showing that current request is internal. 4169
4085 To enter the internal state, a request should pass through an internal 4170 <listitem>
4171 <literal>internal</literal> — Flag indicating that the current request
4172 is internal.
4173 To enter the internal state, a request must pass through an internal
4086 redirect or be a subrequest. 4174 redirect or be a subrequest.
4087 Internal requests are allowed to enter internal locations 4175 Internal requests are allowed to enter internal locations.
4088 </listitem> 4176 </listitem>
4089 4177
4090 <listitem> 4178 <listitem>
4091 <literal>allow_ranges</literal> — flag showing that partial response can be 4179 <literal>allow_ranges</literal> — Flag indicating that a partial response
4092 sent to client, if requested by the HTTP Range header 4180 can be sent to the client, as requested by the HTTP Range header.
4093 </listitem> 4181 </listitem>
4094 4182
4095 <listitem> 4183 <listitem>
4096 <literal>subrequest_ranges</literal> — flag showing that a partial response is 4184 <literal>subrequest_ranges</literal> — Flag indicating that a partial response
4097 allowed to be sent while processing a subrequest 4185 can be sent while a subrequest is being processed.
4098 </listitem> 4186 </listitem>
4099 4187
4100 <listitem> 4188 <listitem>
4101 <literal>single_range</literal> — flag showing that only a single continuous 4189 <literal>single_range</literal> — Flag indicating that only a single continuous
4102 range of output data can be sent to the client. 4190 range of output data can be sent to the client.
4103 This flag is usually set when sending a stream of data, for example from a 4191 This flag is usually set when sending a stream of data, for example from a
4104 proxied server, and the entire response is not available at once 4192 proxied server, and the entire response is not available in one buffer.
4105 </listitem> 4193 </listitem>
4106 4194
4107 <listitem> 4195 <listitem>
4108 <literal>main_filter_need_in_memory, filter_need_in_memory</literal> — flags 4196 <literal>main_filter_need_in_memory</literal>,
4109 showing that the output should be produced in memory buffers but not in files. 4197 <literal>filter_need_in_memory</literal> — Flags
4198 requesting that the output produced in memory buffers rather than files.
4110 This is a signal to the copy filter to read data from file buffers even if 4199 This is a signal to the copy filter to read data from file buffers even if
4111 sendfile is enabled. 4200 sendfile is enabled.
4112 The difference between these two flags is the location of filter modules which 4201 The difference between the two flags is the location of the filter modules that
4113 set them. 4202 set them.
4114 Filters called before the postpone filter in filter chain, set 4203 Filters called before the postpone filter in the filter chain set
4115 <literal>filter_need_in_memory</literal> requesting that only the current 4204 <literal>filter_need_in_memory</literal>, requesting that only the current
4116 request output should come in memory buffers. 4205 request output come in memory buffers.
4117 Filters called later in filter chain set 4206 Filters called later in the filter chain set
4118 <literal>main_filter_need_in_memory</literal> requiring that both the main 4207 <literal>main_filter_need_in_memory</literal>, requesting that
4119 request and all the subrequest read files in memory while sending output 4208 both the main request and all subrequests read files in memory
4120 </listitem> 4209 while sending output.
4121 4210 </listitem>
4122 <listitem> 4211
4123 <literal>filter_need_temporary</literal> — flag showing that the request output 4212 <listitem>
4124 should be produced in temporary buffers, but not in readonly memory buffers or 4213 <literal>filter_need_temporary</literal> — Flag requesting that the request
4214 output be produced in temporary buffers, but not in readonly memory buffers or
4125 file buffers. 4215 file buffers.
4126 This is used by filters which may change output directly in the buffers, where 4216 This is used by filters which may change output directly in the buffers where
4127 it's sent </listitem> 4217 it's sent.</listitem>
4128 4218
4129 </list> 4219 </list>
4130 4220
4131 </section> 4221 </section>
4132 4222
4133 4223
4134 <section name="Configuration" id="http_conf"> 4224 <section name="Configuration" id="http_conf">
4135 4225
4136 <para> 4226 <para>
4137 Each HTTP module may have three types of configuration: 4227 Each HTTP module can have three types of configuration:
4138 </para> 4228 </para>
4139 4229
4140 <list type="bullet"> 4230 <list type="bullet">
4141 4231
4142 <listitem> 4232 <listitem>
4143 Main configuration. 4233 Main configuration — Applies to the entire <literal>http</literal> block.
4144 This configuration applies to the entire nginx http{} block. This is global 4234 Functions as global settings for a module.
4145 configuration. 4235 </listitem>
4146 It stores global settings for a module 4236
4147 </listitem> 4237 <listitem>
4148 4238 Server configuration — Applies to a single <literal>server</literal> block.
4149 <listitem> 4239 Functions as server-specific settings for a module.
4150 Server configuration. 4240 </listitem>
4151 This configuration applies to a single nginx server{}. 4241
4152 It stores server-specific settings for a module 4242 <listitem>
4153 </listitem> 4243 Location configuration — Applies to a single <literal>location</literal>,
4154 4244 <literal>if</literal> or <literal>limit_except</literal> block.
4155 <listitem> 4245 Functions as location-specific settings for a module.
4156 Location configuration. 4246 </listitem>
4157 This configuration applies to a single location{}, if{} or limit_except() block. 4247
4158 This configuration stores settings specific to a location 4248 </list>
4159 </listitem> 4249
4160 4250 <para>
4161 </list> 4251 Configuration structures are created at the nginx configuration stage by
4162 4252 calling functions, which allocate the structures, initialize them
4163 <para> 4253 and merge them.
4164 Configuration structures are created at nginx configuration stage by calling 4254 The following example shows how to create a simple location
4165 functions, which allocate these structures, initialize them and merge. 4255 configuration for a module.
4166 The following example shows how to create a simple module location 4256 The configuration has one setting, <literal>foo</literal>, of type
4167 configuration. 4257 unsigned integer.
4168 The configuration has one setting <literal>foo</literal> of unsiged integer
4169 type.
4170 </para> 4258 </para>
4171 4259
4172 <programlisting> 4260 <programlisting>
4173 typedef struct { 4261 typedef struct {
4174 ngx_uint_t foo; 4262 ngx_uint_t foo;
4215 ngx_conf_merge_uint_value(conf->foo, prev->foo, 1); 4303 ngx_conf_merge_uint_value(conf->foo, prev->foo, 1);
4216 } 4304 }
4217 </programlisting> 4305 </programlisting>
4218 4306
4219 <para> 4307 <para>
4220 As seen in the example, <literal>ngx_http_foo_create_loc_conf()</literal> 4308 As seen in the example, the <literal>ngx_http_foo_create_loc_conf()</literal>
4221 function creates a new configuration structure and 4309 function creates a new configuration structure, and
4222 <literal>ngx_http_foo_merge_loc_conf()</literal> merges a configuration with 4310 <literal>ngx_http_foo_merge_loc_conf()</literal> merges a configuration with
4223 another configuration from a higher level. 4311 configuration from a higher level.
4224 In fact, server and location configuration do not only exist at server and 4312 In fact, server and location configuration do not exist only at the server and
4225 location levels, but also created for all the levels above. 4313 location levels, but are also created for all levels above them.
4226 Specifically, a server configuration is created at the main level as well and 4314 Specifically, a server configuration is also created at the main level and
4227 location configurations are created for main, server and location levels. 4315 location configurations are created at the main, server, and location levels.
4228 These configurations make it possible to specify server and location-specific 4316 These configurations make it possible to specify server- and location-specific
4229 settings at any level of nginx configuration file. 4317 settings at any level of an nginx configuration file.
4230 Eventually configurations are merged down. 4318 Eventually configurations are merged down.
4231 To indicate a missing setting and ignore it while merging, nginx provides a 4319 A number of macros like <literal>NGX_CONF_UNSET</literal> and
4232 number of macros like <literal>NGX_CONF_UNSET</literal> and 4320 <literal>NGX_CONF_UNSET_UINT</literal> are provided
4233 <literal>NGX_CONF_UNSET_UINT</literal>. 4321 for indicating a missing setting and ignoring it while merging.
4234 Standard nginx merge macros like <literal>ngx_conf_merge_value()</literal> and 4322 Standard nginx merge macros like <literal>ngx_conf_merge_value()</literal> and
4235 <literal>ngx_conf_merge_uint_value()</literal> provide a convenient way to 4323 <literal>ngx_conf_merge_uint_value()</literal> provide a convenient way to
4236 merge a setting and set the default value if none of configurations provided an 4324 merge a setting and set the default value if none of the configurations
4237 explicit value. 4325 provided an explicit value.
4238 For complete list of macros for different types see 4326 For complete list of macros for different types, see
4239 <literal>src/core/ngx_conf_file.h</literal>. 4327 <literal>src/core/ngx_conf_file.h</literal>.
4240 </para> 4328 </para>
4241 4329
4242 <para> 4330 <para>
4243 To access configuration of any HTTP module at configuration time, the following 4331 The following macros are available.
4244 macros are available. 4332 for accessing configuration for HTTP modules at configuration time.
4245 They receive <literal>ngx_conf_t</literal> reference as the first argument. 4333 They all take <literal>ngx_conf_t</literal> reference as the first argument.
4246 </para> 4334 </para>
4247 4335
4248 <list type="bullet"> 4336 <list type="bullet">
4249 4337
4250 <listitem> 4338 <listitem>
4263 4351
4264 <para> 4352 <para>
4265 The following example gets a pointer to a location configuration of 4353 The following example gets a pointer to a location configuration of
4266 standard nginx core module 4354 standard nginx core module
4267 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link> 4355 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link>
4268 and changes 4356 and replaces the location content handler kept
4269 location content handler kept in the <literal>handler</literal> field of the 4357 in the <literal>handler</literal> field of the structure.
4270 structure.
4271 </para> 4358 </para>
4272 4359
4273 <programlisting> 4360 <programlisting>
4274 static ngx_int_t ngx_http_foo_handler(ngx_http_request_t *r); 4361 static ngx_int_t ngx_http_foo_handler(ngx_http_request_t *r);
4275 4362
4298 return NGX_CONF_OK; 4385 return NGX_CONF_OK;
4299 } 4386 }
4300 </programlisting> 4387 </programlisting>
4301 4388
4302 <para> 4389 <para>
4303 In runtime the following macros are available to get configurations of HTTP 4390 The following macros are available for accessing configuration for HTTP
4304 modules. 4391 modules at runtime.
4305 </para> 4392 </para>
4306 4393
4307 <list type="bullet"> 4394 <list type="bullet">
4308 4395
4309 <listitem> 4396 <listitem>
4321 </list> 4408 </list>
4322 4409
4323 <para> 4410 <para>
4324 These macros receive a reference to an HTTP request 4411 These macros receive a reference to an HTTP request
4325 <literal>ngx_http_request_t</literal>. 4412 <literal>ngx_http_request_t</literal>.
4326 Main configuration of a request never changes. 4413 The main configuration of a request never changes.
4327 Server configuration may change from a default one after choosing a virtual 4414 Server configuration can change from the default after
4328 server for a request. 4415 the virtual server for the request is chosen.
4329 Request location configuration may change multiple times as a result of a 4416 Location configuration selected for processing a request can change multiple
4330 rewrite or internal redirect. 4417 times as a result of a rewrite operation or internal redirect.
4331 The following example shows how to access HTTP configuration in runtime. 4418 The following example shows how to access a module's HTTP configuration at
4419 runtime.
4332 </para> 4420 </para>
4333 4421
4334 <programlisting> 4422 <programlisting>
4335 static ngx_int_t 4423 static ngx_int_t
4336 ngx_http_foo_handler(ngx_http_request_t *r) 4424 ngx_http_foo_handler(ngx_http_request_t *r)
4347 4435
4348 4436
4349 <section name="Phases" id="http_phases"> 4437 <section name="Phases" id="http_phases">
4350 4438
4351 <para> 4439 <para>
4352 Each HTTP request passes through a list of HTTP phases. 4440 Each HTTP request passes through a sequence of phases.
4353 Each phase is specialized in a particular type of processing. 4441 In each phase a distinct type of processing is performed on the request.
4354 Most phases allow installing handlers. 4442 Module-specific handlers can be registered in most phases,
4355 The phase handlers are called successively once the request reaches the phase. 4443 and many standard nginx modules register their phase handlers as a way
4356 Many standard nginx modules install their phase handlers as a way to get called 4444 to get called at a specific stage of request processing.
4357 at a specific request processing stage. 4445 Phases are processed successively and the phase handlers are called
4446 once the request reaches the phase.
4358 Following is the list of nginx HTTP phases. 4447 Following is the list of nginx HTTP phases.
4359 </para> 4448 </para>
4360 4449
4361 <list type="bullet"> 4450 <list type="bullet">
4362 4451
4363 <listitem> 4452 <listitem>
4364 <literal>NGX_HTTP_POST_READ_PHASE</literal> is the earliest phase. 4453 <literal>NGX_HTTP_POST_READ_PHASE</literal> — First phase.
4365 The <link doc="../http/ngx_http_realip_module.xml">ngx_http_realip_module</link> 4454 The <link doc="../http/ngx_http_realip_module.xml">ngx_http_realip_module</link>
4366 installs its handler at this phase. 4455 registers its handler at this phase to enable
4367 This allows to substitute client address before any other module is invoked 4456 substitution of client addresses before any other module is invoked.
4368 </listitem> 4457 </listitem>
4369 4458
4370 <listitem> 4459 <listitem>
4371 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> is used to run rewrite script, 4460 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> — Phase where
4372 defined at the server level, that is out of any location block. 4461 rewrite directives defined in a <literal>server</literal> block
4462 (but outside a <literal>location</literal> block) are processed.
4373 The 4463 The
4374 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link> 4464 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link>
4375 installs its handler at this phase 4465 installs its handler at this phase.
4376 </listitem> 4466 </listitem>
4377 4467
4378 <listitem> 4468 <listitem>
4379 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> — a special phase used to choose a 4469 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> — Special phase
4380 location based on request URI. 4470 where a location is chosen based on the request URI.
4381 This phase does not allow installing any handlers. 4471 Before this phase, the default location for the relevant virtual server
4382 It only performs the default action of choosing a location. 4472 is assigned to the request, and any module requesting a location configuration
4383 Before this phase, the server default location is assigned to the request. 4473 receives the configuration for the default server location.
4384 Any module requesting a location configuration, will receive the default server 4474 This phase a assigns a new location to the request.
4385 location configuration. 4475 No additional handlers can be registered at this phase.
4386 After this phase a new location is assigned to the request 4476 </listitem>
4387 </listitem> 4477
4388 4478 <listitem>
4389 <listitem> 4479 <literal>NGX_HTTP_REWRITE_PHASE</literal> — Same as
4390 <literal>NGX_HTTP_REWRITE_PHASE</literal> — same as 4480 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal>, but for
4391 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal>, but for a new location, 4481 rewrite rules defined in the location, chosen in the previous phase.
4392 chosen at the previous phase 4482 </listitem>
4393 </listitem> 4483
4394 4484 <listitem>
4395 <listitem> 4485 <literal>NGX_HTTP_POST_REWRITE_PHASE</literal> — Special phase
4396 <literal>NGX_HTTP_POST_REWRITE_PHASE</literal> — a special phase, used to 4486 where the request is redirected to a new location if its URI changed
4397 redirect the request to a new location, if the URI was changed during rewrite. 4487 during a rewrite.
4398 The redirect is done by going back to 4488 This is implemented by the request going through
4399 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal>. 4489 the <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> again.
4400 No handlers are allowed at this phase 4490 No additional handlers can be registered at this phase.
4401 </listitem> 4491 </listitem>
4402 4492
4403 <listitem> 4493 <listitem>
4404 <literal>NGX_HTTP_PREACCESS_PHASE</literal> — a common phase for different 4494 <literal>NGX_HTTP_PREACCESS_PHASE</literal> — A common phase for different
4405 types of handlers, not associated with access check. 4495 types of handlers, not associated with access control.
4406 Standard nginx modules 4496 The standard nginx modules
4407 <link doc="../http/ngx_http_limit_conn_module.xml">ngx_http_limit_conn_module 4497 <link doc="../http/ngx_http_limit_conn_module.xml">ngx_http_limit_conn_module
4408 </link> and 4498 </link> and
4409 <link doc="../http/ngx_http_limit_req_module.xml"> 4499 <link doc="../http/ngx_http_limit_req_module.xml">
4410 ngx_http_limit_req_module</link> register their handlers at this phase 4500 ngx_http_limit_req_module</link> register their handlers at this phase.
4411 </listitem> 4501 </listitem>
4412 4502
4413 <listitem> 4503 <listitem>
4414 <literal>NGX_HTTP_ACCESS_PHASE</literal> — used to check access permissions 4504 <literal>NGX_HTTP_ACCESS_PHASE</literal> — Phase where it is verified
4415 for the request. 4505 that the client is authorized to make the request.
4416 Standard nginx modules such as 4506 Standard nginx modules such as
4417 <link doc="../http/ngx_http_access_module.xml">ngx_http_access_module</link> and 4507 <link doc="../http/ngx_http_access_module.xml">ngx_http_access_module</link> and
4418 <link doc="../http/ngx_http_auth_basic_module.xml">ngx_http_auth_basic_module 4508 <link doc="../http/ngx_http_auth_basic_module.xml">ngx_http_auth_basic_module
4419 </link> register their handlers at this phase. 4509 </link> register their handlers at this phase.
4420 If configured so by the 4510 By default the client must pass the authorization check of all handlers
4421 <link doc="../http/ngx_http_core_module.xml" id="satisfy"/> directive, only one 4511 registered at this phase for the request to continue to the next phase.
4422 of access phase handlers may allow access to the request in order to continue 4512 The <link doc="../http/ngx_http_core_module.xml" id="satisfy"/> directive,
4423 processing 4513 can be used to permit processing to continue if any of the phase handlers
4424 </listitem> 4514 authorizes the client.
4425 4515 </listitem>
4426 <listitem> 4516
4427 <literal>NGX_HTTP_POST_ACCESS_PHASE</literal> — a special phase for the 4517 <listitem>
4518 <literal>NGX_HTTP_POST_ACCESS_PHASE</literal> — Special phase where the
4428 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link> 4519 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link>
4429 case. 4520 directive is processed.
4430 If some access phase handlers denied the access and none of them allowed, the 4521 If some access phase handlers denied access and none explictly allowed it, the
4431 request is finalized. 4522 request is finalized.
4432 No handlers are supported at this phase 4523 No additional handlers can be registered at this phase.
4433 </listitem> 4524 </listitem>
4434 4525
4435 <listitem> 4526 <listitem>
4436 <literal>NGX_HTTP_TRY_FILES_PHASE</literal> — a special phase, for the 4527 <literal>NGX_HTTP_TRY_FILES_PHASE</literal> — Special phase where
4437 <link doc="../http/ngx_http_core_module.xml" id="try_files"/> feature. 4528 the <link doc="../http/ngx_http_core_module.xml" id="try_files"/> directive
4438 No handlers are allowed at this phase 4529 is processed.
4439 </listitem> 4530 No additional handlers can be registered at this phase.
4440 4531 </listitem>
4441 <listitem> 4532
4442 <literal>NGX_HTTP_CONTENT_PHASE</literal> — a phase, at which the response 4533 <listitem>
4443 is supposed to be generated. 4534 <literal>NGX_HTTP_CONTENT_PHASE</literal> — Phase where the response
4444 Multiple nginx standard modules register their handers at this phase, for 4535 is normally generated.
4445 example 4536 Multiple nginx standard modules register their handlers at this phase,
4537 including
4446 <link doc="../http/ngx_http_index_module.xml">ngx_http_index_module</link> or 4538 <link doc="../http/ngx_http_index_module.xml">ngx_http_index_module</link> or
4447 <literal>ngx_http_static_module</literal>. 4539 <literal>ngx_http_static_module</literal>.
4448 All these handlers are called sequentially until one of them finally produces 4540 They are called sequentially until one of them produces
4449 the output. 4541 the output.
4450 It's also possible to set content handlers on a per-location basis. 4542 It's also possible to set content handlers on a per-location basis.
4451 If the 4543 If the
4452 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link>'s 4544 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link>'s
4453 location configuration has <literal>handler</literal> set, this handler is 4545 location configuration has <literal>handler</literal> set, it is
4454 called as the content handler and content phase handlers are ignored 4546 called as the content handler and the handlers installed at this phase
4455 </listitem> 4547 are ignored.
4456 4548 </listitem>
4457 <listitem> 4549
4458 <literal>NGX_HTTP_LOG_PHASE</literal> is used to perform request logging. 4550 <listitem>
4551 <literal>NGX_HTTP_LOG_PHASE</literal> — Phase where request logging
4552 is performed.
4459 Currently, only the 4553 Currently, only the
4460 <link doc="../http/ngx_http_log_module.xml">ngx_http_log_module</link> 4554 <link doc="../http/ngx_http_log_module.xml">ngx_http_log_module</link>
4461 registers its handler 4555 registers its handler
4462 at this stage for access logging. 4556 at this stage for access logging.
4463 Log phase handlers are called at the very end of request processing, right 4557 Log phase handlers are called at the very end of request processing, right
4464 before freeing the request 4558 before freeing the request.
4465 </listitem> 4559 </listitem>
4466 4560
4467 </list> 4561 </list>
4468 4562
4469 <para> 4563 <para>
4530 </para> 4624 </para>
4531 4625
4532 <list type="bullet"> 4626 <list type="bullet">
4533 4627
4534 <listitem> 4628 <listitem>
4535 <literal>NGX_OK</literal> — proceed to the next phase 4629 <literal>NGX_OK</literal> — Proceed to the next phase.
4536 </listitem> 4630 </listitem>
4537 4631
4538 <listitem> 4632 <listitem>
4539 <literal>NGX_DECLINED</literal> — proceed to the next handler of the current 4633 <literal>NGX_DECLINED</literal> — Proceed to the next handler of the current
4540 phase. 4634 phase.
4541 If current handler is the last in current phase, move to the next phase 4635 If the current handler is the last in the current phase,
4542 </listitem> 4636 move to the next phase.
4543 4637 </listitem>
4544 <listitem> 4638
4545 <literal>NGX_AGAIN, NGX_DONE</literal> — suspend phase handling until some 4639 <listitem>
4546 future event. 4640 <literal>NGX_AGAIN</literal>, <literal>NGX_DONE</literal> — Suspend
4547 This can be for example asynchronous I/O operation or just a delay. 4641 phase handling until some future event which can be
4548 It is supposed, that phase handling will be resumed later by calling 4642 an asynchronous I/O operation or just a delay, for example.
4549 <literal>ngx_http_core_run_phases()</literal> 4643 It is assumed, that phase handling will be resumed later by calling
4644 <literal>ngx_http_core_run_phases()</literal>.
4550 </listitem> 4645 </listitem>
4551 4646
4552 <listitem> 4647 <listitem>
4553 Any other value returned by the phase handler is treated as a request 4648 Any other value returned by the phase handler is treated as a request
4554 finalization code, in particular, HTTP response code. 4649 finalization code, in particular, an HTTP response code.
4555 The request is finalized with the code provided 4650 The request is finalized with the code provided.
4556 </listitem> 4651 </listitem>
4557 4652
4558 </list> 4653 </list>
4559 4654
4560 <para> 4655 <para>
4561 Some phases treat return codes in a slightly different way. 4656 For some phases, return codes are treated in a slightly different way.
4562 At content phase, any return code other that <literal>NGX_DECLINED</literal> 4657 At the content phase, any return code other that
4563 is considered a finalization code. 4658 <literal>NGX_DECLINED</literal> is considered a finalization code.
4564 As for the location content handlers, any return from them is considered a 4659 Any return code from the location content handlers is considered a
4565 finalization code. 4660 finalization code.
4566 At access phase, in 4661 At the access phase, in
4567 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link> 4662 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link>
4568 mode, returning a code other 4663 mode,
4569 than <literal>NGX_OK, NGX_DECLINED, NGX_AGAIN, NGX_DONE</literal> is considered 4664 any return code other than <literal>NGX_OK</literal>,
4570 a denial. 4665 <literal>NGX_DECLINED</literal>, <literal>NGX_AGAIN</literal>,
4571 If none of future access handlers allow access or deny with a new 4666 <literal>NGX_DONE</literal> is considered a denial.
4667 If no subsequent access handlers allow or deny access with a different
4572 code, the denial code will become the finalization code. 4668 code, the denial code will become the finalization code.
4573 </para> 4669 </para>
4574 4670
4575 </section> 4671 </section>
4576 4672
4578 <section name="Variables" id="http_variables"> 4674 <section name="Variables" id="http_variables">
4579 4675
4580 <section name="Accessing existing variables" id="http_existing_variables"> 4676 <section name="Accessing existing variables" id="http_existing_variables">
4581 4677
4582 <para> 4678 <para>
4583 Variables may be referenced using index (this is the most common method) 4679 Variables can be referenced by index (this is the most common method)
4584 or names (see below in the section about creating variables). 4680 or name (see <link id="http_creating_variables">below</link>).
4585 Index is created at configuration stage, when a variable is added 4681 The index is created at configuration stage, when a variable is added
4586 to configuration. 4682 to the configuration.
4587 The variable index can be obtained using 4683 To obtain the variable index, use
4588 <literal>ngx_http_get_variable_index()</literal>: 4684 <literal>ngx_http_get_variable_index()</literal>:
4589 <programlisting> 4685 <programlisting>
4590 ngx_str_t name; /* ngx_string("foo") */ 4686 ngx_str_t name; /* ngx_string("foo") */
4591 ngx_int_t index; 4687 ngx_int_t index;
4592 4688
4593 index = ngx_http_get_variable_index(cf, &amp;name); 4689 index = ngx_http_get_variable_index(cf, &amp;name);
4594 </programlisting> 4690 </programlisting>
4595 Here, the <literal>cf</literal> is a pointer to nginx configuration and the 4691 Here, <literal>cf</literal> is a pointer to nginx configuration and
4596 <literal>name</literal> points to a string with the variable name. 4692 <literal>name</literal> points to a string containing the variable name.
4597 The function returns <literal>NGX_ERROR</literal> on error or valid index 4693 The function returns <literal>NGX_ERROR</literal> on error or a valid index
4598 otherwise, which is typically stored somewhere in a module configuration for 4694 otherwise, which is typically stored somewhere in the module's
4599 future use. 4695 configuration for future use.
4600 </para> 4696 </para>
4601 4697
4602 <para> 4698 <para>
4603 All HTTP variables are evaluated in the context of HTTP request and results 4699 All HTTP variables are evaluated in the context of a given HTTP request,
4604 are specific to and cached in HTTP request. 4700 and results are specific to and cached in that HTTP request.
4605 All functions that evaluate variables return 4701 All functions that evaluate variables return the
4606 <literal>ngx_http_variable_value_t</literal> type, representing 4702 <literal>ngx_http_variable_value_t</literal> type, representing
4607 the variable value: 4703 the variable value:
4608 <programlisting> 4704 <programlisting>
4609 typedef ngx_variable_value_t ngx_http_variable_value_t; 4705 typedef ngx_variable_value_t ngx_http_variable_value_t;
4610 4706
4621 </programlisting> 4717 </programlisting>
4622 where: 4718 where:
4623 <list type="bullet"> 4719 <list type="bullet">
4624 4720
4625 <listitem> 4721 <listitem>
4626 <literal>len</literal> — length of a value 4722 <literal>len</literal> — The length of the value
4627 </listitem> 4723 </listitem>
4628 4724
4629 <listitem> 4725 <listitem>
4630 <literal>data</literal> — value itself 4726 <literal>data</literal> — The value itself
4631 </listitem> 4727 </listitem>
4632 4728
4633 <listitem> 4729 <listitem>
4634 <literal>valid</literal> — value is valid 4730 <literal>valid</literal> — The value is valid
4635 </listitem> 4731 </listitem>
4636 4732
4637 <listitem> 4733 <listitem>
4638 <literal>not_found</literal> — variable was not found and thus 4734 <literal>not_found</literal> — The variable was not found and thus
4639 the <literal>data</literal> and <literal>len</literal> fields are irrelevant; 4735 the <literal>data</literal> and <literal>len</literal> fields are irrelevant;
4640 this may happen, for example, with such variables as <var>$arg_foo</var> 4736 this can happen, for example, with variables like <var>$arg_foo</var>
4641 when a corresponding argument was not passed in a request 4737 when a corresponding argument was not passed in a request
4642 </listitem> 4738 </listitem>
4643 4739
4644 <listitem> 4740 <listitem>
4645 <literal>no_cacheable</literal> — do not cache result 4741 <literal>no_cacheable</literal> — Do not cache result
4646 </listitem> 4742 </listitem>
4647 4743
4648 <listitem> 4744 <listitem>
4649 <literal>escape</literal> — used internally by the logging module to mark 4745 <literal>escape</literal> — Used internally by the logging module to mark
4650 values that require escaping on output 4746 values that require escaping on output.
4651 </listitem> 4747 </listitem>
4652 4748
4653 </list> 4749 </list>
4654 </para> 4750 </para>
4655 4751
4656 <para> 4752 <para>
4657 The <literal>ngx_http_get_flushed_variable()</literal> 4753 The <literal>ngx_http_get_flushed_variable()</literal>
4658 and <literal>ngx_http_get_indexed_variable()</literal> functions 4754 and <literal>ngx_http_get_indexed_variable()</literal> functions
4659 are used to obtain the variable value. 4755 are used to obtain the value of a variable.
4660 They have the same interface - accepting a HTTP request <literal>r</literal> 4756 They have the same interface - accepting an HTTP request <literal>r</literal>
4661 as a context for evaluating the variable and an <literal>index</literal>, 4757 as a context for evaluating the variable and an <literal>index</literal>
4662 identifying it. 4758 that identifies it.
4663 Example of typical usage: 4759 An example of typical usage:
4664 <programlisting> 4760 <programlisting>
4665 ngx_http_variable_value_t *v; 4761 ngx_http_variable_value_t *v;
4666 4762
4667 v = ngx_http_get_flushed_variable(r, index); 4763 v = ngx_http_get_flushed_variable(r, index);
4668 4764
4672 } 4768 }
4673 4769
4674 /* some meaningful value is found */ 4770 /* some meaningful value is found */
4675 </programlisting> 4771 </programlisting>
4676 The difference between functions is that the 4772 The difference between functions is that the
4677 <literal>ngx_http_get_indexed_variable()</literal> returns cached value 4773 <literal>ngx_http_get_indexed_variable()</literal> returns a cached value
4678 and <literal>ngx_http_get_flushed_variable()</literal> flushes cache for 4774 and <literal>ngx_http_get_flushed_variable()</literal> flushes the cache for
4679 non-cacheable variables. 4775 non-cacheable variables.
4680 </para> 4776 </para>
4681 4777
4682 <para> 4778 <para>
4683 There are cases when it is required to deal with variables which names are 4779 Some modules, such as SSI and Perl, need to deal with variables for which the
4684 not known at configuration time and thus they cannot be accessed using indexes, 4780 name is not known at configuration time.
4685 for example in modules like SSI or Perl. 4781 An index therefore cannot be used to access them, but the
4686 The <literal>ngx_http_get_variable(r, name, key)</literal> function may be 4782 <literal>ngx_http_get_variable(r, name, key)</literal> function
4687 used in such cases. 4783 is available.
4688 It searches for the <literal>variable</literal> with a given 4784 It searches for a variable with a given
4689 <literal>name</literal> and its hash <literal>key</literal>. 4785 <literal>name</literal> and its hash <literal>key</literal> derived
4786 from the name.
4690 </para> 4787 </para>
4691 4788
4692 </section> 4789 </section>
4693 4790
4694 4791
4695 <section name="Creating variables" id="http_creating_variables"> 4792 <section name="Creating variables" id="http_creating_variables">
4696 4793
4697 <para> 4794 <para>
4698 To create a variable <literal>ngx_http_add_variable()</literal> function 4795 To create a variable, use the <literal>ngx_http_add_variable()</literal>
4699 is used. 4796 function.
4700 It takes configuration (where variable is registered), variable name and 4797 It takes as arguments a configuration (where the variable is registered),
4701 flags that control its behaviour: 4798 the variable name and flags that control the function's behaviour:
4702 4799
4703 <list type="bullet"> 4800 <list type="bullet">
4704 <listitem><literal>NGX_HTTP_VAR_CHANGEABLE</literal> — allows redefining 4801 <listitem><literal>NGX_HTTP_VAR_CHANGEABLE</literal> — Enables redefinition of
4705 the variable; If another module will define a variable with such name, 4802 the variable: there is no conflict if another module defines a variable with
4706 no conflict will happen. 4803 the same name.
4707 For example, this allows user to override variables using the 4804 This allows the
4708 <link doc="../http/ngx_http_rewrite_module.xml" id="set"/> directive. 4805 <link doc="../http/ngx_http_rewrite_module.xml" id="set"/> directive
4709 </listitem> 4806 to override variables.
4710 4807 </listitem>
4711 <listitem><literal>NGX_HTTP_VAR_NOCACHEABLE</literal> — disables caching, 4808
4712 is useful for such variables as <literal>$time_local</literal> 4809 <listitem><literal>NGX_HTTP_VAR_NOCACHEABLE</literal> — Disables caching,
4713 </listitem> 4810 which is useful for variables such as <literal>$time_local</literal>.
4714 4811 </listitem>
4715 <listitem><literal>NGX_HTTP_VAR_NOHASH</literal> — indicates that 4812
4813 <listitem><literal>NGX_HTTP_VAR_NOHASH</literal> — Indicates that
4716 this variable is only accessible by index, not by name. 4814 this variable is only accessible by index, not by name.
4717 This is a small optimization which may be used when it is known that the 4815 This is a small optimization for use when it is known that the
4718 variable is not needed in modules like SSI or Perl. 4816 variable is not needed in modules like SSI or Perl.
4719 </listitem> 4817 </listitem>
4720 4818
4721 <listitem><literal>NGX_HTTP_VAR_PREFIX</literal> — the name of this 4819 <listitem><literal>NGX_HTTP_VAR_PREFIX</literal> — The name of the
4722 variable is a prefix. 4820 variable is a prefix.
4723 A handler must implement additional logic to obtain value of specific 4821 In this case, a handler must implement additional logic to obtain the value
4724 variable. 4822 of a specific variable.
4725 For example, all “<literal>arg_</literal>” variables are processed by the 4823 For example, all “<literal>arg_</literal>” variables are processed by the
4726 same handler which performs lookup in request arguments and returns value 4824 same handler, which performs lookup in request arguments and returns the value
4727 of specific argument. 4825 of a specific argument.
4728 </listitem> 4826 </listitem>
4729 4827
4730 </list> 4828 </list>
4731 4829
4732 The function returns NULL in case of error or a pointer to 4830 The function returns NULL in case of error or a pointer to
4733 <literal>ngx_http_variable_t</literal>: 4831 <literal>ngx_http_variable_t</literal> otherwise:
4734 <programlisting> 4832 <programlisting>
4735 struct ngx_http_variable_s { 4833 struct ngx_http_variable_s {
4736 ngx_str_t name; 4834 ngx_str_t name;
4737 ngx_http_set_variable_pt set_handler; 4835 ngx_http_set_variable_pt set_handler;
4738 ngx_http_get_variable_pt get_handler; 4836 ngx_http_get_variable_pt get_handler;
4742 }; 4840 };
4743 </programlisting> 4841 </programlisting>
4744 4842
4745 The <literal>get</literal> and <literal>set</literal> handlers 4843 The <literal>get</literal> and <literal>set</literal> handlers
4746 are called to obtain or set the variable value, 4844 are called to obtain or set the variable value,
4747 <literal>data</literal> will be passed to variable handlers, 4845 <literal>data</literal> is passed to variable handlers, and
4748 <literal>index</literal> will hold assigned variable index, used to reference 4846 <literal>index</literal> holds assigned variable index used to reference
4749 the variable. 4847 the variable.
4750 </para> 4848 </para>
4751 4849
4752 <para> 4850 <para>
4753 Usually, a null-terminated static array of such structures is created 4851 Usually, a null-terminated static array of
4852 <literal>ngx_http_variable_t</literal> structures is created
4754 by a module and processed at the preconfiguration stage to add variables 4853 by a module and processed at the preconfiguration stage to add variables
4755 into configuration: 4854 into the configuration, for example:
4756 <programlisting> 4855 <programlisting>
4757 static ngx_http_variable_t ngx_http_foo_vars[] = { 4856 static ngx_http_variable_t ngx_http_foo_vars[] = {
4758 4857
4759 { ngx_string("foo_v1"), NULL, ngx_http_foo_v1_variable, NULL, 0, 0 }, 4858 { ngx_string("foo_v1"), NULL, ngx_http_foo_v1_variable, NULL, 0, 0 },
4760 4859
4777 } 4876 }
4778 4877
4779 return NGX_OK; 4878 return NGX_OK;
4780 } 4879 }
4781 </programlisting> 4880 </programlisting>
4782 This function is used to initialize the <literal>preconfiguration</literal> 4881 This function in the example is used to initialize
4783 field of the HTTP module context and is called before parsing HTTP configuration, 4882 the <literal>preconfiguration</literal>
4784 so it could refer to these variables. 4883 field of the HTTP module context and is called before the parsing of HTTP
4785 </para> 4884 configuration, so that the parser can refer to these variables.
4786 4885 </para>
4787 <para> 4886
4788 The <literal>get</literal> handler is responsible for evaluating the variable 4887 <para>
4789 in a context of specific request, for example: 4888 The <literal>get</literal> handler is responsible for evaluating a variable
4889 in the context of a specific request, for example:
4790 <programlisting> 4890 <programlisting>
4791 static ngx_int_t 4891 static ngx_int_t
4792 ngx_http_variable_connection(ngx_http_request_t *r, 4892 ngx_http_variable_connection(ngx_http_request_t *r,
4793 ngx_http_variable_value_t *v, uintptr_t data) 4893 ngx_http_variable_value_t *v, uintptr_t data)
4794 { 4894 {
4808 return NGX_OK; 4908 return NGX_OK;
4809 } 4909 }
4810 </programlisting> 4910 </programlisting>
4811 It returns <literal>NGX_ERROR</literal> in case of internal error 4911 It returns <literal>NGX_ERROR</literal> in case of internal error
4812 (for example, failed memory allocation) or <literal>NGX_OK</literal> otherwise. 4912 (for example, failed memory allocation) or <literal>NGX_OK</literal> otherwise.
4813 The status of variable evaluation may be understood by inspecting flags 4913 To learn the status of variable evaluation, inspect the flags
4814 of the <literal>ngx_http_variable_value_t</literal> (see description above). 4914 in <literal>ngx_http_variable_value_t</literal> (see the description
4915 <link id="http_existing_variables">above</link>).
4815 </para> 4916 </para>
4816 4917
4817 <para> 4918 <para>
4818 The <literal>set</literal> handler allows setting the property 4919 The <literal>set</literal> handler allows setting the property
4819 referred by the variable. 4920 referenced by the variable.
4820 For example, the <literal>$limit_rate</literal> variable set handler 4921 For example, the set handler of the <literal>$limit_rate</literal> variable
4821 modifies the request's <literal>limit_rate</literal> field: 4922 modifies the request's <literal>limit_rate</literal> field:
4822 <programlisting> 4923 <programlisting>
4823 ... 4924 ...
4824 { ngx_string("limit_rate"), ngx_http_variable_request_set_size, 4925 { ngx_string("limit_rate"), ngx_http_variable_request_set_size,
4825 ngx_http_variable_request_get_size, 4926 ngx_http_variable_request_get_size,
4862 4963
4863 <section name="Complex values" id="http_complex_values"> 4964 <section name="Complex values" id="http_complex_values">
4864 4965
4865 <para> 4966 <para>
4866 A complex value, despite its name, provides an easy way to evaluate 4967 A complex value, despite its name, provides an easy way to evaluate
4867 expressions that may contain text, variables, and their combination. 4968 expressions which can contain text, variables, and their combination.
4868 </para> 4969 </para>
4869 4970
4870 <para> 4971 <para>
4871 The complex value description in 4972 The complex value description in
4872 <literal>ngx_http_compile_complex_value</literal> is compiled at the 4973 <literal>ngx_http_compile_complex_value</literal> is compiled at the
4873 configuration stage into <literal>ngx_http_complex_value_t</literal> 4974 configuration stage into <literal>ngx_http_complex_value_t</literal>
4874 which is used at runtime to obtain evaluated expression results. 4975 which is used at runtime to obtain results of expression evaluation.
4875 4976
4876 <programlisting> 4977 <programlisting>
4877 ngx_str_t *value; 4978 ngx_str_t *value;
4878 ngx_http_complex_value_t cv; 4979 ngx_http_complex_value_t cv;
4879 ngx_http_compile_complex_value_t ccv; 4980 ngx_http_compile_complex_value_t ccv;
4897 initialize the complex value <literal>cv</literal>: 4998 initialize the complex value <literal>cv</literal>:
4898 4999
4899 <list type="bullet"> 5000 <list type="bullet">
4900 5001
4901 <listitem> 5002 <listitem>
4902 <literal>cf</literal> — configuration pointer 5003 <literal>cf</literal> — Configuration pointer
4903 </listitem> 5004 </listitem>
4904 5005
4905 <listitem> 5006 <listitem>
4906 <literal>value</literal> — string for parsing (input) 5007 <literal>value</literal> — String to be parsed (input)
4907 </listitem> 5008 </listitem>
4908 5009
4909 <listitem> 5010 <listitem>
4910 <literal>complex_value</literal> — compiled value (output) 5011 <literal>complex_value</literal> — Compiled value (output)
4911 </listitem> 5012 </listitem>
4912 5013
4913 <listitem> 5014 <listitem>
4914 <literal>zero</literal> — flag that enables zero-terminating value 5015 <literal>zero</literal> — Flag that enables zero-terminating value
4915 </listitem> 5016 </listitem>
4916 5017
4917 <listitem> 5018 <listitem>
4918 <literal>conf_prefix</literal> — prefixes result with configuration prefix 5019 <literal>conf_prefix</literal> — Prefixes the result with the
4919 (the directory where nginx is currently looking for configuration) 5020 configuration prefix (the directory where nginx is currently looking for
4920 </listitem> 5021 configuration)
4921 5022 </listitem>
4922 <listitem> 5023
4923 <literal>root_prefix</literal> — prefixes result with root prefix 5024 <listitem>
4924 (this is the normal nginx installation prefix) 5025 <literal>root_prefix</literal> — Prefixes the result with the root prefix
4925 </listitem> 5026 (the normal nginx installation prefix)
4926 5027 </listitem>
4927 </list> 5028
4928 The <literal>zero</literal> flag is usable when results are to be passed to 5029 </list>
5030 The <literal>zero</literal> flag is useful when results are to be passed to
4929 libraries that require zero-terminated strings, and prefixes are handy when 5031 libraries that require zero-terminated strings, and prefixes are handy when
4930 dealing with filenames. 5032 dealing with filenames.
4931 </para> 5033 </para>
4932 5034
4933 <para> 5035 <para>
4934 Upon successful compilation, <literal>cv.lengths</literal> may 5036 Upon successful compilation, <literal>cv.lengths</literal>
4935 be inspected to get information about the presence of variables 5037 contains information about the presence of variables
4936 in the expression. 5038 in the expression.
4937 The NULL value means that the expression contained static text only, 5039 The NULL value means that the expression contained static text only,
4938 and there is no need in storing it as a complex value, 5040 and so can be stored in a simple string rather than as a complex value.
4939 so a simple string can be used.
4940 </para> 5041 </para>
4941 5042
4942 <para> 5043 <para>
4943 The <literal>ngx_http_set_complex_value_slot()</literal> is a convenient 5044 The <literal>ngx_http_set_complex_value_slot()</literal> is a convenient
4944 function used to initialize complex value completely right in the directive 5045 function used to initialize a complex value completely in the directive
4945 declaration. 5046 declaration itself.
4946 </para> 5047 </para>
4947 5048
4948 <para> 5049 <para>
4949 At runtime, a complex value may be calculated using the 5050 At runtime, a complex value can be calculated using the
4950 <literal>ngx_http_complex_value()</literal> function: 5051 <literal>ngx_http_complex_value()</literal> function:
4951 <programlisting> 5052 <programlisting>
4952 ngx_str_t res; 5053 ngx_str_t res;
4953 5054
4954 if (ngx_http_complex_value(r, &amp;cv, &amp;res) != NGX_OK) { 5055 if (ngx_http_complex_value(r, &amp;cv, &amp;res) != NGX_OK) {
4955 return NGX_ERROR; 5056 return NGX_ERROR;
4956 } 5057 }
4957 </programlisting> 5058 </programlisting>
4958 Given the request <literal>r</literal> and previously compiled 5059 Given the request <literal>r</literal> and previously compiled
4959 value <literal>cv</literal> the function will evaluate 5060 value <literal>cv</literal>, the function evaluates the
4960 expression and put result into <literal>res</literal>. 5061 expression and writes the result to <literal>res</literal>.
4961 </para> 5062 </para>
4962 5063
4963 </section> 5064 </section>
4964 5065
4965 5066
4970 <literal>loc_conf</literal> field of the <literal>ngx_http_request_t</literal> 5071 <literal>loc_conf</literal> field of the <literal>ngx_http_request_t</literal>
4971 structure. 5072 structure.
4972 This means that at any point the location configuration of any module can be 5073 This means that at any point the location configuration of any module can be
4973 retrieved from the request by calling 5074 retrieved from the request by calling
4974 <literal>ngx_http_get_module_loc_conf(r, module)</literal>. 5075 <literal>ngx_http_get_module_loc_conf(r, module)</literal>.
4975 Request location may be changed several times throughout its lifetime. 5076 Request location can change several times during the request's lifetime.
4976 Initially, a default server location of the default server is assigned to a 5077 Initially, a default server location of the default server is assigned to a
4977 request. 5078 request.
4978 Once a request switches to a different server (chosen by the HTTP 5079 If the request switches to a different server (chosen by the HTTP
4979 <header>Host</header> header or SSL SNI extension), the request switches to the 5080 <header>Host</header> header or SSL SNI extension), the request switches to the
4980 default location of that server as well. 5081 default location of that server as well.
4981 The next change of the location takes place at the 5082 The next change of the location takes place at the
4982 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> request phase. 5083 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> request phase.
4983 At this phase a location is chosen by request URI among all non-named locations 5084 At this phase a location is chosen by request URI among all non-named locations
4984 configured for the server. 5085 configured for the server.
4985 The 5086 The
4986 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link> 5087 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link>
4987 may change the request URI at the 5088 can change the request URI at the
4988 <literal>NGX_HTTP_REWRITE_PHASE</literal> request phase as a result of 5089 <literal>NGX_HTTP_REWRITE_PHASE</literal> request phase as a result of
4989 <link doc="../http/ngx_http_rewrite_module.xml" id="rewrite">rewrite</link> and 5090 the <link doc="../http/ngx_http_rewrite_module.xml" id="rewrite">rewrite</link>
4990 return to the <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> phase for choosing a 5091 directive and send the request back
5092 to the <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> phase for selection of a
4991 new location based on the new URI. 5093 new location based on the new URI.
4992 </para> 5094 </para>
4993 5095
4994 <para> 5096 <para>
4995 It is also possible to redirect a request to a new location at any point by 5097 It is also possible to redirect a request to a new location at any point by
4996 calling one of the functions 5098 calling one of
4997 <literal>ngx_http_internal_redirect(r, uri, args)</literal> or 5099 <literal>ngx_http_internal_redirect(r, uri, args)</literal> or
4998 <literal>ngx_http_named_location(r, name)</literal>. 5100 <literal>ngx_http_named_location(r, name)</literal>.
4999 </para> 5101 </para>
5000 5102
5001 <para> 5103 <para>
5002 The function <literal>ngx_http_internal_redirect(r, uri, args)</literal> changes 5104 The <literal>ngx_http_internal_redirect(r, uri, args)</literal> function changes
5003 the request URI and returns the request to the 5105 the request URI and returns the request to the
5004 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> phase. 5106 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> phase.
5005 The request proceeds with a server default location. 5107 The request proceeds with a server default location.
5006 Later at <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> a new location is chosen 5108 Later at <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> a new location is chosen
5007 based on the new request URI. 5109 based on the new request URI.
5049 return ngx_http_named_location(r, &amp;name); 5151 return ngx_http_named_location(r, &amp;name);
5050 } 5152 }
5051 </programlisting> 5153 </programlisting>
5052 5154
5053 <para> 5155 <para>
5054 Both functions <literal>ngx_http_internal_redirect(r, uri, args)</literal> 5156 Both functions - <literal>ngx_http_internal_redirect(r, uri, args)</literal>
5055 and <literal>ngx_http_named_location(r, name)</literal> may be called when 5157 and <literal>ngx_http_named_location(r, name)</literal> can be called when
5056 a request already has some contexts saved in its <literal>ctx</literal> field 5158 nginx modules have already stored some contexts in a request's
5057 by nginx modules. 5159 <literal>ctx</literal> field.
5058 These contexts could become inconsistent with the new 5160 It's possible for these contexts to become inconsistent with the new
5059 location configuration. 5161 location configuration.
5060 To prevent inconsistency, all request contexts are 5162 To prevent inconsistency, all request contexts are
5061 erased by both redirect functions. 5163 erased by both redirect functions.
5062 </para> 5164 </para>
5063 5165
5064 <para> 5166 <para>
5065 Redirected and rewritten requests become internal and may access the 5167 Calling <literal>ngx_http_internal_redirect(r, uri, args)</literal>
5168 or <literal>ngx_http_named_location(r, name)</literal> increases the request
5169 <literal>count</literal>.
5170 For consistent request reference counting, call
5171 <literal>ngx_http_finalize_request(r, NGX_DONE)</literal> after redirecting the
5172 request.
5173 This will finalize current request code path and decrease the counter.
5174 </para>
5175
5176 <para>
5177 Redirected and rewritten requests become internal and can access the
5066 <link doc="../http/ngx_http_core_module.xml" id="internal">internal</link> 5178 <link doc="../http/ngx_http_core_module.xml" id="internal">internal</link>
5067 locations. 5179 locations.
5068 Internal requests have the <literal>internal</literal> flag set. 5180 Internal requests have the <literal>internal</literal> flag set.
5069 </para> 5181 </para>
5070 5182
5072 5184
5073 5185
5074 <section name="Subrequests" id="http_subrequests"> 5186 <section name="Subrequests" id="http_subrequests">
5075 5187
5076 <para> 5188 <para>
5077 Subrequests are primarily used to include output of one request into another, 5189 Subrequests are primarily used to insert output of one request into another,
5078 possibly mixed with other data. 5190 possibly mixed with other data.
5079 A subrequest looks like a normal request, but shares some data with its parent. 5191 A subrequest looks like a normal request, but shares some data with its parent.
5080 Particularly, all fields related to client input are shared since a subrequest 5192 In particular, all fields related to client input are shared
5081 does not receive any other input from client. 5193 because a subrequest does not receive any other input from the client.
5082 The request field <literal>parent</literal> for a subrequest keeps a link to its 5194 The request field <literal>parent</literal> for a subrequest contains a link
5083 parent request and is NULL for the main request. 5195 to its parent request and is NULL for the main request.
5084 The field <literal>main</literal> keeps a link to the main request in a group of 5196 The field <literal>main</literal> contains a link to the main request in
5085 requests. 5197 a group of requests.
5086 </para> 5198 </para>
5087 5199
5088 <para> 5200 <para>
5089 A subrequest starts with <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> phase. 5201 A subrequest starts in the <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal>
5090 It passes through the same phases as a normal request and is assigned a location 5202 phase.
5091 based on its own URI. 5203 It passes through the same subsequent phases as a normal request and is
5092 </para> 5204 assigned a location based on its own URI.
5093 5205 </para>
5094 <para> 5206
5095 Subrequest output header is always ignored. 5207 <para>
5096 Subrequest output body is placed by the 5208 The output header in a subrequest is always ignored.
5097 <literal>ngx_http_postpone_filter</literal> into the right position in relation 5209 The <literal>ngx_http_postpone_filter</literal> places the subrequest's
5098 to other data produced by the parent request. 5210 output body in the right position relative to other data produced
5211 by the parent request.
5099 </para> 5212 </para>
5100 5213
5101 <para> 5214 <para>
5102 Subrequests are related to the concept of active requests. 5215 Subrequests are related to the concept of active requests.
5103 A request <literal>r</literal> is considered active if 5216 A request <literal>r</literal> is considered active if
5104 <literal>c->data == r</literal>, where <literal>c</literal> is the client 5217 <literal>c->data == r</literal>, where <literal>c</literal> is the client
5105 connection object. 5218 connection object.
5106 At any point, only the active request in a request group is allowed to output 5219 At any given point, only the active request in a request group is allowed
5107 its buffers to the client. 5220 to output its buffers to the client.
5108 A non-active request can still send its data to the filter chain, but they 5221 An inactive request can still send its output to the filter chain, but it
5109 will not pass beyond the <literal>ngx_http_postpone_filter</literal> and will 5222 does not pass beyond the <literal>ngx_http_postpone_filter</literal> and
5110 remain buffered by that filter until the request becomes active. 5223 remains buffered by that filter until the request becomes active.
5111 Here are some rules of request activation: 5224 Here are some rules of request activation:
5112 </para> 5225 </para>
5113 5226
5114 <list type="bullet"> 5227 <list type="bullet">
5115 5228
5116 <listitem> 5229 <listitem>
5117 Initially, the main request is active 5230 Initially, the main request is active.
5118 </listitem> 5231 </listitem>
5119 5232
5120 <listitem> 5233 <listitem>
5121 The first subrequest of an active request becomes active right after creation 5234 The first subrequest of an active request becomes active right after creation.
5122 </listitem> 5235 </listitem>
5123 5236
5124 <listitem> 5237 <listitem>
5125 The <literal>ngx_http_postpone_filter</literal> activates the next request 5238 The <literal>ngx_http_postpone_filter</literal> activates the next request
5126 in active request's subrequest list, once all data prior to that request are 5239 in the active request's subrequest list, once all data prior to that request
5127 sent 5240 are sent.
5128 </listitem> 5241 </listitem>
5129 5242
5130 <listitem> 5243 <listitem>
5131 When a request is finalized, its parent is activated 5244 When a request is finalized, its parent is activated.
5132 </listitem> 5245 </listitem>
5133 5246
5134 </list> 5247 </list>
5135 5248
5136 <para> 5249 <para>
5137 A subrequest is created by calling the function 5250 Create a subrequest by calling the function
5138 <literal>ngx_http_subrequest(r, uri, args, psr, ps, flags)</literal>, where 5251 <literal>ngx_http_subrequest(r, uri, args, psr, ps, flags)</literal>, where
5139 <literal>r</literal> is the parent request, <literal>uri</literal> and 5252 <literal>r</literal> is the parent request, <literal>uri</literal> and
5140 <literal>args</literal> are URI and arguments of the 5253 <literal>args</literal> are the URI and arguments of the
5141 subrequest, <literal>psr</literal> is the output parameter, receiving the 5254 subrequest, <literal>psr</literal> is the output parameter, which receives the
5142 newly created subrequest reference, <literal>ps</literal> is a callback object 5255 newly created subrequest reference, <literal>ps</literal> is a callback object
5143 for notifying the parent request that the subrequest is being finalized, 5256 for notifying the parent request that the subrequest is being finalized, and
5144 <literal>flags</literal> is subrequest creation flags bitmask. 5257 <literal>flags</literal> is bitmask of flags.
5145 The following flags are available: 5258 The following flags are available:
5146 </para> 5259 </para>
5147 5260
5148 <list type="bullet"> 5261 <list type="bullet">
5149 5262
5150 <listitem> 5263 <listitem>
5151 <literal>NGX_HTTP_SUBREQUEST_IN_MEMORY</literal> - subrequest output should not 5264 <literal>NGX_HTTP_SUBREQUEST_IN_MEMORY</literal> - Output is not
5152 be sent to the client, but rather stored in memory. 5265 sent to the client, but rather stored in memory.
5153 This only works for proxying subrequests. 5266 The flag only affects subrequests which are processed by one of the proxying
5154 After subrequest finalization its output is available in 5267 modules.
5155 <literal>r->upstream->buffer</literal> buffer of type 5268 After a subrequest is finalized its output is available in
5156 <literal>ngx_buf_t</literal> 5269 a <literal>r->upstream->buffer</literal> of type <literal>ngx_buf_t</literal>.
5157 </listitem> 5270 </listitem>
5158 5271
5159 <listitem> 5272 <listitem>
5160 <literal>NGX_HTTP_SUBREQUEST_WAITED</literal> - the subrequest 5273 <literal>NGX_HTTP_SUBREQUEST_WAITED</literal> - The subrequest's
5161 <literal>done</literal> flag is set even if it is finalized being non-active. 5274 <literal>done</literal> flag is set even if the subrequest is not active when
5162 This subrequest flag is used by the SSI filter 5275 it is finalized.
5163 </listitem> 5276 This subrequest flag is used by the SSI filter.
5164 5277 </listitem>
5165 <listitem> 5278
5166 <literal>NGX_HTTP_SUBREQUEST_CLONE</literal> - the subrequest is created as a 5279 <listitem>
5280 <literal>NGX_HTTP_SUBREQUEST_CLONE</literal> - The subrequest is created as a
5167 clone of its parent. 5281 clone of its parent.
5168 It is started at the same location and proceeds from the same phase as the 5282 It is started at the same location and proceeds from the same phase as the
5169 parent request 5283 parent request.
5170 </listitem> 5284 </listitem>
5171 5285
5172 </list> 5286 </list>
5173 5287
5174 <para> 5288 <para>
5175 The following example creates a subrequest with the URI of "/foo". 5289 The following example creates a subrequest with the URI
5290 of <literal>/foo</literal>.
5176 </para> 5291 </para>
5177 5292
5178 <programlisting> 5293 <programlisting>
5179 ngx_int_t rc; 5294 ngx_int_t rc;
5180 ngx_str_t uri; 5295 ngx_str_t uri;
5226 return rc; 5341 return rc;
5227 } 5342 }
5228 </programlisting> 5343 </programlisting>
5229 5344
5230 <para> 5345 <para>
5231 Subrequests are normally created in a body filter. 5346 Subrequests are normally created in a body filter, in which case their output
5232 In this case subrequest output can be treated as any other explicit request 5347 can be treated like the output from any explicit request.
5233 output. 5348 This means that eventually the output of a subrequest is sent to the client,
5234 This means that eventually the output of a subrequest is sent to the client 5349 after all explicit buffers that are passed before subrequest creation and
5235 after all explicit buffers passed prior to subrequest creation and before any 5350 before any buffers that are passed after creation.
5236 buffers passed later.
5237 This ordering is preserved even for large hierarchies of subrequests. 5351 This ordering is preserved even for large hierarchies of subrequests.
5238 The following example inserts a subrequest output after all request data 5352 The following example inserts output from a subrequest after all request data
5239 buffers, but before the final buffer with the <literal>last_buf</literal> flag. 5353 buffers, but before the final buffer with the <literal>last_buf</literal> flag.
5240 </para> 5354 </para>
5241 5355
5242 <programlisting> 5356 <programlisting>
5243 ngx_int_t 5357 ngx_int_t
5284 return NGX_ERROR; 5398 return NGX_ERROR;
5285 } 5399 }
5286 5400
5287 ngx_http_set_ctx(r, NULL, ngx_http_foo_filter_module); 5401 ngx_http_set_ctx(r, NULL, ngx_http_foo_filter_module);
5288 5402
5289
5290 /* Output the final buffer with the last_buf flag */ 5403 /* Output the final buffer with the last_buf flag */
5291 5404
5292 b = ngx_calloc_buf(r->pool); 5405 b = ngx_calloc_buf(r->pool);
5293 if (b == NULL) { 5406 if (b == NULL) {
5294 return NGX_ERROR; 5407 return NGX_ERROR;
5302 return ngx_http_output_filter(r, &amp;out); 5415 return ngx_http_output_filter(r, &amp;out);
5303 } 5416 }
5304 </programlisting> 5417 </programlisting>
5305 5418
5306 <para> 5419 <para>
5307 A subrequest may also be created for other purposes than data output. 5420 A subrequest can also be created for other purposes than data output.
5308 For example, the <link doc="../http/ngx_http_auth_request_module.xml"> 5421 For example, the <link doc="../http/ngx_http_auth_request_module.xml">
5309 ngx_http_auth_request_module</link> 5422 ngx_http_auth_request_module</link> module
5310 creates a subrequest at <literal>NGX_HTTP_ACCESS_PHASE</literal> phase. 5423 creates a subrequest at the <literal>NGX_HTTP_ACCESS_PHASE</literal> phase.
5311 To disable any output at this point, the subrequest 5424 To disable output at this point, the
5312 <literal>header_only</literal> flag is set. 5425 <literal>header_only</literal> flag is set on the subrequest.
5313 This prevents subrequest body from being sent to the client. 5426 This prevents the subrequest body from being sent to the client.
5314 Its header is ignored anyway. 5427 Note that the subrequest's header is never sent to the client.
5315 The result of the subrequest can be analyzed in the callback handler. 5428 The result of the subrequest can be analyzed in the callback handler.
5316 </para> 5429 </para>
5317 5430
5318 </section> 5431 </section>
5319 5432
5321 <section name="Request finalization" id="http_request_finalization"> 5434 <section name="Request finalization" id="http_request_finalization">
5322 5435
5323 <para> 5436 <para>
5324 An HTTP request is finalized by calling the function 5437 An HTTP request is finalized by calling the function
5325 <literal>ngx_http_finalize_request(r, rc)</literal>. 5438 <literal>ngx_http_finalize_request(r, rc)</literal>.
5326 It is usually finalized by the content handler after sending all output buffers 5439 It is usually finalized by the content handler after all output buffers
5327 to the filter chain. 5440 are sent to the filter chain.
5328 At this point the output may not be completely sent to the client, but remain 5441 At this point all of the output might not be sent to the client,
5329 buffered somewhere along the filter chain. 5442 with some of it remaining buffered somewhere along the filter chain.
5330 If it is, the <literal>ngx_http_finalize_request(r, rc)</literal> function will 5443 If it is, the <literal>ngx_http_finalize_request(r, rc)</literal> function
5331 automatically install a special handler <literal>ngx_http_writer(r)</literal> 5444 automatically installs a special handler <literal>ngx_http_writer(r)</literal>
5332 to finish sending the output. 5445 to finish sending the output.
5333 A request is also finalized in case of an error or if a standard HTTP response 5446 A request is also finalized in case of an error or if a standard HTTP response
5334 code needs to be returned to the client. 5447 code needs to be returned to the client.
5335 </para> 5448 </para>
5336 5449
5340 </para> 5453 </para>
5341 5454
5342 <list type="bullet"> 5455 <list type="bullet">
5343 5456
5344 <listitem> 5457 <listitem>
5345 <literal>NGX_DONE</literal> - fast finalization. 5458 <literal>NGX_DONE</literal> - Fast finalization.
5346 Decrement request <literal>count</literal> and destroy the request if it 5459 Decrement the request <literal>count</literal> and destroy the request if it
5347 reaches zero. 5460 reaches zero.
5348 The client connection may still be used for more requests after that 5461 The client connection can be used for more requests after the current request
5462 is destroyed.
5349 </listitem> 5463 </listitem>
5350 5464
5351 <listitem> 5465 <listitem>
5352 <literal>NGX_ERROR</literal>, <literal>NGX_HTTP_REQUEST_TIME_OUT</literal> 5466 <literal>NGX_ERROR</literal>, <literal>NGX_HTTP_REQUEST_TIME_OUT</literal>
5353 (408), <literal>NGX_HTTP_CLIENT_CLOSED_REQUEST</literal> (499) - error 5467 (<literal>408</literal>), <literal>NGX_HTTP_CLIENT_CLOSED_REQUEST</literal>
5354 finalization. 5468 (<literal>499</literal>) - Error finalization.
5355 Terminate the request as soon as possible and close the client connection. 5469 Terminate the request as soon as possible and close the client connection.
5356 </listitem> 5470 </listitem>
5357 5471
5358 <listitem> 5472 <listitem>
5359 <literal>NGX_HTTP_CREATED</literal> (201), 5473 <literal>NGX_HTTP_CREATED</literal> (<literal>201</literal>),
5360 <literal>NGX_HTTP_NO_CONTENT</literal> (204), codes greater than or equal to 5474 <literal>NGX_HTTP_NO_CONTENT</literal> (<literal>204</literal>), codes greater
5361 <literal>NGX_HTTP_SPECIAL_RESPONSE</literal> (300) - special response 5475 than or equal to <literal>NGX_HTTP_SPECIAL_RESPONSE</literal>
5362 finalization. 5476 (<literal>300</literal>) - Special response finalization.
5363 For these values nginx either sends a default code response page to the client 5477 For these values nginx either sends to the client a default response page for
5364 or performs the internal redirect to an 5478 the code or performs the internal redirect to an
5365 <link doc="../http/ngx_http_core_module.xml" id="error_page"/> location if it's 5479 <link doc="../http/ngx_http_core_module.xml" id="error_page"/> location if that
5366 configured for the code 5480 is configured for the code.
5367 </listitem> 5481 </listitem>
5368 5482
5369 <listitem> 5483 <listitem>
5370 Other codes are considered success finalization codes and may activate the 5484 Other codes are considered successful finalization codes and might activate the
5371 request writer to finish sending the response body. 5485 request writer to finish sending the response body.
5372 Once body is completely sent, request <literal>count</literal> is decremented. 5486 Once the body is completely sent, the request <literal>count</literal>
5373 If it reaches zero, the request is destroyed, but the client connection may 5487 is decremented.
5488 If it reaches zero, the request is destroyed, but the client connection can
5374 still be used for other requests. 5489 still be used for other requests.
5375 If <literal>count</literal> is positive, there are unfinished activities 5490 If <literal>count</literal> is positive, there are unfinished activities
5376 within the request, which will be finalized at a later point. 5491 within the request, which will be finalized at a later point.
5377 </listitem> 5492 </listitem>
5378 5493
5382 5497
5383 5498
5384 <section name="Request body" id="http_request_body"> 5499 <section name="Request body" id="http_request_body">
5385 5500
5386 <para> 5501 <para>
5387 For dealing with client request body, nginx provides the following functions: 5502 For dealing with the body of a client request, nginx provides the
5388 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> and 5503 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> and
5389 <literal>ngx_http_discard_request_body(r)</literal>. 5504 <literal>ngx_http_discard_request_body(r)</literal> functions.
5390 The first function reads the request body and makes it available via the 5505 The first function reads the request body and makes it available via the
5391 <literal>request_body</literal> request field. 5506 <literal>request_body</literal> request field.
5392 The second function instructs nginx to discard (read and ignore) the request 5507 The second function instructs nginx to discard (read and ignore) the request
5393 body. 5508 body.
5394 One of these functions must be called for every request. 5509 One of these functions must be called for every request.
5395 Normally, it is done in the content handler. 5510 Normally, the content handler makes the call.
5396 </para> 5511 </para>
5397 5512
5398 <para> 5513 <para>
5399 Reading or discarding client request body from a subrequest is not allowed. 5514 Reading or discarding the client request body from a subrequest is not allowed.
5400 It should always be done in the main request. 5515 It must always be done in the main request.
5401 When a subrequest is created, it inherits the parent 5516 When a subrequest is created, it inherits the parent's
5402 <literal>request_body</literal> object which can be used by the subrequest if 5517 <literal>request_body</literal> object which can be used by the subrequest if
5403 the main request has previously read the request body. 5518 the main request has previously read the request body.
5404 </para> 5519 </para>
5405 5520
5406 <para> 5521 <para>
5407 The function 5522 The function
5408 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> starts 5523 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> starts
5409 the process of reading the request body. 5524 the process of reading the request body.
5410 When the body is completely read, the <literal>post_handler</literal> callback 5525 When the body is completely read, the <literal>post_handler</literal> callback
5411 is called to continue processing the request. 5526 is called to continue processing the request.
5412 If request body is missing or already read, the callback is called immediately. 5527 If the request body is missing or has already been read, the callback is called
5528 immediately.
5413 The function 5529 The function
5414 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> 5530 <literal>ngx_http_read_client_request_body(r, post_handler)</literal>
5415 allocates the <literal>request_body</literal> request field of type 5531 allocates the <literal>request_body</literal> request field of type
5416 <literal>ngx_http_request_body_t</literal>. 5532 <literal>ngx_http_request_body_t</literal>.
5417 The field <literal>bufs</literal> of this object keeps the result as a buffer 5533 The field <literal>bufs</literal> of this object keeps the result as a buffer
5418 chain. 5534 chain.
5419 The body can be saved in memory buffers or file buffers, if 5535 The body can be saved in memory buffers or file buffers, if the capacity
5536 specified by the
5420 <link doc="../http/ngx_http_core_module.xml" id="client_body_buffer_size"/> 5537 <link doc="../http/ngx_http_core_module.xml" id="client_body_buffer_size"/>
5421 is not enough to fit the entire body in memory. 5538 directive is not enough to fit the entire body in memory.
5422 </para> 5539 </para>
5423 5540
5424 <para> 5541 <para>
5425 The following example reads client request body and returns its size. 5542 The following example reads a client request body and returns its size.
5426 </para> 5543 </para>
5427 5544
5428 <programlisting> 5545 <programlisting>
5429 ngx_int_t 5546 ngx_int_t
5430 ngx_http_foo_content_handler(ngx_http_request_t *r) 5547 ngx_http_foo_content_handler(ngx_http_request_t *r)
5489 ngx_http_finalize_request(r, rc); 5606 ngx_http_finalize_request(r, rc);
5490 } 5607 }
5491 </programlisting> 5608 </programlisting>
5492 5609
5493 <para> 5610 <para>
5494 The following fields of the request affect the way request body is read: 5611 The following fields of the request determine how the request body is read:
5495 </para> 5612 </para>
5496 5613
5497 <list type="bullet"> 5614 <list type="bullet">
5498 5615
5499 <listitem> 5616 <listitem>
5500 <literal>request_body_in_single_buf</literal> - read body to a single memory 5617 <literal>request_body_in_single_buf</literal> - Read the body to a single memory
5501 buffer 5618 buffer.
5502 </listitem> 5619 </listitem>
5503 5620
5504 <listitem> 5621 <listitem>
5505 <literal>request_body_in_file_only</literal> - always read body to a file, 5622 <literal>request_body_in_file_only</literal> - Always read the body to a file,
5506 even if fits the memory buffer 5623 even if fits in the memory buffer.
5507 </listitem> 5624 </listitem>
5508 5625
5509 <listitem> 5626 <listitem>
5510 <literal>request_body_in_persistent_file</literal> - do not unlink the file 5627 <literal>request_body_in_persistent_file</literal> - Do not unlink the file
5511 right after creation. 5628 immediately after creation.
5512 Such a file can be moved to another directory 5629 A file with this flag can be moved to another directory.
5513 </listitem> 5630 </listitem>
5514 5631
5515 <listitem> 5632 <listitem>
5516 <literal>request_body_in_clean_file</literal> - unlink the file the when the 5633 <literal>request_body_in_clean_file</literal> - Unlink the file when the
5517 request is finalized. 5634 request is finalized.
5518 This can be useful when a file was supposed to be moved to another directory 5635 This can be useful when a file was supposed to be moved to another directory
5519 but eventually was not moved for some reason 5636 but was not moved for some reason.
5520 </listitem> 5637 </listitem>
5521 5638
5522 <listitem> 5639 <listitem>
5523 <literal>request_body_file_group_access</literal> - enable file group access. 5640 <literal>request_body_file_group_access</literal> - Enable group access to the
5524 By default a file is created with 0600 access mask. 5641 file by replacing the default 0600 access mask with 0660.
5525 When the flag is set, 0660 access mask is used 5642 </listitem>
5526 </listitem> 5643
5527 5644 <listitem>
5528 <listitem> 5645 <literal>request_body_file_log_level</literal> - Severity level at which to
5529 <literal>request_body_file_log_level</literal> - log file errors with this 5646 log file errors.
5530 log level 5647 </listitem>
5531 </listitem> 5648
5532 5649 <listitem>
5533 <listitem> 5650 <literal>request_body_no_buffering</literal> - Read the request body without
5534 <literal>request_body_no_buffering</literal> - read request body without 5651 buffering.
5535 buffering 5652 </listitem>
5536 </listitem> 5653
5537 5654 </list>
5538 </list> 5655
5539 5656 <para>
5540 <para> 5657 The <literal>request_body_no_buffering</literal> flag enables the
5541 When the <literal>request_body_no_buffering</literal> flag is set, the 5658 unbuffered mode of reading a request body.
5542 unbuffered mode of reading the request body is enabled.
5543 In this mode, after calling 5659 In this mode, after calling
5544 <literal>ngx_http_read_client_request_body()</literal>, the 5660 <literal>ngx_http_read_client_request_body()</literal>, the
5545 <literal>bufs</literal> chain may keep only a part of the body. 5661 <literal>bufs</literal> chain might keep only a part of the body.
5546 To read the next part, the 5662 To read the next part, call the
5547 <literal>ngx_http_read_unbuffered_request_body(r)</literal> function should be 5663 <literal>ngx_http_read_unbuffered_request_body(r)</literal> function.
5548 called. 5664 The return value <literal>NGX_AGAIN</literal> and the request flag
5549 The return value of <literal>NGX_AGAIN</literal> and the request flag
5550 <literal>reading_body</literal> indicate that more data is available. 5665 <literal>reading_body</literal> indicate that more data is available.
5551 If <literal>bufs</literal> is NULL after calling this function, there is 5666 If <literal>bufs</literal> is NULL after calling this function, there is
5552 nothing to read at the moment. 5667 nothing to read at the moment.
5553 The request callback <literal>read_event_handler</literal> will be called when 5668 The request callback <literal>read_event_handler</literal> will be called when
5554 the next part of request body is available. 5669 the next part of request body is available.
5558 5673
5559 5674
5560 <section name="Response" id="http_response"> 5675 <section name="Response" id="http_response">
5561 5676
5562 <para> 5677 <para>
5563 An HTTP response in nginx is produced by sending the response header followed by 5678 In nginx an HTTP response is produced by sending the response header followed by
5564 the optional response body. 5679 the optional response body.
5565 Both header and body are passed through a chain of filters and eventually get 5680 Both header and body are passed through a chain of filters and eventually get
5566 written to the client socket. 5681 written to the client socket.
5567 An nginx module can install its handler into the header or body filter chain 5682 An nginx module can install its handler into the header or body filter chain
5568 and process the output coming from the previous handler. 5683 and process the output coming from the previous handler.
5570 5685
5571 5686
5572 <section name="Response header" id="http_response_header"> 5687 <section name="Response header" id="http_response_header">
5573 5688
5574 <para> 5689 <para>
5575 Output header is sent by the function 5690 The <literal>ngx_http_send_header(r)</literal>
5576 <literal>ngx_http_send_header(r)</literal>. 5691 function sends the output header.
5577 Prior to calling this function, <literal>r->headers_out</literal> should contain 5692 Do not call this function until <literal>r->headers_out</literal>
5578 all the data required to produce the HTTP response header. 5693 contains all of the data required to produce the HTTP response header.
5579 It's always required to set the <literal>status</literal> field of 5694 The <literal>status</literal> field in <literal>r->headers_out</literal>
5580 <literal>r->headers_out</literal>. 5695 must always be set.
5581 If the response status suggests that a response body follows the header, 5696 If the response status indicates that a response body follows the header,
5582 <literal>content_length_n</literal> can be set as well. 5697 <literal>content_length_n</literal> can be set as well.
5583 The default value for this field is -1, which means that the body size is 5698 The default value for this field is <literal>-1</literal>,
5584 unknown. 5699 which means that the body size is unknown.
5585 In this case, chunked transfer encoding is used. 5700 In this case, chunked transfer encoding is used.
5586 To output an arbitrary header, <literal>headers</literal> list should be 5701 To output an arbitrary header, append the <literal>headers</literal> list.
5587 appended.
5588 </para> 5702 </para>
5589 5703
5590 <programlisting> 5704 <programlisting>
5591 static ngx_int_t 5705 static ngx_int_t
5592 ngx_http_foo_content_handler(ngx_http_request_t *r) 5706 ngx_http_foo_content_handler(ngx_http_request_t *r)
5627 5741
5628 <section name="Header filters" id="http_header_filters"> 5742 <section name="Header filters" id="http_header_filters">
5629 5743
5630 <para> 5744 <para>
5631 The <literal>ngx_http_send_header(r)</literal> function invokes the header 5745 The <literal>ngx_http_send_header(r)</literal> function invokes the header
5632 filter chain by calling the top header filter handler 5746 filter chain by calling the first header filter handler stored in
5633 <literal>ngx_http_top_header_filter</literal>. 5747 the <literal>ngx_http_top_header_filter</literal> variable.
5634 It's assumed that every header handler calls the next handler in chain until 5748 It's assumed that every header handler calls the next handler in the chain
5635 the final handler <literal>ngx_http_header_filter(r)</literal> is called. 5749 until the final handler <literal>ngx_http_header_filter(r)</literal> is called.
5636 The final header handler constructs the HTTP response based on 5750 The final header handler constructs the HTTP response based on
5637 <literal>r->headers_out</literal> and passes it to the 5751 <literal>r->headers_out</literal> and passes it to the
5638 <literal>ngx_http_writer_filter</literal> for output. 5752 <literal>ngx_http_writer_filter</literal> for output.
5639 </para> 5753 </para>
5640 5754
5641 <para> 5755 <para>
5642 To add a handler to the header filter chain, one should store its address in 5756 To add a handler to the header filter chain, store its address in
5643 <literal>ngx_http_top_header_filter</literal> global variable at configuration 5757 the global variable <literal>ngx_http_top_header_filter</literal>
5644 time. 5758 at configuration time.
5645 The previous handler address is normally stored in a module's static variable 5759 The previous handler address is normally stored in a static variable in a module
5646 and is called by the newly added handler before exiting. 5760 and is called by the newly added handler before exiting.
5647 </para> 5761 </para>
5648 5762
5649 <para> 5763 <para>
5650 The following is an example header filter module, adding the HTTP header 5764 The following example of a header filter module adds the HTTP header
5651 "X-Foo: foo" to every output with the status 200. 5765 "<literal>X-Foo: foo</literal>" to every response with status
5766 <literal>200</literal>.
5652 </para> 5767 </para>
5653 5768
5654 <programlisting> 5769 <programlisting>
5655 #include &lt;ngx_config.h&gt; 5770 #include &lt;ngx_config.h&gt;
5656 #include &lt;ngx_core.h&gt; 5771 #include &lt;ngx_core.h&gt;
5738 5853
5739 5854
5740 <section name="Response body" id="http_response_body"> 5855 <section name="Response body" id="http_response_body">
5741 5856
5742 <para> 5857 <para>
5743 Response body is sent by calling the function 5858 To send the response body, call the
5744 <literal>ngx_http_output_filter(r, cl)</literal>. 5859 <literal>ngx_http_output_filter(r, cl)</literal> function.
5745 The function can be called multiple times. 5860 The function can be called multiple times.
5746 Each time it sends a part of the response body passed as a buffer chain. 5861 Each time, it sends a part of the response body in the form of a buffer chain.
5747 The last body buffer should have the <literal>last_buf</literal> flag set. 5862 Set the <literal>last_buf</literal> flag in the last body buffer.
5748 </para> 5863 </para>
5749 5864
5750 <para> 5865 <para>
5751 The following example produces a complete HTTP output with "foo" as its body. 5866 The following example produces a complete HTTP response with "foo" as its body.
5752 In order for the example to work not only as a main request but as a subrequest 5867 For the example to work as subrequest as well as a main request,
5753 as well, the <literal>last_in_chain</literal> flag is set in the last buffer 5868 the <literal>last_in_chain</literal> flag is set in the last buffer
5754 of the output. 5869 of the output.
5755 The <literal>last_buf</literal> flag is set only for the main request since 5870 The <literal>last_buf</literal> flag is set only for the main request because
5756 a subrequest's last buffers does not end the entire output. 5871 the last buffer for a subrequest does not end the entire output.
5757 </para> 5872 </para>
5758 5873
5759 <programlisting> 5874 <programlisting>
5760 static ngx_int_t 5875 static ngx_int_t
5761 ngx_http_bar_content_handler(ngx_http_request_t *r) 5876 ngx_http_bar_content_handler(ngx_http_request_t *r)
5802 5917
5803 <section name="Body filters" id="http_body_filters"> 5918 <section name="Body filters" id="http_body_filters">
5804 5919
5805 <para> 5920 <para>
5806 The function <literal>ngx_http_output_filter(r, cl)</literal> invokes the 5921 The function <literal>ngx_http_output_filter(r, cl)</literal> invokes the
5807 body filter chain by calling the top body filter handler 5922 body filter chain by calling the first body filter handler stored in
5808 <literal>ngx_http_top_body_filter</literal>. 5923 the <literal>ngx_http_top_body_filter</literal> variable.
5809 It's assumed that every body handler calls the next handler in chain until 5924 It's assumed that every body handler calls the next handler in the chain until
5810 the final handler <literal>ngx_http_write_filter(r, cl)</literal> is called. 5925 the final handler <literal>ngx_http_write_filter(r, cl)</literal> is called.
5811 </para> 5926 </para>
5812 5927
5813 <para> 5928 <para>
5814 A body filter handler receives a chain of buffers. 5929 A body filter handler receives a chain of buffers.
5815 The handler is supposed to process the buffers and pass a possibly new chain to 5930 The handler is supposed to process the buffers and pass a possibly new chain to
5816 the next handler. 5931 the next handler.
5817 It's worth noting that the chain links <literal>ngx_chain_t</literal> of the 5932 It's worth noting that the chain links <literal>ngx_chain_t</literal> of the
5818 incoming chain belong to the caller. 5933 incoming chain belong to the caller, and must not be reused or changed.
5819 They should never be reused or changed.
5820 Right after the handler completes, the caller can use its output chain links 5934 Right after the handler completes, the caller can use its output chain links
5821 to keep track of the buffers it has sent. 5935 to keep track of the buffers it has sent.
5822 To save the buffer chain or to substitute some buffers before sending further, 5936 To save the buffer chain or to substitute some buffers before passing to the
5823 a handler should allocate its own chain links. 5937 next filter, a handler needs to allocate its own chain links.
5824 </para> 5938 </para>
5825 5939
5826 <para> 5940 <para>
5827 Following is the example of a simple body filter counting the number of 5941 Following is an example of a simple body filter that counts the number of
5828 body bytes. 5942 bytes in the body.
5829 The result is available as the <literal>$counter</literal> variable which can be 5943 The result is available as the <literal>$counter</literal> variable which can be
5830 used in the access log. 5944 used in the access log.
5831 </para> 5945 </para>
5832 5946
5833 <programlisting> 5947 <programlisting>
5967 6081
5968 6082
5969 <section name="Building filter modules" id="http_building_filter_modules"> 6083 <section name="Building filter modules" id="http_building_filter_modules">
5970 6084
5971 <para> 6085 <para>
5972 When writing a body or header filter, a special care should be taken of the 6086 When writing a body or header filter, pay special attention to the filter's
5973 filters order. 6087 position in the filter order.
5974 There's a number of header and body filters registered by nginx standard 6088 There's a number of header and body filters registered by nginx standard
5975 modules. 6089 modules.
5976 It's important to register a filter module in the right place in respect to 6090 The nginx standard modules register a number of head and body filters and it's
5977 other filters. 6091 important to register a new filter module in the right place with respect to
5978 Normally, filters are registered by modules in their postconfiguration handlers. 6092 them.
5979 The order in which filters are called is obviously the reverse of when they are 6093 Normally, modules register filters in their postconfiguration handlers.
5980 registered. 6094 The order in which filters are called during processing is obviously the
5981 </para> 6095 reverse of the order in which they are registered.
5982 6096 </para>
5983 <para> 6097
5984 A special slot <literal>HTTP_AUX_FILTER_MODULES</literal> for third-party filter 6098 <para>
5985 modules is provided by nginx. 6099 For third-party filter modules nginx provides a special slot
5986 To register a filter module in this slot, the <literal>ngx_module_type</literal> 6100 <literal>HTTP_AUX_FILTER_MODULES</literal>.
5987 variable should be set to the value of <literal>HTTP_AUX_FILTER</literal> in 6101 To register a filter module in this slot, set
5988 module's configuration. 6102 the <literal>ngx_module_type</literal> variable to
5989 </para> 6103 <literal>HTTP_AUX_FILTER</literal> in the module's configuration.
5990 6104 </para>
5991 <para> 6105
5992 The following example shows a filter module config file assuming it only has 6106 <para>
5993 one source file <literal>ngx_http_foo_filter_module.c</literal> 6107 The following example shows a filter module config file assuming
6108 for a module with just
6109 one source file, <literal>ngx_http_foo_filter_module.c</literal>.
5994 </para> 6110 </para>
5995 6111
5996 <programlisting> 6112 <programlisting>
5997 ngx_module_type=HTTP_AUX_FILTER 6113 ngx_module_type=HTTP_AUX_FILTER
5998 ngx_module_name=ngx_http_foo_filter_module 6114 ngx_module_name=ngx_http_foo_filter_module
6007 <section name="Buffer reuse" id="http_body_buffers_reuse"> 6123 <section name="Buffer reuse" id="http_body_buffers_reuse">
6008 6124
6009 <para> 6125 <para>
6010 When issuing or altering a stream of buffers, it's often desirable to reuse the 6126 When issuing or altering a stream of buffers, it's often desirable to reuse the
6011 allocated buffers. 6127 allocated buffers.
6012 A standard approach widely adopted in nginx code is to keep two buffer chains 6128 A standard and widely adopted approach in nginx code is to keep
6013 for this purpose: <literal>free</literal> and <literal>busy</literal>. 6129 two buffer chains for this purpose:
6014 The <literal>free</literal> chain keeps all free buffers. 6130 <literal>free</literal> and <literal>busy</literal>.
6015 These buffers can be reused. 6131 The <literal>free</literal> chain keeps all free buffers,
6132 which can be reused.
6016 The <literal>busy</literal> chain keeps all buffers sent by the current 6133 The <literal>busy</literal> chain keeps all buffers sent by the current
6017 module which are still in use by some other filter handler. 6134 module that are still in use by some other filter handler.
6018 A buffer is considered in use if its size is greater than zero. 6135 A buffer is considered in use if its size is greater than zero.
6019 Normally, when a buffer is consumed by a filter, its <literal>pos</literal> 6136 Normally, when a buffer is consumed by a filter, its <literal>pos</literal>
6020 (or <literal>file_pos</literal> for a file buffer) is moved towards 6137 (or <literal>file_pos</literal> for a file buffer) is moved towards
6021 <literal>last</literal> (<literal>file_last</literal> for a file buffer). 6138 <literal>last</literal> (<literal>file_last</literal> for a file buffer).
6022 Once a buffer is completely consumed, it's ready to be reused. 6139 Once a buffer is completely consumed, it's ready to be reused.
6023 To update the <literal>free</literal> chain with newly freed buffers, 6140 To add newly freed buffers to the <literal>free</literal> chain
6024 it's enough to iterate over the <literal>busy</literal> chain and move the zero 6141 it's enough to iterate over the <literal>busy</literal> chain and move the zero
6025 size buffers at the head of it to <literal>free</literal>. 6142 size buffers at the head of it to <literal>free</literal>.
6026 This operation is so common that there is a special function 6143 This operation is so common that there is a special function for it,
6027 <literal>ngx_chain_update_chains(free, busy, out, tag)</literal> which does 6144 <literal>ngx_chain_update_chains(free, busy, out, tag)</literal>.
6028 this.
6029 The function appends the output chain <literal>out</literal> to 6145 The function appends the output chain <literal>out</literal> to
6030 <literal>busy</literal> and moves free buffers from the top of 6146 <literal>busy</literal> and moves free buffers from the top of
6031 <literal>busy</literal> to <literal>free</literal>. 6147 <literal>busy</literal> to <literal>free</literal>.
6032 Only the buffers with the given <literal>tag</literal> are reused. 6148 Only the buffers with the specified <literal>tag</literal> are reused.
6033 This lets a module reuse only the buffers allocated by itself. 6149 This lets a module reuse only the buffers that it allocated itself.
6034 </para> 6150 </para>
6035 6151
6036 <para> 6152 <para>
6037 The following example is a body filter inserting the “foo” string before each 6153 The following example is a body filter that inserts the string “foo” before each
6038 incoming buffer. 6154 incoming buffer.
6039 The new buffers allocated by the module are reused if possible. 6155 The new buffers allocated by the module are reused if possible.
6040 Note that for this example to work properly, it's also required to set up a 6156 Note that for this example to work properly, setting up a
6041 header filter and reset <literal>content_length_n</literal> to -1, which is 6157 <link id="http_header_filters">header filter</link>
6042 beyond the scope of this section. 6158 and resetting <literal>content_length_n</literal> to <literal>-1</literal>
6159 is also required, but the relevant code is not provided here.
6043 </para> 6160 </para>
6044 6161
6045 <programlisting> 6162 <programlisting>
6046 typedef struct { 6163 typedef struct {
6047 ngx_chain_t *free; 6164 ngx_chain_t *free;
6122 <section name="Load balancing" id="http_load_balancing"> 6239 <section name="Load balancing" id="http_load_balancing">
6123 6240
6124 <para> 6241 <para>
6125 The 6242 The
6126 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link> 6243 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link>
6127 provides basic functionality to pass requests to remote servers. 6244 provides the basic functionality needed to pass requests to remote servers.
6128 This functionality is used by modules that implement specific protocols, 6245 Modules that implement specific protocols, such as HTTP or FastCGI, use
6129 such as HTTP or FastCGI. 6246 this functionality.
6130 The module also provides an interface for creating custom 6247 The module also provides an interface for creating custom
6131 load balancing modules and implements a default round-robin balancing method. 6248 load-balancing modules and implements a default round-robin method.
6132 </para> 6249 </para>
6133 6250
6134 <para> 6251 <para>
6135 Examples of modules that implement alternative load balancing methods are 6252 The <link doc="../http/ngx_http_upstream_module.xml" id="least_conn"/>
6136 <link doc="../http/ngx_http_upstream_module.xml" id="least_conn"/> 6253 and <link doc="../http/ngx_http_upstream_module.xml" id="hash"/>
6137 and <link doc="../http/ngx_http_upstream_module.xml" id="hash"/>. 6254 modules implement alternative load-balancing methods, but
6138 Note that these modules are actually implemented as extensions of the upstream 6255 are actually implemented as extensions of the upstream round-robin
6139 module and share a lot of code, such as representation of a server group. 6256 module and share a lot of code with it, such as the representation
6257 of a server group.
6140 The <link doc="../http/ngx_http_upstream_module.xml" id="keepalive"/> module 6258 The <link doc="../http/ngx_http_upstream_module.xml" id="keepalive"/> module
6141 is an example of an independent module, extending upstream functionality. 6259 is an independent module that extends upstream functionality.
6142 </para> 6260 </para>
6143 6261
6144 <para> 6262 <para>
6145 The 6263 The
6146 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link> 6264 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link>
6147 may be configured explicitly by placing the corresponding 6265 can be configured explicitly by placing the corresponding
6148 <link doc="../http/ngx_http_upstream_module.xml" id="upstream"/> block into 6266 <link doc="../http/ngx_http_upstream_module.xml" id="upstream"/> block into
6149 the configuration file, or implicitly by using directives 6267 the configuration file, or implicitly by using directives
6150 that accept a URL evaluated at some point to the list of servers, 6268 such as <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/>
6151 for example, 6269 that accept a URL that gets evaluated at some point into a list of servers.
6152 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/>. 6270 The alternative load-balancing methods are available only with an explicit
6153 Only explicit configurations may use an alternative load balancing method. 6271 upstream configuration.
6154 The upstream module configuration has its own directive context 6272 The upstream module configuration has its own directive context
6155 <literal>NGX_HTTP_UPS_CONF</literal>. 6273 <literal>NGX_HTTP_UPS_CONF</literal>.
6156 The structure is defined as follows: 6274 The structure is defined as follows:
6157 <programlisting> 6275 <programlisting>
6158 struct ngx_http_upstream_srv_conf_s { 6276 struct ngx_http_upstream_srv_conf_s {
6175 </programlisting> 6293 </programlisting>
6176 6294
6177 <list type="bullet"> 6295 <list type="bullet">
6178 6296
6179 <listitem> 6297 <listitem>
6180 <literal>srv_conf</literal> — configuration context of upstream modules 6298 <literal>srv_conf</literal> — Configuration context of upstream modules.
6181 </listitem> 6299 </listitem>
6182 6300
6183 <listitem> 6301 <listitem>
6184 <literal>servers</literal> — array of 6302 <literal>servers</literal> — Array of
6185 <literal>ngx_http_upstream_server_t</literal>, the result of parsing a set of 6303 <literal>ngx_http_upstream_server_t</literal>, the result of parsing a set of
6186 <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directives 6304 <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directives
6187 in the <literal>upstream</literal> block 6305 in the <literal>upstream</literal> block.
6188 </listitem> 6306 </listitem>
6189 6307
6190 <listitem> 6308 <listitem>
6191 <literal>flags</literal> — flags that mostly mark which features 6309 <literal>flags</literal> — Flags that mostly mark which features
6192 (configured as parameters of 6310 are supported by the load-balancing method.
6193 the <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directive) 6311 The features are configured as parameters of
6194 are supported by the particular load balancing method. 6312 the <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directive:
6195 6313
6196 <list type="bullet"> 6314
6197 6315 <list type="bullet">
6198 <listitem> 6316
6199 <literal>NGX_HTTP_UPSTREAM_CREATE</literal> — used to distinguish explicitly 6317 <listitem>
6200 defined upstreams from automatically created by 6318 <literal>NGX_HTTP_UPSTREAM_CREATE</literal> — Distinguishes explicitly
6201 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/> and “friends” 6319 defined upstreams from those that are automatically created by the
6320 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/> directive
6321 and “friends”
6202 (FastCGI, SCGI, etc.) 6322 (FastCGI, SCGI, etc.)
6203 </listitem> 6323 </listitem>
6204 6324
6205 <listitem> 6325 <listitem>
6206 <literal>NGX_HTTP_UPSTREAM_WEIGHT</literal> — “<literal>weight</literal>” 6326 <literal>NGX_HTTP_UPSTREAM_WEIGHT</literal> — The “<literal>weight</literal>”
6207 is supported 6327 parameter is supported
6208 </listitem> 6328 </listitem>
6209 6329
6210 <listitem> 6330 <listitem>
6211 <literal>NGX_HTTP_UPSTREAM_MAX_FAILS</literal> — “<literal>max_fails</literal>” 6331 <literal>NGX_HTTP_UPSTREAM_MAX_FAILS</literal> — The
6212 is supported 6332 “<literal>max_fails</literal>” parameter is supported
6213 </listitem> 6333 </listitem>
6214 6334
6215 <listitem> 6335 <listitem>
6216 <literal>NGX_HTTP_UPSTREAM_FAIL_TIMEOUT</literal> — 6336 <literal>NGX_HTTP_UPSTREAM_FAIL_TIMEOUT</literal> —
6217 “<literal>fail_timeout</literal>” is supported 6337 The “<literal>fail_timeout</literal>” parameter is supported
6218 </listitem> 6338 </listitem>
6219 6339
6220 <listitem> 6340 <listitem>
6221 <literal>NGX_HTTP_UPSTREAM_DOWN</literal> — “<literal>down</literal>” 6341 <literal>NGX_HTTP_UPSTREAM_DOWN</literal> — The “<literal>down</literal>”
6222 is supported 6342 parameter is supported
6223 </listitem> 6343 </listitem>
6224 6344
6225 <listitem> 6345 <listitem>
6226 <literal>NGX_HTTP_UPSTREAM_BACKUP</literal> — “<literal>backup</literal>” 6346 <literal>NGX_HTTP_UPSTREAM_BACKUP</literal> — The “<literal>backup</literal>”
6227 is supported 6347 parameter is supported
6228 </listitem> 6348 </listitem>
6229 6349
6230 <listitem> 6350 <listitem>
6231 <literal>NGX_HTTP_UPSTREAM_MAX_CONNS</literal> — “<literal>max_conns</literal>” 6351 <literal>NGX_HTTP_UPSTREAM_MAX_CONNS</literal> — The
6232 is supported 6352 “<literal>max_conns</literal>” parameter is supported
6233 </listitem> 6353 </listitem>
6234 6354
6235 </list> 6355 </list>
6236 6356
6237 </listitem> 6357 </listitem>
6238 6358
6239 <listitem> 6359 <listitem>
6240 <literal>host</literal> — the name of an upstream 6360 <literal>host</literal> — Name of the upstream.
6241 </listitem> 6361 </listitem>
6242 6362
6243 <listitem> 6363 <listitem>
6244 <literal>file_name, line</literal> — the name of the configuration file 6364 <literal>file_name, line</literal> — Name of the configuration file
6245 and the line where the <literal>upstream</literal> block is located 6365 and the line where the <literal>upstream</literal> block is located.
6246 </listitem> 6366 </listitem>
6247 6367
6248 <listitem> 6368 <listitem>
6249 <literal>port</literal> and <literal>no_port</literal> — unused by explicit 6369 <literal>port</literal> and <literal>no_port</literal> — Not used for
6250 upstreams 6370 explicitly defined upstream groups.
6251 </listitem> 6371 </listitem>
6252 6372
6253 <listitem> 6373 <listitem>
6254 <literal>shm_zone</literal> — a shared memory zone used by this upstream, if any 6374 <literal>shm_zone</literal> — Shared memory zone used by this upstream group,
6255 </listitem> 6375 if any.
6256 6376 </listitem>
6257 <listitem> 6377
6258 <literal>peer</literal> — an object that holds generic methods for 6378 <listitem>
6379 <literal>peer</literal> — object that holds generic methods for
6259 initializing upstream configuration: 6380 initializing upstream configuration:
6260 6381
6261 <programlisting> 6382 <programlisting>
6262 typedef struct { 6383 typedef struct {
6263 ngx_http_upstream_init_pt init_upstream; 6384 ngx_http_upstream_init_pt init_upstream;
6264 ngx_http_upstream_init_peer_pt init; 6385 ngx_http_upstream_init_peer_pt init;
6265 void *data; 6386 void *data;
6266 } ngx_http_upstream_peer_t; 6387 } ngx_http_upstream_peer_t;
6267 </programlisting> 6388 </programlisting>
6268 A module that implements a load balancing algorithm must set these 6389 A module that implements a load-balancing algorithm must set these
6269 methods and initialize private <literal>data</literal>. 6390 methods and initialize private <literal>data</literal>.
6270 If <literal>init_upstream</literal> was not initialized during configuration 6391 If <literal>init_upstream</literal> was not initialized during configuration
6271 parsing, <literal>ngx_http_upstream_module</literal> sets it to default 6392 parsing, <literal>ngx_http_upstream_module</literal> sets it to the default
6272 <literal>ngx_http_upstream_init_round_robin</literal>. 6393 <literal>ngx_http_upstream_init_round_robin</literal> algorithm.
6273 6394
6274 <list type="bullet"> 6395 <list type="bullet">
6275 <listitem> 6396 <listitem>
6276 <literal>init_upstream(cf, us)</literal> — configuration-time 6397 <literal>init_upstream(cf, us)</literal> — Configuration-time
6277 method responsible for initializing a group of servers and 6398 method responsible for initializing a group of servers and
6278 initializing the <literal>init()</literal> method in case of success. 6399 initializing the <literal>init()</literal> method in case of success.
6279 A typical load balancing module uses a list of servers in the upstream block 6400 A typical load-balancing module uses a list of servers in the
6280 to create some efficient data structure that it uses and saves own 6401 <literal>upstream</literal> block
6402 to create an efficient data structure that it uses and saves its own
6281 configuration to the <literal>data</literal> field. 6403 configuration to the <literal>data</literal> field.
6282 </listitem> 6404 </listitem>
6283 6405
6284 <listitem> 6406 <listitem>
6285 <literal>init(r, us)</literal> — initializes per-request 6407 <literal>init(r, us)</literal> — Initializes a per-request
6286 <literal>ngx_http_upstream_peer_t.peer</literal> (not to be confused with the 6408 <literal>ngx_http_upstream_peer_t.peer</literal> structure that is used for
6409 load balancing (not to be confused with the
6287 <literal>ngx_http_upstream_srv_conf_t.peer</literal> described above which 6410 <literal>ngx_http_upstream_srv_conf_t.peer</literal> described above which
6288 is per-upstream) structure that is used for load balancing. 6411 is per-upstream).
6289 It will be passed as <literal>data</literal> argument to all callbacks that 6412 It is passed as the <literal>data</literal> argument to all callbacks that
6290 deal with server selection. 6413 deal with server selection.
6291 </listitem> 6414 </listitem>
6292 </list> 6415 </list>
6293 6416
6294 </listitem> 6417 </listitem>
6295 </list> 6418 </list>
6296 </para> 6419 </para>
6297 6420
6298 <para> 6421 <para>
6299 When nginx has to pass a request to another host for processing, it uses 6422 When nginx has to pass a request to another host for processing, it uses
6300 a configured load balancing method to obtain an address to connect to. 6423 the configured load-balancing method to obtain an address to connect to.
6301 The method is taken from the 6424 The method is obtained from the
6302 <literal>ngx_http_upstream_t.peer</literal> object 6425 <literal>ngx_http_upstream_t.peer</literal> object
6303 of type <literal>ngx_peer_connection_t</literal>: 6426 of type <literal>ngx_peer_connection_t</literal>:
6304 <programlisting> 6427 <programlisting>
6305 struct ngx_peer_connection_s { 6428 struct ngx_peer_connection_s {
6306 [...] 6429 ...
6307 6430
6308 struct sockaddr *sockaddr; 6431 struct sockaddr *sockaddr;
6309 socklen_t socklen; 6432 socklen_t socklen;
6310 ngx_str_t *name; 6433 ngx_str_t *name;
6311 6434
6319 #if (NGX_SSL || NGX_COMPAT) 6442 #if (NGX_SSL || NGX_COMPAT)
6320 ngx_event_set_peer_session_pt set_session; 6443 ngx_event_set_peer_session_pt set_session;
6321 ngx_event_save_peer_session_pt save_session; 6444 ngx_event_save_peer_session_pt save_session;
6322 #endif 6445 #endif
6323 6446
6324 [..] 6447 ...
6325 }; 6448 };
6326 </programlisting> 6449 </programlisting>
6327 6450
6328 The structure has the following fields: 6451 The structure has the following fields:
6329 6452
6330 <list type="bullet"> 6453 <list type="bullet">
6331 <listitem> 6454 <listitem>
6332 <literal>sockaddr</literal>, <literal>socklen</literal>, 6455 <literal>sockaddr</literal>, <literal>socklen</literal>,
6333 <literal>name</literal> — address of an upstream server to connect to; 6456 <literal>name</literal> — Address of the upstream server to connect to;
6334 this is the output parameter of a load balancing method 6457 this is the output parameter of a load-balancing method.
6335 </listitem> 6458 </listitem>
6336 6459
6337 <listitem> 6460 <listitem>
6338 <literal>data</literal> — per-request load balancing method data; keeps the 6461 <literal>data</literal> — The per-request data of a load-balancing method;
6339 state of selection algorithm and usually includes the link to upstream 6462 keeps the state of the selection algorithm and usually includes the link
6340 configuration. 6463 to the upstream configuration.
6341 It will be passed as an argument to all methods that deal with server selection 6464 It is passed as an argument to all methods that deal with server selection
6342 (see below) 6465 (see <link id="lb_method_get">below</link>).
6343 </listitem> 6466 </listitem>
6344 6467
6345 <listitem> 6468 <listitem>
6346 <literal>tries</literal> — allowed 6469 <literal>tries</literal> — Allowed
6347 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_next_upstream_tries">number</link> 6470 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_next_upstream_tries">number</link>
6348 of attempts to connect to an upstream. 6471 of attempts to connect to an upstream server.
6349 </listitem> 6472 </listitem>
6350 6473
6351 <listitem> 6474 <listitem>
6352 <literal>get</literal>, <literal>free</literal>, <literal>notify</literal>, 6475 <literal>get</literal>, <literal>free</literal>, <literal>notify</literal>,
6353 <literal>set_session</literal>, and <literal>save_session</literal> 6476 <literal>set_session</literal>, and <literal>save_session</literal>
6354 - methods of the load balancing module, see description below 6477 - Methods of the load-balancing module, described below.
6355 </listitem> 6478 </listitem>
6356 6479
6357 </list> 6480 </list>
6358 6481
6359 </para> 6482 </para>
6360 6483
6361 <para> 6484 <para>
6362 All methods accept at least two arguments: peer connection object 6485 All methods accept at least two arguments: a peer connection object
6363 <literal>pc</literal> and the <literal>data</literal> created by 6486 <literal>pc</literal> and the <literal>data</literal> created by
6364 <literal>ngx_http_upstream_srv_conf_t.peer.init()</literal>. 6487 <literal>ngx_http_upstream_srv_conf_t.peer.init()</literal>.
6365 Note that in general case it may differ from <literal>pc.data</literal> due 6488 Note that it might differ from <literal>pc.data</literal> due
6366 to “chaining” of load balancing modules. 6489 to “chaining” of load-balancing modules.
6367 </para> 6490 </para>
6368 6491
6369 <para> 6492 <para>
6370 6493
6371 <list type="bullet"> 6494 <list type="bullet">
6372 <listitem> 6495 <listitem id="lb_method_get">
6373 <literal>get(pc, data)</literal> — the method is called when the upstream 6496 <literal>get(pc, data)</literal> — The method called when the upstream
6374 module is ready to pass a request to an upstream server and needs to know 6497 module is ready to pass a request to an upstream server and needs to know
6375 its address. 6498 its address.
6376 The method is responsible to fill in the <literal>sockaddr</literal>, 6499 The method has to fill the <literal>sockaddr</literal>,
6377 <literal>socklen</literal>, and <literal>name</literal> fields of 6500 <literal>socklen</literal>, and <literal>name</literal> fields of
6378 <literal>ngx_peer_connection_t</literal> structure. 6501 <literal>ngx_peer_connection_t</literal> structure.
6379 The return value may be one of: 6502 The return is one of:
6380 6503
6381 <list type="bullet"> 6504 <list type="bullet">
6382 6505
6383 <listitem> 6506 <listitem>
6384 <literal>NGX_OK</literal> — server was selected 6507 <literal>NGX_OK</literal> — Server was selected.
6385 </listitem> 6508 </listitem>
6386 6509
6387 <listitem> 6510 <listitem>
6388 <literal>NGX_ERROR</literal> — internal error occurred 6511 <literal>NGX_ERROR</literal> — Internal error occurred.
6389 </listitem> 6512 </listitem>
6390 6513
6391 <listitem> 6514 <listitem>
6392 <literal>NGX_BUSY</literal> — there are no available servers at the moment. 6515 <literal>NGX_BUSY</literal> — no servers are currently available.
6393 This can happen due to many reasons, such as: dynamic server group is empty, 6516 This can happen due to many reasons, including: the dynamic server group is
6394 all servers in the group are in the failed state, 6517 empty, all servers in the group are in the failed state, or
6395 all servers in the group are already 6518 all servers in the group are already
6396 handling the maximum number of connections or similar. 6519 handling the maximum number of connections.
6397 </listitem> 6520 </listitem>
6398 6521
6399 <listitem> 6522 <listitem>
6400 <literal>NGX_DONE</literal> — this is set by the <literal>keepalive</literal> 6523 <literal>NGX_DONE</literal> — the underlying connection was reused and there
6401 module to indicate that the underlying connection was reused and there is no 6524 is no need to create a new connection to the upstream server.
6402 need to create a new connection to the upstream server. 6525 This value is set by the <literal>keepalive</literal> module.
6403 </listitem> 6526 </listitem>
6404 6527
6405 <!-- 6528 <!--
6406 <listitem> 6529 <listitem>
6407 <literal>NGX_ABORT</literal> — this is set by the <literal>queue</literal> 6530 <literal>NGX_ABORT</literal> — the request was queued and the further
6408 module to indicate that the request was queued and the further processing 6531 processing of this request should be postponed.
6409 of this request should be postponed. 6532 This value is set by the <literal>queue</literal> module.
6410 </listitem> 6533 </listitem>
6411 --> 6534 -->
6412 6535
6413 </list> 6536 </list>
6414 6537
6415 </listitem> 6538 </listitem>
6416 6539
6417 <listitem> 6540 <listitem>
6418 <literal>free(pc, data, state)</literal> — the method is called when an 6541 <literal>free(pc, data, state)</literal> — The method called when an
6419 upstream module has finished work with a particular server. 6542 upstream module has finished work with a particular server.
6420 The <literal>state</literal> argument is the status of upstream connection 6543 The <literal>state</literal> argument is the completion status of the upstream
6421 completion. 6544 connection, a bitmask with the following possible values:
6422 This is a bitmask, the following values may be set: 6545
6423 <literal>NGX_PEER_FAILED</literal> — this attempt is considered 6546 <list type="bullet">
6424 <link doc="../http/ngx_http_upstream_module.xml" id="max_fails">unsuccessful</link>, 6547
6425 <literal>NGX_PEER_NEXT</literal> — a special case with codes 403 and 404 6548 <listitem>
6426 (see link above), which are not considered a failure. 6549 <literal>NGX_PEER_FAILED</literal> — Attempt was
6427 <literal>NGX_PEER_KEEPALIVE</literal>. 6550 <link doc="../http/ngx_http_upstream_module.xml" id="max_fails">unsuccessful</link>
6428 Also, <literal>tries</literal> counter is decremented by this method. 6551 </listitem>
6429 </listitem> 6552
6430 6553 <listitem>
6431 <listitem> 6554 <literal>NGX_PEER_NEXT</literal> — A special case when upstream server
6432 <literal>notify(pc, data, type)</literal> — currently unused 6555 returns codes <literal>403</literal> or <literal>404</literal>,
6556 which are not considered a
6557 <link doc="../http/ngx_http_upstream_module.xml" id="max_fails">failure</link>.
6558 </listitem>
6559
6560 <listitem>
6561 <literal>NGX_PEER_KEEPALIVE</literal> — Currently unused
6562 </listitem>
6563
6564 </list>
6565
6566 This method also decrements the <literal>tries</literal> counter.
6567
6568 </listitem>
6569
6570 <listitem>
6571 <literal>notify(pc, data, type)</literal> — Currently unused
6433 in the OSS version. 6572 in the OSS version.
6434 </listitem> 6573 </listitem>
6435 6574
6436 <listitem> 6575 <listitem>
6437 <literal>set_session(pc, data)</literal> and 6576 <literal>set_session(pc, data)</literal> and
6438 <literal>save_session(pc, data)</literal> 6577 <literal>save_session(pc, data)</literal>
6439 — SSL-specific methods that allow to cache sessions to upstream 6578 — SSL-specific methods that enable caching sessions to upstream
6440 servers. 6579 servers.
6441 The implementation is provided by the round-robin balancing method. 6580 The implementation is provided by the round-robin balancing method.
6442 </listitem> 6581 </listitem>
6443 6582
6444 </list> 6583 </list>