Server sent unexpected return value (502 Bad Gateway) in response to COPY

This was an annoying issue. I initially blamed it on an Apache update and wasted a lot of time going in that direction. When I finally gave up and used Google, I got somewhere.

Thankfully, I wasn’t the only person to run into this and there are at least three different solutions to the problem. Take your pick.

It was explained rather succinctly here:

because nginx pass to backend https:// url in Destination header.
but http:// and https:// urls can not be mixed in COPY request.

In short, the front end speaks https to the client, but it speaks http (non-ssl) to the back end. The header is not changed to reflect this and the COPY command fails.

  1. Rewrite the Destination header via nginx’s rewrite feature:

    set $fixed_destination $http_destination;
    if ( $http_destination ~* ^https(.*)$ )
    set $fixed_destination http$1;

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Destination $fixed_destination;
    proxy_pass http://subversion_hosts;

  2. Have Apache also use SSL (I was using this approach until just recently)
  3. Use mod_headers with Apache.