Search code examples
nginxelastic-load-balancer

NGINX proxy_pass to ELB with Variable


I am trying to setup a proxy rule for nginx that will hit an internal loadbalancer in front of some node boxes serving an API. I want to use a variable in the address to force it to resolve because of how dynamic the addresses are in aws.

The original setup works fine but occasionally was pointing to an addresss that was no longer there which caused intermittent problems as would be expected:

location /protected/ {
  proxy_pass http://my-internal-aws-loadbalancer.com:8083/protected/;
}

This however produces gives a 404 error when I hit the same route:

location /protected/ {
  set $node "my-internal-aws-loadbalancer";
  proxy_pass http://$node:8083/protected/;
}

Also one thing to note is this is a post request. I guess I am just very confused as to why the addition of the variable breaks this.


Solution

  • So ... after a lot of looking and reading I believe I have this solved. When using a variable in the proxy_pass nginx does in fact not behave the same.

    This article was extremely helpful. TLDR of the whole article in regards to this case is the addition of the variable does cause nginx to behave differently on the proxy_pass so removing the trailing slash from the variable URL and rewriting any thing that could come after it solves the issue.

    location ~ ^/protected/(.*)$ {
      resolver 8.8.8.8;
      set $node "http:///my-internal-aws-loadbalancer.com:8083/";
      proxy_pass $node/protected/$1;
    }