Still confused with try_files
Igor Sysoev
is at rambler-co.ru
Thu Mar 19 17:13:04 MSK 2009
On Thu, Mar 19, 2009 at 02:54:53PM +0100, Otto Bretz wrote:
> 2009/3/19 Igor Sysoev <is at rambler-co.ru>:
> >
> > The attached patch should fix the bug.
>
> I tried this patch together with the 0.6.3.5 try_files patch and I get
> the following log message now:
> 2009/03/19 14:30:10 [error] 2795#0: *43 rewrite or internal
> redirection cycle while internal redirect to
> "/members/index.php?q=/members/user/register", client: 89.236.30.115,
> server: themansion.mine.nu, request: "GET /members/user/register
> HTTP/1.1", host: "themansion.mine.nu"
>
> It looks like the $request_uri; contains the directory (/members/).
> How can this be removed? Drupal expects a redirect to
> "/members/index.php?q=user/register".
>
> My config file is available here:
> http://www.giuntilabs.net/files/nginx/nginx.conf
You can not apply the patch against 0.6.35: there are missed parts, that
availabe in 0.7 only.
The attached patch includes all try_files functionality and bugfixes.
--
Igor Sysoev
http://sysoev.ru/en/
-------------- next part --------------
Index: src/http/ngx_http.h
===================================================================
--- src/http/ngx_http.h (revision 1891)
+++ src/http/ngx_http.h (working copy)
@@ -71,6 +71,8 @@
ngx_int_t ngx_http_parse_header_line(ngx_http_request_t *r, ngx_buf_t *b);
ngx_int_t ngx_http_parse_multi_header_lines(ngx_array_t *headers,
ngx_str_t *name, ngx_str_t *value);
+void ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri,
+ ngx_str_t *args);
ngx_int_t ngx_http_find_server_conf(ngx_http_request_t *r);
void ngx_http_update_location_config(ngx_http_request_t *r);
Index: src/http/ngx_http_core_module.c
===================================================================
--- src/http/ngx_http_core_module.c (revision 1891)
+++ src/http/ngx_http_core_module.c (working copy)
@@ -62,6 +62,8 @@
void *conf);
static char *ngx_http_core_error_page(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
+static char *ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd,
+ void *conf);
static char *ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_core_error_log(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -515,6 +517,13 @@
0,
NULL },
+ { ngx_string("try_files"),
+ NGX_HTTP_LOC_CONF|NGX_CONF_2MORE,
+ ngx_http_core_try_files,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ 0,
+ NULL },
+
{ ngx_string("post_action"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
@@ -997,6 +1006,185 @@
ngx_int_t
+ngx_http_core_try_files_phase(ngx_http_request_t *r,
+ ngx_http_phase_handler_t *ph)
+{
+ size_t len, root, alias, reserve, allocated;
+ u_char *p, *name;
+ ngx_str_t path, args;
+ ngx_uint_t test_dir;
+ ngx_http_try_file_t *tf;
+ ngx_open_file_info_t of;
+ ngx_http_script_code_pt code;
+ ngx_http_script_engine_t e;
+ ngx_http_core_loc_conf_t *clcf;
+ ngx_http_script_len_code_pt lcode;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "try files phase: %ui", r->phase_handler);
+
+ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
+ if (clcf->try_files == NULL) {
+ r->phase_handler++;
+ return NGX_AGAIN;
+ }
+
+ allocated = 0;
+ root = 0;
+ name = NULL;
+ /* suppress MSVC warning */
+ path.data = NULL;
+
+ tf = clcf->try_files;
+
+ alias = clcf->alias ? clcf->name.len : 0;
+
+ for ( ;; ) {
+
+ if (tf->lengths) {
+ ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
+
+ e.ip = tf->lengths->elts;
+ e.request = r;
+
+ /* 1 is for terminating '\0' as in static names */
+ len = 1;
+
+ while (*(uintptr_t *) e.ip) {
+ lcode = *(ngx_http_script_len_code_pt *) e.ip;
+ len += lcode(&e);
+ }
+
+ } else {
+ len = tf->name.len;
+ }
+
+ /* 16 bytes are preallocation */
+ reserve = ngx_abs((ssize_t) (len - r->uri.len)) + alias + 16;
+
+ if (reserve > allocated) {
+
+ /* we just need to allocate path and to copy a root */
+
+ if (ngx_http_map_uri_to_path(r, &path, &root, reserve) == NULL) {
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return NGX_OK;
+ }
+
+ name = path.data + root;
+ allocated = path.len - root - (r->uri.len - alias);
+ }
+
+ if (tf->values == NULL) {
+
+ /* tf->name.len includes the terminating '\0' */
+
+ ngx_memcpy(name, tf->name.data, tf->name.len);
+
+ path.len = (name + tf->name.len - 1) - path.data;
+
+ } else {
+ e.ip = tf->values->elts;
+ e.pos = name;
+ e.flushed = 1;
+
+ while (*(uintptr_t *) e.ip) {
+ code = *(ngx_http_script_code_pt *) e.ip;
+ code((ngx_http_script_engine_t *) &e);
+ }
+
+ path.len = e.pos - path.data;
+
+ *e.pos = '\0';
+
+ if (alias && ngx_strncmp(name, clcf->name.data, alias) == 0) {
+ ngx_memcpy(name, name + alias, len - alias);
+ path.len -= alias;
+ }
+ }
+
+ test_dir = tf->test_dir;
+
+ tf++;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "try to use file: \"%s\"", name);
+
+ if (tf->lengths == NULL && tf->name.len == 0) {
+
+ path.len -= root;
+ path.data += root;
+
+ if (path.data[0] == '@') {
+ (void) ngx_http_named_location(r, &path);
+
+ } else {
+ ngx_http_split_args(r, &path, &args);
+
+ (void) ngx_http_internal_redirect(r, &path, &args);
+ }
+
+ return NGX_OK;
+ }
+
+ ngx_memzero(&of, sizeof(ngx_open_file_info_t));
+
+ of.valid = clcf->open_file_cache_valid;
+ of.min_uses = clcf->open_file_cache_min_uses;
+ of.errors = clcf->open_file_cache_errors;
+ of.events = clcf->open_file_cache_events;
+
+ if (ngx_open_cached_file(clcf->open_file_cache, &path, &of, r->pool)
+ != NGX_OK)
+ {
+ if (of.err != NGX_ENOENT && of.err != NGX_ENOTDIR) {
+ ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
+ ngx_open_file_n " \"%s\" failed", path.data);
+ }
+
+ continue;
+ }
+
+ if (of.is_dir && !test_dir) {
+ continue;
+ }
+
+ path.len -= root;
+ path.data += root;
+
+ if (!alias) {
+ r->uri = path;
+
+ } else {
+ r->uri.len = alias + path.len;
+ r->uri.data = ngx_palloc(r->pool, r->uri.len);
+ if (r->uri.data == NULL) {
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return NGX_OK;
+ }
+
+ p = ngx_copy(r->uri.data, clcf->name.data, alias);
+ ngx_memcpy(p, name, path.len);
+ }
+
+ if (ngx_http_set_exten(r) != NGX_OK) {
+ ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
+ return NGX_OK;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
+ "try file uri: \"%V\"", &r->uri);
+
+ r->phase_handler++;
+ return NGX_AGAIN;
+ }
+
+ /* not reached */
+}
+
+
+ngx_int_t
ngx_http_core_content_phase(ngx_http_request_t *r,
ngx_http_phase_handler_t *ph)
{
@@ -2672,6 +2860,7 @@
* lcf->default_type = { 0, NULL };
* lcf->err_log = NULL;
* lcf->error_pages = NULL;
+ * lcf->try_files = NULL;
* lcf->client_body_path = NULL;
* lcf->regex = NULL;
* lcf->exact_match = 0;
@@ -3588,6 +3777,70 @@
static char *
+ngx_http_core_try_files(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
+{
+ ngx_http_core_loc_conf_t *clcf = conf;
+
+ ngx_str_t *value;
+ ngx_uint_t i, n;
+ ngx_http_try_file_t *tf;
+ ngx_http_script_compile_t sc;
+ ngx_http_core_main_conf_t *cmcf;
+
+ if (clcf->try_files) {
+ return "is duplicate";
+ }
+
+ cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
+
+ cmcf->try_files = 1;
+
+ tf = ngx_pcalloc(cf->pool, cf->args->nelts * sizeof(ngx_http_try_file_t));
+ if (tf == NULL) {
+ return NGX_CONF_ERROR;
+ }
+
+ clcf->try_files = tf;
+
+ value = cf->args->elts;
+
+ for (i = 0; i < cf->args->nelts - 1; i++) {
+
+ tf[i].name = value[i + 1];
+
+ if (tf[i].name.data[tf[i].name.len - 1] == '/') {
+ tf[i].test_dir = 1;
+ tf[i].name.len--;
+ }
+
+ n = ngx_http_script_variables_count(&tf[i].name);
+
+ if (n) {
+ ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
+
+ sc.cf = cf;
+ sc.source = &tf[i].name;
+ sc.lengths = &tf[i].lengths;
+ sc.values = &tf[i].values;
+ sc.variables = n;
+ sc.complete_lengths = 1;
+ sc.complete_values = 1;
+
+ if (ngx_http_script_compile(&sc) != NGX_OK) {
+ return NGX_CONF_ERROR;
+ }
+
+ } else {
+ /* add trailing '\0' to length */
+ tf[i].name.len++;
+ }
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
ngx_http_core_open_file_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_core_loc_conf_t *lcf = conf;
Index: src/http/ngx_http_core_module.h
===================================================================
--- src/http/ngx_http_core_module.h (revision 1891)
+++ src/http/ngx_http_core_module.h (working copy)
@@ -74,6 +74,7 @@
NGX_HTTP_ACCESS_PHASE,
NGX_HTTP_POST_ACCESS_PHASE,
+ NGX_HTTP_TRY_FILES_PHASE,
NGX_HTTP_CONTENT_PHASE,
NGX_HTTP_LOG_PHASE
@@ -122,6 +123,8 @@
ngx_hash_keys_arrays_t *variables_keys;
+ ngx_uint_t try_files; /* unsigned try_files:1 */
+
ngx_http_phase_t phases[NGX_HTTP_LOG_PHASE + 1];
} ngx_http_core_main_conf_t;
@@ -231,6 +234,14 @@
} ngx_http_err_page_t;
+typedef struct {
+ ngx_array_t *lengths;
+ ngx_array_t *values;
+ ngx_str_t name;
+ ngx_uint_t test_dir; /* unsigned test_dir:1; */
+} ngx_http_try_file_t;
+
+
typedef struct ngx_http_core_loc_conf_s ngx_http_core_loc_conf_t;
struct ngx_http_core_loc_conf_s {
@@ -319,6 +330,7 @@
#endif
ngx_array_t *error_pages; /* error_page */
+ ngx_http_try_file_t *try_files; /* try_files */
ngx_path_t *client_body_temp_path; /* client_body_temp_path */
@@ -350,6 +362,8 @@
ngx_http_phase_handler_t *ph);
ngx_int_t ngx_http_core_post_access_phase(ngx_http_request_t *r,
ngx_http_phase_handler_t *ph);
+ngx_int_t ngx_http_core_try_files_phase(ngx_http_request_t *r,
+ ngx_http_phase_handler_t *ph);
ngx_int_t ngx_http_core_content_phase(ngx_http_request_t *r,
ngx_http_phase_handler_t *ph);
Index: src/http/ngx_http_parse.c
===================================================================
--- src/http/ngx_http_parse.c (revision 1891)
+++ src/http/ngx_http_parse.c (working copy)
@@ -1467,3 +1467,37 @@
return NGX_DECLINED;
}
+
+
+void
+ngx_http_split_args(ngx_http_request_t *r, ngx_str_t *uri, ngx_str_t *args)
+{
+ u_char ch, *p, *last;
+
+ p = uri->data;
+
+ last = p + uri->len;
+
+ while (p < last) {
+
+ ch = *p++;
+
+ if (ch == '?') {
+ args->len = last - p;
+ args->data = p;
+
+ uri->len = p - 1 - uri->data;
+
+ if (ngx_strlchr(p, last, '\0') != NULL) {
+ r->zero_in_uri = 1;
+ }
+
+ return;
+ }
+
+ if (ch == '\0') {
+ r->zero_in_uri = 1;
+ continue;
+ }
+ }
+}
Index: src/http/ngx_http.c
===================================================================
--- src/http/ngx_http.c (revision 1891)
+++ src/http/ngx_http.c (working copy)
@@ -406,7 +406,7 @@
use_rewrite = cmcf->phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0;
use_access = cmcf->phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0;
- n = use_rewrite + use_access + 1; /* find config phase */
+ n = use_rewrite + use_access + cmcf->try_files + 1 /* find config phase */;
for (i = 0; i < NGX_HTTP_LOG_PHASE; i++) {
n += cmcf->phases[i].handlers.nelts;
@@ -475,6 +475,15 @@
continue;
+ case NGX_HTTP_TRY_FILES_PHASE:
+ if (cmcf->try_files) {
+ ph->checker = ngx_http_core_try_files_phase;
+ n++;
+ ph++;
+ }
+
+ continue;
+
case NGX_HTTP_CONTENT_PHASE:
checker = ngx_http_core_content_phase;
break;
Index: src/core/ngx_string.h
===================================================================
--- src/core/ngx_string.h (revision 1891)
+++ src/core/ngx_string.h (working copy)
@@ -52,10 +52,26 @@
#define ngx_strstr(s1, s2) strstr((const char *) s1, (const char *) s2)
-#define ngx_strchr(s1, c) strchr((const char *) s1, (int) c)
#define ngx_strlen(s) strlen((const char *) s)
+#define ngx_strchr(s1, c) strchr((const char *) s1, (int) c)
+static ngx_inline u_char *
+ngx_strlchr(u_char *p, u_char *last, u_char c)
+{
+ while (p < last) {
+
+ if (*p == c) {
+ return p;
+ }
+
+ p++;
+ }
+
+ return NULL;
+}
+
+
/*
* msvc and icc7 compile memset() to the inline "rep stos"
* while ZeroMemory() and bzero() are the calls.
More information about the nginx
mailing list