如何将变量附加到nginx中的每个请求?

Moo*_*oon 1 nginx url-rewriting

我有一种情况,我需要在 nginx 中的所有请求中附加一个国家/地区代码。

例如,如果用户访问http://example.com:3333/html/about,那么我应该重定向(在 nginx 中使用重写)到http://example.com:3333/html/about?country_code=en

我有以下重写,但我得到“太多循环”。

rewrite ^(.*)$ http://$host:3333/$1?country_code=en last;
Run Code Online (Sandbox Code Playgroud)

我如何解决它?

配置文件

server {
    ### USA specific config ###                                                                                                                                                                     
    if ($geoip_country_code = US) {
        # do something here for USA visitors;
        # root path /var/www/html/content/usa/;                                                                                                                                                     
        rewrite ^(.*)$ http://$host:3333/$1?country_code=en last;
    }
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*bov 5

if ($geoip_country_code = US) {
    set $test "US";
}
if ($arg_country_code != 'en') {
    set $test "{$test}_R";
}
if ($test = 'US_R') {
    rewrite ^(.*)$ http://$host:3333/$1?country_code=en last;
}
Run Code Online (Sandbox Code Playgroud)