How to do dynamic path routing requests to single root directory and url to /<country_zone>/<actual_url>/*
.
I am able to retrive $country_zone
value which can be any of (in|uk|us|other)
using geoip module.
e.g
Request => Response
/ => /
/ (with geoip country as US) => /us/
/path/ => /path
/path/ (with geoip country as US) => /us/path/
/us/path/ => /us/path/
/us/path/ (with geoip country as US) => /us/path/
/in/* => /*
My codebase is at : /var/www/html (All routes should consider this as a root directory after /
My current nginx config :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location ~ ^/$country_zone {
alias /var/www/html;
index index.html index.htm index.nginx-debian.html;
break;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
if ($country_zone != in) {
return 307 /$country_zone;
}
add_header X_COUNTRY_ZONE_ID $country_zone;
try_files $uri $uri/ =404;
}
}
PS : I will also proxy the request to another server some day