diff --git a/src/ngx_http_lua_headers_out.c b/src/ngx_http_lua_headers_out.c
index c51146a3fc..1424b379eb 100644
--- a/src/ngx_http_lua_headers_out.c
+++ b/src/ngx_http_lua_headers_out.c
@@ -319,62 +319,40 @@ ngx_http_set_builtin_multi_header(ngx_http_request_t *r,
 
     headers = (ngx_table_elt_t **) ((char *) &r->headers_out + hv->offset);
 
-    if (hv->no_override) {
-        for (h = *headers; h; h = h->next) {
-            if (!h->hash) {
-                h->value = *value;
-                h->hash = hv->hash;
-                return NGX_OK;
-            }
-        }
-
-        goto create;
-    }
-
-    /* override old values (if any) */
-
-    if (*headers) {
-        for (h = (*headers)->next; h; h = h->next) {
+    for (h = *headers; h; h = h->next) {
+        if (!hv->no_override) {
             h->hash = 0;
             h->value.len = 0;
         }
+    }
 
-        h = *headers;
-
+    if (h->hash == 0) {
+        // update the last element if possible
         h->value = *value;
-
-        if (value->len == 0) {
-            h->hash = 0;
-
-        } else {
+        if (value->len != 0) {
             h->hash = hv->hash;
         }
 
-        return NGX_OK;
-    }
-
-create:
-
-    for (ph = headers; *ph; ph = &(*ph)->next) { /* void */ }
+    } else {
+        ho = ngx_list_push(&r->headers_out.headers);
+        if (ho == NULL) {
+            return NGX_ERROR;
+        }
 
-    ho = ngx_list_push(&r->headers_out.headers);
-    if (ho == NULL) {
-        return NGX_ERROR;
-    }
+        ho->value = *value;
 
-    ho->value = *value;
+        if (value->len == 0) {
+            ho->hash = 0;
 
-    if (value->len == 0) {
-        ho->hash = 0;
+        } else {
+            ho->hash = hv->hash;
+        }
 
-    } else {
-        ho->hash = hv->hash;
+        ho->key = hv->key;
+        ho->next = NULL;
+        h->next = ho;
     }
 
-    ho->key = hv->key;
-    ho->next = NULL;
-    *ph = ho;
-
     return NGX_OK;
 #else
     ngx_array_t      *pa;