0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #ifndef _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_
|
|
8 #define _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_
|
|
9
|
|
10
|
|
11 #include <ngx_config.h>
|
|
12 #include <ngx_core.h>
|
|
13 #include <ngx_event.h>
|
|
14 #include <ngx_event_connect.h>
|
|
15 #include <ngx_event_pipe.h>
|
|
16 #include <ngx_http.h>
|
|
17
|
|
18
|
|
19 typedef enum {
|
|
20 NGX_HTTP_PROXY_CACHE_PASS = 1,
|
|
21 NGX_HTTP_PROXY_CACHE_BYPASS,
|
|
22 NGX_HTTP_PROXY_CACHE_AUTH,
|
|
23 NGX_HTTP_PROXY_CACHE_PGNC,
|
|
24 NGX_HTTP_PROXY_CACHE_MISS,
|
|
25 NGX_HTTP_PROXY_CACHE_EXPR,
|
|
26 NGX_HTTP_PROXY_CACHE_AGED,
|
|
27 NGX_HTTP_PROXY_CACHE_HIT
|
|
28 } ngx_http_proxy_state_e;
|
|
29
|
|
30
|
|
31 typedef enum {
|
|
32 NGX_HTTP_PROXY_CACHE_BPS = 1,
|
|
33 NGX_HTTP_PROXY_CACHE_XAE,
|
|
34 NGX_HTTP_PROXY_CACHE_CTL,
|
|
35 NGX_HTTP_PROXY_CACHE_EXP,
|
|
36 NGX_HTTP_PROXY_CACHE_MVD,
|
|
37 NGX_HTTP_PROXY_CACHE_LMF,
|
|
38 NGX_HTTP_PROXY_CACHE_PDE
|
|
39 } ngx_http_proxy_reason_e;
|
|
40
|
|
41
|
|
42 typedef struct {
|
|
43 ngx_str_t url;
|
|
44 ngx_str_t host;
|
|
45 ngx_str_t uri;
|
|
46 ngx_str_t host_header;
|
|
47 ngx_str_t port_text;
|
|
48 ngx_str_t *location;
|
|
49
|
|
50 in_port_t port;
|
|
51
|
|
52 unsigned default_port:1;
|
|
53 } ngx_http_proxy_upstream_conf_t;
|
|
54
|
|
55
|
|
56 typedef struct {
|
|
57 size_t header_buffer_size;
|
|
58 size_t busy_buffers_size;
|
|
59 size_t max_temp_file_size;
|
|
60 size_t temp_file_write_size;
|
|
61
|
|
62 ngx_msec_t connect_timeout;
|
|
63 ngx_msec_t send_timeout;
|
|
64 ngx_msec_t read_timeout;
|
|
65 time_t default_expires;
|
|
66
|
|
67 ngx_int_t lm_factor;
|
|
68
|
|
69 ngx_uint_t next_upstream;
|
|
70 ngx_uint_t use_stale;
|
|
71
|
|
72 ngx_bufs_t bufs;
|
|
73
|
|
74 ngx_flag_t cyclic_temp_file;
|
|
75 ngx_flag_t cache;
|
|
76 ngx_flag_t preserve_host;
|
|
77 ngx_flag_t set_x_real_ip;
|
|
78 ngx_flag_t add_x_forwarded_for;
|
|
79 ngx_flag_t pass_server;
|
|
80 ngx_flag_t pass_x_accel_expires;
|
|
81 ngx_flag_t ignore_expires;
|
|
82
|
|
83 ngx_path_t *cache_path;
|
|
84 ngx_path_t *temp_path;
|
|
85
|
|
86 ngx_http_busy_lock_t *busy_lock;
|
|
87
|
|
88 ngx_http_proxy_upstream_conf_t *upstream;
|
|
89 ngx_peers_t *peers;
|
|
90 } ngx_http_proxy_loc_conf_t;
|
|
91
|
|
92
|
|
93 /*
|
|
94 * "EXPR/10/5/- 200/EXP/60 4"
|
|
95 * "MISS/-/-/B 503/-/- -"
|
|
96 * "EXPR/10/20/SB HIT/-/- -"
|
|
97 * "EXPR/10/15/NB HIT/-/- -"
|
|
98 */
|
|
99
|
|
100 typedef struct {
|
|
101 ngx_http_proxy_state_e cache_state;
|
|
102 time_t expired;
|
|
103 time_t bl_time;
|
|
104 ngx_uint_t bl_state;
|
|
105
|
|
106 ngx_uint_t status;
|
|
107 ngx_http_proxy_reason_e reason;
|
|
108 time_t time;
|
|
109 time_t expires;
|
|
110
|
|
111 ngx_str_t *peer;
|
|
112 } ngx_http_proxy_state_t;
|
|
113
|
|
114
|
|
115 typedef struct {
|
|
116 ngx_list_t headers;
|
|
117 #if 0
|
|
118 ngx_table_t headers; /* it must be first field */
|
|
119 #endif
|
|
120
|
|
121 ngx_table_elt_t *date;
|
|
122 ngx_table_elt_t *server;
|
|
123
|
|
124 ngx_table_elt_t *expires;
|
|
125 ngx_table_elt_t *cache_control;
|
|
126 ngx_table_elt_t *etag;
|
|
127 ngx_table_elt_t *x_accel_expires;
|
|
128
|
|
129 ngx_table_elt_t *connection;
|
|
130 ngx_table_elt_t *content_type;
|
|
131 ngx_table_elt_t *content_length;
|
|
132 ngx_table_elt_t *last_modified;
|
|
133 ngx_table_elt_t *location;
|
|
134 ngx_table_elt_t *accept_ranges;
|
|
135 ngx_table_elt_t *x_pad;
|
|
136
|
|
137 off_t content_length_n;
|
|
138 } ngx_http_proxy_headers_in_t;
|
|
139
|
|
140
|
|
141 typedef struct {
|
|
142 ngx_http_cache_ctx_t ctx;
|
|
143 ngx_uint_t status;
|
|
144 ngx_str_t status_line;
|
|
145
|
|
146 ngx_http_proxy_headers_in_t headers_in;
|
|
147 } ngx_http_proxy_cache_t;
|
|
148
|
|
149
|
|
150 typedef struct {
|
|
151 ngx_peer_connection_t peer;
|
|
152 ngx_uint_t status;
|
|
153 ngx_str_t status_line;
|
|
154 ngx_uint_t method;
|
|
155
|
|
156 ngx_output_chain_ctx_t *output_chain_ctx;
|
|
157 ngx_event_pipe_t *event_pipe;
|
|
158
|
|
159 ngx_http_proxy_headers_in_t headers_in;
|
|
160 } ngx_http_proxy_upstream_t;
|
|
161
|
|
162
|
|
163 typedef struct ngx_http_proxy_ctx_s ngx_http_proxy_ctx_t;
|
|
164
|
|
165 struct ngx_http_proxy_ctx_s {
|
|
166 ngx_http_request_t *request;
|
|
167 ngx_http_proxy_loc_conf_t *lcf;
|
|
168 ngx_http_proxy_upstream_t *upstream;
|
|
169 ngx_http_proxy_cache_t *cache;
|
|
170
|
|
171 ngx_buf_t *header_in;
|
|
172
|
|
173 ngx_http_busy_lock_ctx_t busy_lock;
|
|
174
|
|
175 unsigned accel:1;
|
|
176
|
|
177 unsigned cachable:1;
|
|
178 unsigned stale:1;
|
|
179 unsigned try_busy_lock:1;
|
|
180 unsigned busy_locked:1;
|
|
181 unsigned valid_header_in:1;
|
|
182
|
|
183 unsigned request_sent:1;
|
|
184 unsigned header_sent:1;
|
|
185
|
|
186
|
|
187 /* used to parse an upstream HTTP header */
|
|
188 ngx_uint_t status;
|
|
189 u_char *status_start;
|
|
190 u_char *status_end;
|
|
191 ngx_uint_t status_count;
|
|
192 ngx_uint_t parse_state;
|
|
193
|
|
194 ngx_http_proxy_state_t *state;
|
|
195 ngx_array_t states; /* of ngx_http_proxy_state_t */
|
|
196
|
|
197 /*
|
|
198 * we declare "action" as "char *" because the actions are usually
|
|
199 * the static strings and in the "u_char *" case we have to override
|
|
200 * all the time their types
|
|
201 */
|
|
202
|
|
203 char *action;
|
|
204 ngx_http_log_ctx_t *saved_ctx;
|
|
205 ngx_log_handler_pt saved_handler;
|
|
206 };
|
|
207
|
|
208
|
|
209 typedef struct {
|
|
210 ngx_uint_t connection;
|
|
211 ngx_http_proxy_ctx_t *proxy;
|
|
212 } ngx_http_proxy_log_ctx_t;
|
|
213
|
|
214
|
|
215 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 30
|
|
216
|
|
217
|
|
218 #define NGX_HTTP_PROXY_FT_ERROR 0x02
|
|
219 #define NGX_HTTP_PROXY_FT_TIMEOUT 0x04
|
|
220 #define NGX_HTTP_PROXY_FT_INVALID_HEADER 0x08
|
|
221 #define NGX_HTTP_PROXY_FT_HTTP_500 0x10
|
|
222 #define NGX_HTTP_PROXY_FT_HTTP_404 0x20
|
|
223 #define NGX_HTTP_PROXY_FT_BUSY_LOCK 0x40
|
|
224 #define NGX_HTTP_PROXY_FT_MAX_WAITING 0x80
|
|
225
|
|
226
|
|
227 int ngx_http_proxy_request_upstream(ngx_http_proxy_ctx_t *p);
|
|
228
|
|
229 #if (NGX_HTTP_FILE_CACHE)
|
|
230
|
|
231 int ngx_http_proxy_get_cached_response(ngx_http_proxy_ctx_t *p);
|
|
232 int ngx_http_proxy_send_cached_response(ngx_http_proxy_ctx_t *p);
|
|
233 int ngx_http_proxy_is_cachable(ngx_http_proxy_ctx_t *p);
|
|
234 int ngx_http_proxy_update_cache(ngx_http_proxy_ctx_t *p);
|
|
235
|
|
236 void ngx_http_proxy_cache_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
237
|
|
238 #endif
|
|
239
|
|
240 void ngx_http_proxy_check_broken_connection(ngx_event_t *ev);
|
|
241
|
|
242 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev);
|
|
243 void ngx_http_proxy_upstream_busy_lock(ngx_http_proxy_ctx_t *p);
|
|
244
|
|
245 size_t ngx_http_proxy_log_error(void *data, char *buf, size_t len);
|
|
246 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc);
|
|
247 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p);
|
|
248
|
|
249 int ngx_http_proxy_parse_status_line(ngx_http_proxy_ctx_t *p);
|
|
250 int ngx_http_proxy_copy_header(ngx_http_proxy_ctx_t *p,
|
|
251 ngx_http_proxy_headers_in_t *headers_in);
|
|
252
|
|
253
|
|
254
|
|
255 extern ngx_module_t ngx_http_proxy_module;
|
|
256 extern ngx_http_header_t ngx_http_proxy_headers_in[];
|
|
257
|
|
258
|
|
259
|
|
260 #endif /* _NGX_HTTP_PROXY_HANDLER_H_INCLUDED_ */
|