Redirect Users To Different Domains Of The Same Web Application According To Their Ip Address Location
I have to internationalize my web application to different countries i have 4 domains corresponding to each country how can i use nginx to detect visitor ip address and redirect them to the correct domain by their ip and if they join from another country which i don't support redirect them to my "default domain"
I have the current nginx.conf file
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
charset_types text/xml text/plain text/vnd.wap.wml application/x-javascript application/rss+xml text/css application/javascript application/json;
passenger_root /Library/Ruby/Gems/2.0.0/gems/passenger-5.0.24;
passenger_ruby /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby;
geoip_country /usr/local/nginx/GeoIP.dat;
geoip_city /usr/local/nginx/GeoLiteCity.dat;
fastcgi_param GEOIP_ADDR $remote_addr;
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_REGION_NAME $geoip_region_name;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_AREA_CODE $geoip_area_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
map $geoip_country_code $subdomain {
default es;
RU ru;
ES es;
DE de;
}
#gzip on;
server {
listen 80;
server_name www.myserver.com;
root /usr/local/nginx/html;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
root /usr/local/nginx/html;
index index.php;
fastcgi_index index.php;
include fastcgi_params;
}
}
upstream ru.server {
server www.example1.com;
}
upstream en.server {
server www.example2.com;
}
upstream default.server {
server wwww.example3.com;
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
How can i use the
rewrite ^ $scheme://$subdomain.my-domain.com$request_uri? permanent;
Answer
You can use GeoIP module - http://nginx.org/en/docs/http/ngx_http_geoip_module.html
In your config create a map like this
map $geoip_country_code $subdomain {
default en;
RU ru;
ES es;
DE de;
}
Then you can redirect, but an actual rule depends on your server-side. Example:
http {
# ...
map $geoip_country_code $subdomain {
default en;
RU ru;
ES es;
DE de;
}
server {
listen 80;
server_name www.myserver.com;
location / {
rewrite ^ $scheme://$subdomain.myserver.com$request_uri? permanent;
}
}
}
Obviously there should be a config for every subdomain (en.myserver.com
, ru.myserver.com
, ...). It can be one server section for one subdomain or for all of them at once.
server {
server_name en.myserver.com, ru.myserver.com, ...
}
When user visits your http://www.myserver.com/
GeoIP matches its IP, then map
maps country code (let it be 'RU') to a subdomain. After that you have a string with subdomain in your $subdomain
variable. Then you can use that variable as you want. rewrite
makes redirect from current location to $subdomain.myserver.com
which is actually ru.myserver.com
in our example.
default
in map
is for default value, if none from the left side was matched.
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?