-p and --prefix=
diff --git a/src/core/nginx.c b/src/core/nginx.c
index bf2f38d..82efda2 100644
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -11,7 +11,7 @@
 
 static ngx_int_t ngx_add_inherited_sockets(ngx_cycle_t *cycle);
 static ngx_int_t ngx_get_options(int argc, char *const *argv);
-static void ngx_process_options(ngx_cycle_t *cycle);
+static ngx_int_t ngx_process_options(ngx_cycle_t *cycle);
 static ngx_int_t ngx_save_argv(ngx_cycle_t *cycle, int argc, char *const *argv);
 static void *ngx_core_module_create_conf(ngx_cycle_t *cycle);
 static char *ngx_core_module_init_conf(ngx_cycle_t *cycle, void *conf);
@@ -186,6 +186,7 @@
 static ngx_uint_t   ngx_show_help;
 static ngx_uint_t   ngx_show_version;
 static ngx_uint_t   ngx_show_configure;
+static u_char      *ngx_prefix;
 static u_char      *ngx_conf_file;
 static u_char      *ngx_conf_params;
 static char        *ngx_signal;
@@ -221,6 +222,12 @@
                 "  -t            : test configuration and exit" CRLF
                 "  -s signal     : send signal to a master process: "
                                    "stop, quit, reopen, reload" CRLF
+#ifdef NGX_PREFIX
+                "  -p prefix     : set prefix path (default: "
+                                   NGX_PREFIX ")" CRLF
+#else
+                "  -p prefix     : set prefix path (default: NONE)" CRLF
+#endif
                 "  -c filename   : set configuration file (default: "
                                    NGX_CONF_PATH ")" CRLF
                 "  -g directives : set global directives out of configuration "
@@ -254,7 +261,7 @@
 
     ngx_pid = ngx_getpid();
 
-    log = ngx_log_init();
+    log = ngx_log_init(ngx_prefix);
     if (log == NULL) {
         return 1;
     }
@@ -282,7 +289,9 @@
         return 1;
     }
 
-    ngx_process_options(&init_cycle);
+    if (ngx_process_options(&init_cycle) != NGX_OK) {
+        return 1;
+    }
 
     if (ngx_os_init(log) != NGX_OK) {
         return 1;
@@ -662,6 +671,20 @@
                 ngx_test_config = 1;
                 break;
 
+            case 'p':
+                if (*p) {
+                    ngx_prefix = p;
+                    goto next;
+                }
+
+                if (argv[++i]) {
+                    ngx_prefix = (u_char *) argv[i];
+                    goto next;
+                }
+
+                ngx_log_stderr(0, "option \"-p\" requires directory name");
+                return NGX_ERROR;
+
             case 'c':
                 if (*p) {
                     ngx_conf_file = p;
@@ -771,9 +794,69 @@
 }
 
 
-static void
+static ngx_int_t
 ngx_process_options(ngx_cycle_t *cycle)
 {
+    u_char  *p;
+    size_t   len;
+
+    if (ngx_prefix) {
+        len = ngx_strlen(ngx_prefix);
+        p = ngx_prefix;
+
+        if (!ngx_path_separator(*p)) {
+            p = ngx_pnalloc(cycle->pool, len + 1);
+            if (p == NULL) {
+                return NGX_ERROR;
+            }
+
+            ngx_memcpy(p, ngx_prefix, len);
+            p[len++] = '/';
+        }
+
+        cycle->conf_prefix.len = len;
+        cycle->conf_prefix.data = p;
+        cycle->prefix.len = len;
+        cycle->prefix.data = p;
+
+    } else {
+
+#ifndef NGX_PREFIX
+
+        p = ngx_pnalloc(cycle->pool, NGX_MAX_PATH);
+        if (p == NULL) {
+            return NGX_ERROR;
+        }
+
+        if (ngx_getcwd(p, NGX_MAX_PATH) == 0) {
+            ngx_log_stderr(ngx_errno, "[emerg]: " ngx_getcwd_n " failed");
+            return NGX_ERROR;
+        }
+
+        len = ngx_strlen(p);
+
+        p[len++] = '/';
+
+        cycle->conf_prefix.len = len;
+        cycle->conf_prefix.data = p;
+        cycle->prefix.len = len;
+        cycle->prefix.data = p;
+
+#else
+
+#ifdef NGX_CONF_PREFIX
+        cycle->conf_prefix.len = sizeof(NGX_CONF_PREFIX) - 1;
+        cycle->conf_prefix.data = (u_char *) NGX_CONF_PREFIX;
+#else
+        cycle->conf_prefix.len = sizeof(NGX_PREFIX) - 1;
+        cycle->conf_prefix.data = (u_char *) NGX_PREFIX;
+#endif
+        cycle->prefix.len = sizeof(NGX_PREFIX) - 1;
+        cycle->prefix.data = (u_char *) NGX_PREFIX;
+
+#endif
+    }
+
     if (ngx_conf_file) {
         cycle->conf_file.len = ngx_strlen(ngx_conf_file);
         cycle->conf_file.data = ngx_conf_file;
@@ -783,6 +866,21 @@
         cycle->conf_file.data = (u_char *) NGX_CONF_PATH;
     }
 
+    if (ngx_conf_full_name(cycle, &cycle->conf_file, 0) != NGX_OK) {
+        return NGX_ERROR;
+    }
+
+    for (p = cycle->conf_file.data + cycle->conf_file.len - 1;
+         p > cycle->conf_file.data;
+         p--)
+    {
+        if (ngx_path_separator(*p)) {
+            cycle->conf_prefix.len = p - ngx_cycle->conf_file.data + 1;
+            cycle->conf_prefix.data = ngx_cycle->conf_file.data;
+            break;
+        }
+    }
+
     if (ngx_conf_params) {
         cycle->conf_param.len = ngx_strlen(ngx_conf_params);
         cycle->conf_param.data = ngx_conf_params;
@@ -791,6 +889,8 @@
     if (ngx_test_config) {
         cycle->log->log_level = NGX_LOG_INFO;
     }
+
+    return NGX_OK;
 }
 
 
diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c
index 9b0f67c..ea35515 100644
--- a/src/core/ngx_conf_file.c
+++ b/src/core/ngx_conf_file.c
@@ -798,10 +798,6 @@
     u_char     *p, *prefix;
     ngx_str_t   old;
 
-    if (name->data[0] == '/') {
-        return NGX_OK;
-    }
-
 #if (NGX_WIN32)
 
     if (name->len > 2
@@ -812,17 +808,23 @@
         return NGX_OK;
     }
 
+#else
+
+    if (name->data[0] == '/') {
+        return NGX_OK;
+    }
+
 #endif
 
     old = *name;
 
     if (conf_prefix) {
-        len = sizeof(NGX_CONF_PREFIX) - 1;
-        prefix = (u_char *) NGX_CONF_PREFIX;
+        len = cycle->conf_prefix.len;
+        prefix = cycle->conf_prefix.data;
 
     } else {
-        len = cycle->root.len;
-        prefix = cycle->root.data;
+        len = cycle->prefix.len;
+        prefix = cycle->prefix.data;
     }
 
     name->len = len + old.len;
@@ -851,7 +853,7 @@
     full.data = NULL;
 #endif
 
-    if (name) {
+    if (name && name->len) {
         full = *name;
 
         if (ngx_conf_full_name(cycle, &full, 0) != NGX_OK) {
@@ -887,7 +889,7 @@
         return NULL;
     }
 
-    if (name) {
+    if (name && name->len) {
         file->fd = NGX_INVALID_FILE;
         file->name = full;
 
diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
index 0c0fd8a..10d0771 100644
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -34,11 +34,7 @@
 static ngx_connection_t  dumb;
 /* STUB */
 
-#ifdef NGX_ERROR_LOG_PATH
 static ngx_str_t  error_log = ngx_string(NGX_ERROR_LOG_PATH);
-#else
-static ngx_str_t  error_log = ngx_null_string;
-#endif
 
 
 ngx_cycle_t *
@@ -87,9 +83,20 @@
     cycle->pool = pool;
     cycle->log = log;
     cycle->old_cycle = old_cycle;
-    cycle->root.len = sizeof(NGX_PREFIX) - 1;
-    cycle->root.data = (u_char *) NGX_PREFIX;
 
+    cycle->conf_prefix.len = old_cycle->conf_prefix.len;
+    cycle->conf_prefix.data = ngx_pstrdup(pool, &old_cycle->conf_prefix);
+    if (cycle->conf_prefix.data == NULL) {
+        ngx_destroy_pool(pool);
+        return NULL;
+    }
+
+    cycle->prefix.len = old_cycle->prefix.len;
+    cycle->prefix.data = ngx_pstrdup(pool, &old_cycle->prefix);
+    if (cycle->prefix.data == NULL) {
+        ngx_destroy_pool(pool);
+        return NULL;
+    }
 
     cycle->conf_file.len = old_cycle->conf_file.len;
     cycle->conf_file.data = ngx_pnalloc(pool, old_cycle->conf_file.len + 1);
@@ -100,15 +107,12 @@
     ngx_cpystrn(cycle->conf_file.data, old_cycle->conf_file.data,
                 old_cycle->conf_file.len + 1);
 
-
     cycle->conf_param.len = old_cycle->conf_param.len;
-    cycle->conf_param.data = ngx_pnalloc(pool, old_cycle->conf_param.len);
+    cycle->conf_param.data = ngx_pstrdup(pool, &old_cycle->conf_param);
     if (cycle->conf_param.data == NULL) {
         ngx_destroy_pool(pool);
         return NULL;
     }
-    ngx_memcpy(cycle->conf_param.data, old_cycle->conf_param.data,
-               old_cycle->conf_param.len);
 
 
     n = old_cycle->pathes.nelts ? old_cycle->pathes.nelts : 10;
@@ -162,14 +166,12 @@
     }
 
 
-    cycle->new_log = ngx_log_create_errlog(cycle, NULL);
+    cycle->new_log = ngx_log_create_errlog(cycle, &error_log);
     if (cycle->new_log == NULL) {
         ngx_destroy_pool(pool);
         return NULL;
     }
 
-    cycle->new_log->file->name = error_log;
-
 
     n = old_cycle->listening.nelts ? old_cycle->listening.nelts : 10;
 
@@ -350,7 +352,7 @@
             i = 0;
         }
 
-        if (file[i].name.data == NULL) {
+        if (file[i].name.len == 0) {
             continue;
         }
 
@@ -1083,7 +1085,7 @@
             i = 0;
         }
 
-        if (file[i].name.data == NULL) {
+        if (file[i].name.len == 0) {
             continue;
         }
 
diff --git a/src/core/ngx_cycle.h b/src/core/ngx_cycle.h
index 6ce2974..b4007d4 100644
--- a/src/core/ngx_cycle.h
+++ b/src/core/ngx_cycle.h
@@ -60,7 +60,8 @@
 
     ngx_str_t                 conf_file;
     ngx_str_t                 conf_param;
-    ngx_str_t                 root;
+    ngx_str_t                 conf_prefix;
+    ngx_str_t                 prefix;
     ngx_str_t                 lock_file;
     ngx_str_t                 hostname;
 };
diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c
index 83bcbb7..89df2d7 100644
--- a/src/core/ngx_log.c
+++ b/src/core/ngx_log.c
@@ -273,38 +273,84 @@
 
 
 ngx_log_t *
-ngx_log_init(void)
+ngx_log_init(u_char *prefix)
 {
+    u_char  *p, *name;
+    size_t   nlen, plen;
+
     ngx_log.file = &ngx_log_file;
     ngx_log.log_level = NGX_LOG_NOTICE;
 
+    name = (u_char *) NGX_ERROR_LOG_PATH;
+
     /*
      * we use ngx_strlen() here since BCC warns about
      * condition is always false and unreachable code
      */
 
-    if (ngx_strlen(NGX_ERROR_LOG_PATH) == 0) {
+    nlen = ngx_strlen(name);
+
+    if (nlen == 0) {
         ngx_log_file.fd = ngx_stderr;
         return &ngx_log;
     }
 
-    ngx_log_file.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH,
-                                    NGX_FILE_APPEND,
+    p = NULL;
+
+#if (NGX_WIN32)
+    if (name[1] != ':') {
+#else
+    if (name[0] != '/') {
+#endif
+        plen = 0;
+
+        if (prefix) {
+            plen = ngx_strlen(prefix);
+
+#ifdef NGX_PREFIX
+        } else {
+            prefix = (u_char *) NGX_PREFIX;
+            plen = ngx_strlen(prefix);
+#endif
+        }
+
+        if (plen) {
+            name = malloc(plen + nlen + 2);
+            if (name == NULL) {
+                return NULL;
+            }
+
+            p = ngx_cpymem(name, prefix, plen);
+
+            if (!ngx_path_separator(*(p - 1))) {
+                *p++ = '/';
+            }
+
+            ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
+
+            p = name;
+        }
+    }
+
+    ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
                                     NGX_FILE_CREATE_OR_OPEN,
                                     NGX_FILE_DEFAULT_ACCESS);
 
     if (ngx_log_file.fd == NGX_INVALID_FILE) {
         ngx_log_stderr(ngx_errno,
-                       "[emerg]: could not open error log file: "
-                       ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
-
+                       "[alert]: could not open error log file: "
+                       ngx_open_file_n " \"%s\" failed", name);
 #if (NGX_WIN32)
         ngx_event_log(ngx_errno,
                        "could not open error log file: "
-                       ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
+                       ngx_open_file_n " \"%s\" failed", name);
 #endif
 
-        return NULL;
+        ngx_log_file.fd = ngx_stderr;
+    }
+
+    if (p) {
+        ngx_free(p);
     }
 
     return &ngx_log;
diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h
index cd719f3..0785783 100644
--- a/src/core/ngx_log.h
+++ b/src/core/ngx_log.h
@@ -195,7 +195,7 @@
 
 /*********************************/
 
-ngx_log_t *ngx_log_init(void);
+ngx_log_t *ngx_log_init(u_char *prefix);
 ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_str_t *name);
 char *ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log);
 void ngx_cdecl ngx_log_abort(ngx_err_t err, const char *fmt, ...);