Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't put proxy_set_header inside an if clause?

With this configuration:

server {
    listen 8080;
    location / {
        if ($http_cookie ~* "mycookie") {
            proxy_set_header X-Request $request;
            proxy_pass http://localhost:8081;
        }
    }
}

I have this error when I reload nginx service:

Reloading nginx configuration: nginx: [emerg] "proxy_set_header" directive is not allowed here in /etc/nginx/conf.d/check_cookie.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed

This configuration works OK, but it does not do what I want:

server {
    listen 8080;
    location / {
        proxy_set_header X-Request $request;
        if ($http_cookie ~* "mycookie") {
            proxy_pass http://localhost:8081;
        }
    }
}

Why I can't put proxy_set_header directive inside an if clause?

like image 346
Neuquino Avatar asked May 11 '13 18:05

Neuquino


2 Answers

Inside location try something like this

# default header value in a new variable
set $esb "$remote_addr, $host";

# if my custom header exists
if ($http_my_header){
  set $esb "$http_my_header, $remote_addr, $host";
}

proxy_set_header my-header $esb;
like image 164
sisma Avatar answered Sep 22 '22 16:09

sisma


Unlike proxy_pass, you cannot put proxy_set_header inside an if block. You can only put it in http/server/location block. So your 2nd config is good.

Reference: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

context: http, server, location

Don't know what the $request variable is. It doesn't appear in nginx variable list: http://wiki.nginx.org/HttpCoreModule#Variables. What are you trying to achieve here?

like image 37
Chuan Ma Avatar answered Sep 24 '22 16:09

Chuan Ma