0

I need to rewrite any root subdomain requests and append locale params if they aren't already there. e.g. -> de.example.com needs to be rewritten as -> de.example.com/?locale=de. then I proxy it off to the app.

2 questions:

1) is this the correct approach? or should I be using regex instead here? (new to this so if other problems, please lmk)

2) is there a way to log things inside the location block? Having trouble getting same config working on another server, logging would help. (e.g logging what args is if it isn't matching, or if it matches on another location block). It only needs to happen on the root page so this is my current config

#existing default (nonsubdomain block)
server {
 server_name _;
 root /var/www/web_app;
 try_files $uri/index.html $uri.html $uri @app;
 location @app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
      }
}

#just added for subdomain 
server {

  server_name de.example.com;
  root /var/www/web_app;

  location / {
  try_files $uri/index.html $uri.html $uri @app;
 }  
  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
  location = / {
   if ($args != locale=de ){ 
    rewrite ^ $scheme://de.example.com/?locale=de permanent;
    }
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
}
2
  • did you indicate that there're multiple subdomains? like de.example.com, en.example.com, etc Commented Nov 15, 2013 at 0:55
  • only one subdomain inside this server block. there is another server block (edited, now above) w/ which uses server_name _; but my understanding was that if the hostname request matched, it would use this block as opposed to the less specific (this one) Commented Nov 15, 2013 at 1:45

1 Answer 1

1

1) is this the correct approach? or should I be using regex instead here? (new to this so if other problems, please lmk)

You should use $arg_locale != de instead of $args != locale=de. Look at the docs: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_

2) is there a way to log things inside the location block? Having trouble getting same config working on another server, logging would help. (e.g logging what args is if it isn't matching, or if it matches on another location block). It only needs to happen on the root page so this is my current config

Debug log: http://nginx.org/en/docs/debugging_log.html

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

Comments

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.