1

Hello So I have already running setup of node server(underly express js), a php server, nginx layer to mask both of them and cloudfront as cdn.

Earlier I used to serve the home page(location /) from php but now I wish to distribute the load amongst both server for some AB testing.

I plan to do this AB testing using a cookie, All my request default route is now node server and depending on the value of that I perform a redirect or do next in a middleware.

The problem is I redirect with setting a response header but the header is either not read or passed to nginx hence I am stuck.

My express middleware looks like

const { path, cookie } = req;

    if (
        path === '/' && cookie === 'something'

    ) {
        const expirationDate = new Date(Date.now() + 60000);
        res.setHeader('X-Php-Redirect', '1');
        return res.redirect(StatusCode.TemporaryRedirect, '/');
    }

    return next();

And in nginx my setup I tried is

location / {
          rewrite                     ^([^.]*[^/])$ $1/ permanent;
          proxy_ssl_server_name       on;
          proxy_set_header            Host $host;
          proxy_set_header            X-Forwarded-For   $proxy_add_x_forwarded_for;
          proxy_set_header            X-Forwarded-Proto $scheme;
          proxy_set_header            X-Forwarded-Host  $host;
          proxy_set_header            X-Forwarded-Port  $server_port;

          if($http_x_php_redirect = '1'){
            proxy_pass                  http://phppath;
          }

          if($upstream_http_x_php_redirect = '1'){
            proxy_pass                  http://phppath;
          }

          if ($someVariable = true) {
            proxy_pass                  http://nodeserver:80;
          }

          proxy_pass                  http://phppath;

          resolver                    10.100.0.10 valid=10s;
        }

This location block is crystal, works under http context and have proper server defined and all. $someVariable is always true because I have configured defaulting to node server

$upstream_http_x_php_redirect and $http_x_php_redirect both are not passing through

Any help appreciated ? I doubt that the response of redirect is upstream or not but still for the sake of what I tried I am not removing the block.

arpit sharma
  • 405
  • 4
  • 11

0 Answers0