Proper way to redirect from http to https w/query string notifier

i need to redirecting from http to https, and append a “source”
attribute for tracking (we’re trying to figure out how the wrong
requests are coming in)

this seems to work:

  if ($query_string){
    return 301 https://$host$request_uri&source=server1 ;
  }
  return 301 https://$host$request_uri?source=server1 ;

I’m just wondering if there is a more appropriate way

On Tue, Mar 24, 2015 at 11:33:41AM -0400, Jonathan V. wrote:

Hi there,

  if ($query_string){
    return 301 https://$host$request_uri&source=server1 ;
  }
  return 301 https://$host$request_uri?source=server1 ;

I’m just wondering if there is a more appropriate way

If your backend will accept /request?source=server1 and
/request?&source=server1 as being equivalent, then you could use the
$is_args variable and just always

return 301 https://$host$request_uri$is_args&source=server1;

f

Francis D. [email protected]

On Wed, Mar 25, 2015 at 2:32 AM, Francis D. [email protected]
wrote:

If your backend will accept /request?source=server1 and
/request?&source=server1 as being equivalent, then you could use the
$is_args variable and just always

return 301 https://$host$request_uri$is_args&source=server1;

that looks wrong since when there’s argument:

$request_uri: /path/name?arg=uments
$is_args: ?

whereas when there’s no argument:

$request_uri: /path/name
$is_args:

(now imagine when your return is used)
.

One possible solution would be just $host$uri?source=server1&$args

<note: untested>

On Wed, Mar 25, 2015 at 02:50:29AM +0900, Edho A. wrote:

On Wed, Mar 25, 2015 at 2:32 AM, Francis D. [email protected] wrote:

Hi there,

If your backend will accept /request?source=server1 and
/request?&source=server1 as being equivalent, then you could use the
$is_args variable and just always

return 301 https://$host$request_uri$is_args&source=server1;

that looks wrong since when there’s argument:

Ah, yes, you are correct - I had it backwards.

I guess one could set something using “map” to be “?” if $is_args is
empty and “&” if $is_args is ?, and build a correct “return” line from
that – but the original “if ($query_string)” is probably simpler at
that point.

One possible solution would be just $host$uri?source=server1&$args

<note: untested>

That will work in the common case, but $uri has been percent-unescaped
so may not be suitable to send as-is.

Thanks,

f

Francis D. [email protected]

On Mar 24, 2015, at 2:10 PM, Gena M. wrote:

Probably you can do such tracking just looking at Referer request header

Long story short - we actually are doing that. This is just to get
stats into the HTTPS log analyzer, which is a different system and much
easier for us to deploy changes to.

On 24.03.2015 17:33, Jonathan V. wrote:

i need to redirecting from http to https,
and append a “source” attribute for tracking
(we’re trying to figure out how the wrong requests are coming in)

Probably you can do such tracking just looking at Referer request header

this seems to work:
if ($query_string){
return 301 https://$host$request_uri&source=server1 ;
}
return 301 https://$host$request_uri?source=server1 ;

I’m just wondering if there is a more appropriate way

Yes, you can use $uri variable and rewrite directive for this:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
| If a replacement string includes the new request arguments,
| the previous request arguments are appended after them.

rewrite ^ https://$host$uri?source=server1 permanent;


Best regards,
Gena

On Mar 24, 2015, at 3:26 PM, Francis D. wrote:

but the original “if ($query_string)” is probably simpler at
that point.

thanks for the help! it’s great having a second set of eyes on this!