0

I have difficulties to apply this if statement in my nginx config:

location / {
    break;
    proxy_pass                  http://127.0.0.1:4444;
    proxy_redirect              off;
    proxy_set_header            Host                        $host;
    proxy_set_header            X-Real-IP               $remote_addr;
    proxy_set_header            X-Forwarded-For         $proxy_add_x_forwarded_for;

    if ($arg_charset ~* windows-1251) {
        charset                     windows-1251;
    }
    source_charset              utf-8;
}

I have tried $arg_charset ~* "windows-1251" and $arg_charset ~* /.windows-1251./ and all others solutions. None does work... Removing if statements give me wanted results, so the problem is inside if statement condition.

It's a bug or i'm doing it wrong?

2
  • if ( $arg_charset ) doesn't work as well. Changed to location ~* ^.*$ and it's not helping as well. Commented Oct 27, 2012 at 11:26
  • using url?charset=windows-1251 Commented Oct 27, 2012 at 11:31

1 Answer 1

4

As http://wiki.nginx.org/IfIsEvil explains, if blocks inside a location are known to be potentially problematic when they contain anything other then a return or rewrite

try simply moving the if into the server block directly (where if has no problems) that should fix it.

updated to avoid using charset directive in serverblock:

try something like the following:

set $requestedcharset utf-8;
if ($arg_charset ~* windows-1251) { set $requestedcharset windows-1251;}

location / {
  source_charset utf-8;
  charset $requestedcharset;
  #add in the rest of your / config
}

NOTE: make sure you have the win-utf charset_map included in your http block (on my debian system that means include /etc/nginx/win-utf;)

Sign up to request clarification or add additional context in comments.

5 Comments

The problem is that charset directive is not allowed in server context... I guess i'll have to try urls insteal of GET parameters and build separate location block. Btw can i include GET parameters inside location statement?
nginx.org/en/docs/http/ngx_http_charset_module.html#charset has server as allowed context for charset, so that should work
Yea but without if statement, what it not a solution for me. I'll try your solution you have updated.
It's working, but it doesn't return utf-8 back if there is no charset variable in the query or it's not windows-1251. Charset is always windows-1251...
Creating separate location blocks with different charset in them works. It's a pain in the ass...really... :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.