0

The NGINX conf bellow is not working on nginx -t:

location ~ ^/ETC[0-9](?:\.json|\.(?<ext>csv))?/?$ {
  if ($ext) {
     set $extOpt  '&csv=1'; 
     proxy_set_header Accept 'text/csv'; # BUG HERE!
  }
  rewrite ^/ETC1               /rpc/etc?q=v1$extOpt;     break;
  rewrite ^/ETC[2-9]           /rpc/etc?q=other$extOpt;  break;
  proxy_pass http://localhost:3002;
}

nginx: [emerg] "proxy_set_header" directive is not allowed here in /etc/nginx/...:17

nginx: configuration file /etc/nginx/nginx.conf test failed

I not see where the problem (see e. g. nginx/wiki)... Well, if the if is not valid, there are a way to express conditional proxy_set_header?


NOTES

  • Not make sense because when using direct proxy_set_header Accept 'text/csv'; instead it in the if ($ext) block, it works fine.

  • It is similar but not a duplicate of this question/answer.

Peter Krauss
  • 13,174
  • 24
  • 167
  • 304
  • 1
    The `proxy_set_header` directive is not allowed within an `if` block, only the `http`, `server`, and `location` blocks. See [this document](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header) and notice the "Context" line in the summary. – Richard Smith Apr 04 '20 at 18:40
  • Thanks @RichardSmith, seems the answer (the "context" show syntax at guide)!... So, no way to **conditional** `proxy_set_header `? – Peter Krauss Apr 04 '20 at 19:25
  • 1
    Use a `map`. See [this answer](https://stackoverflow.com/questions/35845155/set-custom-header-in-nginx-config-and-pass-it-to-gunicorn/35846083#35846083). – Richard Smith Apr 04 '20 at 19:39

0 Answers0