0

I am trying to set proxy_pass based on $host value, I tried this:

location /api/v1/ {
    if ($host = .*\.npreprod.company.com)
    {
            proxy_pass https://frontend-api.preprod.company.com/api/v1/;
    }
}

however, I got the error

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/nuxtjs.conf:45

so, what's the proper way to achieve this?

Gerald Schneider
  • 19,757
  • 8
  • 52
  • 79
simo
  • 150
  • 9
  • Does this answer your question? [Nginx location regex doesn't work with proxy pass](https://serverfault.com/questions/649151/nginx-location-regex-doesnt-work-with-proxy-pass) – AlexD Jan 03 '22 at 11:01
  • I don't think this is the correct duplicate. I'm pretty sure we already have one or more questions addressing the [if is evil problem](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/), but I can't find one right now. – Gerald Schneider Jan 03 '22 at 11:16
  • @AlexD I am not sure how would I apply the answer at that question in my case – simo Jan 03 '22 at 11:26
  • @GeraldSchneider this one describes how to match a hostname better https://serverfault.com/questions/286828/nginx-matching-server-host-name-in-location-directive – AlexD Jan 03 '22 at 11:43
  • 1
    Rather than testing the value of `$host` inside the `server` block, you could create a separate `server` block. See [this document](http://nginx.org/en/docs/http/server_names.html). – Richard Smith Jan 03 '22 at 12:34
  • 1
    Thanks@AlexD , yes, that's what I ended doing, it worked well. – simo Jan 03 '22 at 13:15

0 Answers0