Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
    
Please don't cross-post. stackoverflow.com/questions/16500594/… –  ceejayoz May 11 '13 at 19:14
    
I opened a chat to discuss about this. We can continue the discussion there: chat.stackexchange.com/rooms/8745/nginx –  Neuquino May 12 '13 at 14:54

2 Answers 2

up vote 3 down vote accepted

Assuming you actually meant to ask, 'how can I get this to work', how about just re-writing so the header is always passed, but has it set to some ignored value if you don't want it set.

server {
    listen 8080;    
    location / {
        set $xheader "someignoredvalue";

        if ($http_cookie ~* "mycookie") {
            set $xheader $request;
        }

        proxy_set_header X-Request $xheader;

        if ($http_cookie ~* "mycookie") {
            proxy_pass http://localhost:8081;
        }
    }
share|improve this answer
    
You mean "", right? –  Michael Hampton May 12 '13 at 2:33
    
I personally prefer to set things to be obviously not a real value, rather than potentially forgetting that this hack was in place, and then wondering why the header was empty. If it's set to "X-Header-not-set-by-nginx" then you're never going to be confused. –  Danack May 12 '13 at 2:51

'If' is generally a bad practive in nginx configuration. You can use map module to make things work. see http://nginx.org/en/docs/http/ngx_http_map_module.html http://wiki.nginx.org/HttpMapModule

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.