500 Error when using proxy_cookie_domain

Hello, I’m using Nginx as a reverse proxy and I’m seeing 500 errors
anytime the proxy_cookie_domain option feature is used. I’m trying to
change the domain in the “Set-Cookie” header.

The response header shows the replaced domain properly in the
“Set-Cookie” header, however, it is a 500 Error response and does not
output the page content.

Is there any way to find the cause of this error? I tried using both
strace and the debug log, but could not find any cause.

Here is a snippet of the debug log

2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “User-Agent:
curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b
zlib/1.2.3 libidn/0.6.5”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Accept:
/
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
"GET /test HTTP/1.0
Host: server.com
Connection: close
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Accept: /

"
2012/05/18 13:16:10 [debug] 31932#0: *1 http cleanup add: 09030628
2012/05/18 13:16:10 [debug] 31932#0: *1 get rr peer, try: 1
2012/05/18 13:16:10 [debug] 31932#0: *1 socket 12
2012/05/18 13:16:10 [debug] 31932#0: *1 epoll add connection: fd:12
ev:80000005
2012/05/18 13:16:10 [debug] 31932#0: *1 connect to 194.71.107.15:80,
fd:12 #2
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream connect: -2
2012/05/18 13:16:10 [debug] 31932#0: *1 posix_memalign: 0902A140:128
@16
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer add: 12:
60000:1626601550
2012/05/18 13:16:10 [debug] 31932#0: *1 http finalize request: -4,
“/test” a:1, c:2
2012/05/18 13:16:10 [debug] 31932#0: *1 http request count:2 blk:0
2012/05/18 13:16:10 [debug] 31932#0: *1 http run request: “/test”
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream check client,
write event:1, “/test”
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream recv(): -1 (11:
Resource temporarily unavailable)
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream request: “/test”
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream send request
handler
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream send request
2012/05/18 13:16:10 [debug] 31932#0: *1 chain writer buf fl:1 s:278
2012/05/18 13:16:10 [debug] 31932#0: *1 chain writer in: 09030644
2012/05/18 13:16:10 [debug] 31932#0: *1 writev: 278
2012/05/18 13:16:10 [debug] 31932#0: *1 chain writer out: 00000000
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer del: 12: 1626601550
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer add: 12:
60000:1626601723
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream request: “/test”
2012/05/18 13:16:10 [debug] 31932#0: *1 http upstream process header
2012/05/18 13:16:10 [debug] 31932#0: *1 malloc: 09030A88:4096
2012/05/18 13:16:10 [debug] 31932#0: *1 recv: fd:12 2920 of 3979
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy status 200 “200 OK”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
“X-Powered-By: PHP/5.4.1”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Set-Cookie:
PHPSESSID=055997276de6833c13c98c8707581925; path=/; domain=.server.com
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Set-Cookie:
language=en_EN; expires=Sat, 18-May-2013 17:16:10 GMT; path=/;
domain=.server.com
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Set-Cookie:
showArtist=1; expires=Thu, 13-May-2032 17:16:10 GMT”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Expires:
Mon, 26 Jul 1997 05:00:00 GMT”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
“Last-Modified: Fri, 18 May 2012 17:16:10 GMT”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
“Cache-Control: no-store, no-cache, must-revalidate”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
“Cache-Control: post-check=0, pre-check=0”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Pragma:
no-cache”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header:
“Content-Type: text/html; charset=UTF-8”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Connection:
close”
2012/05/18 13:16:10 [debug] 31932#0: *1 posix_memalign: 0907E4E0:4096
@16
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Date: Fri,
18 May 2012 17:16:10 GMT”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header: “Server:
lighttpd”
2012/05/18 13:16:10 [debug] 31932#0: *1 http proxy header done
2012/05/18 13:16:10 [debug] 31932#0: *1 finalize http upstream request:
500
2012/05/18 13:16:10 [debug] 31932#0: *1 finalize http proxy request
2012/05/18 13:16:10 [debug] 31932#0: *1 free rr peer 1 0
2012/05/18 13:16:10 [debug] 31932#0: *1 close http upstream connection:
12
2012/05/18 13:16:10 [debug] 31932#0: *1 free: 0902A140, unused: 88
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer del: 12: 1626601723
2012/05/18 13:16:10 [debug] 31932#0: *1 reusable connection: 0
2012/05/18 13:16:10 [debug] 31932#0: *1 http file cache free, fd: -1
2012/05/18 13:16:10 [debug] 31932#0: *1 http finalize request: 500,
“/test” a:1, c:1
2012/05/18 13:16:10 [debug] 31932#0: *1 http special response: 500,
“/test”
2012/05/18 13:16:10 [debug] 31932#0: *1 http set discard body
2012/05/18 13:16:10 [debug] 31932#0: *1 http subs filter header ignored,
this may be special or compressed response
2012/05/18 13:16:10 [debug] 31932#0: *1 HTTP/1.1 500 Internal Server
Error
Server: nginx/1.3.0
Date: Fri, 18 May 2012 17:16:10 GMT
Content-Type: text/html
Content-Length: 192
Connection: close
X-Powered-By: PHP/5.4.1
Set-Cookie: PHPSESSID=055997276de6833c13c98c8707581925; path=/;
domain=.mydomain.com
Set-Cookie: language=en_EN; expires=Sat, 18-May-2013 17:16:10 GMT;
path=/; domain=.mydomain.com
Set-Cookie: showArtist=1; expires=Thu, 13-May-2032 17:16:10 GMT

2012/05/18 13:16:10 [debug] 31932#0: *1 write new buf t:1 f:0 0907E564,
pos 0907E564, size: 436 file: 0, size: 0
2012/05/18 13:16:10 [debug] 31932#0: *1 http write filter: l:1 f:0
s:436
2012/05/18 13:16:10 [debug] 31932#0: *1 http write filter limit 0
2012/05/18 13:16:10 [debug] 31932#0: *1 writev: 436
2012/05/18 13:16:10 [debug] 31932#0: *1 http write filter 00000000
2012/05/18 13:16:10 [debug] 31932#0: *1 http finalize request: 0,
“/test” a:1, c:1
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer add: 11:
5000:1626546906
2012/05/18 13:16:10 [debug] 31932#0: *1 http lingering close handler
2012/05/18 13:16:10 [debug] 31932#0: *1 recv: fd:11 -1 of 4096
2012/05/18 13:16:10 [debug] 31932#0: *1 recv() not ready (11: Resource
temporarily unavailable)
2012/05/18 13:16:10 [debug] 31932#0: *1 lingering read: -2
2012/05/18 13:16:10 [debug] 31932#0: *1 event timer: 11, old:
1626546906, new: 1626546906
2012/05/18 13:16:10 [debug] 31932#0: *1 http lingering close handler
2012/05/18 13:16:10 [debug] 31932#0: *1 recv: fd:11 0 of 4096
2012/05/18 13:16:10 [debug] 31932#0: *1 lingering read: 0
2012/05/18 13:16:10 [debug] 31932#0: *1 http request count:1 blk:0
2012/05/18 13:16:10 [debug] 31932#0: *1 http close request
2012/05/18 13:16:10 [debug] 31932#0: *1 http log handler

Any help is appreciated!

Posted at Nginx Forum:

Also, this issue is occuring on both Nginx 1.3.0 and 1.2.0. I am using
1 extra module, substitutions4nginx.

Posted at Nginx Forum:

On Friday 18 May 2012 21:25:41 omar_h wrote:

Here is a snippet of the debug log

[…]

Could you provide your config?

wbr, Valentin V. Bartenev

On Friday 18 May 2012 23:38:50 Valentin V. Bartenev wrote:

strace and the debug log, but could not find any cause.

Here is a snippet of the debug log

[…]

Could you provide your config?

Never mind. I am able to reproduce the problem and going to fix it soon.

wbr, Valentin V. Bartenev

On Friday 18 May 2012 21:25:41 omar_h wrote:

Here is a snippet of the debug log

The patch below should resolve this issue. Could you please test it?

Index: src/http/ngx_http_upstream.c

— src/http/ngx_http_upstream.c (revision 4642)
+++ src/http/ngx_http_upstream.c (working copy)
@@ -3677,6 +3677,7 @@ static ngx_int_t
ngx_http_upstream_rewrite_set_cookie(ngx_http_request_t *r,
ngx_table_elt_t *h,
ngx_uint_t offset)
{

  • ngx_int_t rc;
    ngx_table_elt_t *ho;

    ho = ngx_list_push(&r->headers_out.headers);
    @@ -3687,7 +3688,8 @@ ngx_http_upstream_rewrite_set_cookie(ngx_http_requ
    *ho = *h;

    if (r->upstream->rewrite_cookie) {

  •    return r->upstream->rewrite_cookie(r, ho);
    
  •    rc = r->upstream->rewrite_cookie(r, ho);
    
  •    return (rc == NGX_DECLINED) ? NGX_OK : rc;
    

    }

    return NGX_OK;

The patch works!

Thank you very much.

Posted at Nginx Forum: