Often we see many enterprise websites redirect users to country specific subdomain. Here is How To Setup Nginx WordPress to Redirect to Geographically Closer Server. This Setup is Known as Geo Redundant Setup of WordPress. This is only practical if your website has lot of traffic from various regions and you need to decrease load. There are two matters involved – one is setting Nginx as Traffic Director, second is setting each WordPress setup to sync with each other.
What Backend Needed to Redirect to Geographically Closer Server With Nginx WordPress?
First, ideally you need a Geo Redundant DNS like dyn with Traffic Redirector, Nginx Plus (contact either Nginx or us) and a CDN with lot of PoP like KeyCDN. Actually Dyn has higher DBS service to work as Traffic Director themselves, that is not available with the normal paid account. If you do not use Dyn’s Traffic Director and Nginx Plus still you can follow our guide. You need a good DNS and Nginx community edition as minimum need.
For this guide, you need to follow the products we are suggesting. Otherwise page speed will be more slower. You need virtual servers in Europe from Aruba Cloud (because Aruba is bigger Italian brand with datacenter in UK and other areas), VPSDime as main server in US. This is normally enough – Global at US and one cluster in Europe. You can add one node in Malaysia server for Asia. You can check WebHostingTalk
, LowEndBox
websites for cheaper, unmanaged servers with 1Gbps to 10 Gbps network connection. Using DigitalOcean, Amazon etc will result in very slow result because their datacenter in each region are not exactly fast. Except Dyn, we suggested cheaper servers. CDN is a normal need.
---
As commonly we use W3 Total Cache Plugin to create cached HTML files and serve static files with CDN, you need to calculate the profit with the amount of work. We have to sync WordPress MySQL database with main server as well as the FTP content. For FTP live data replication using lsyncd solves the matter.
WordPress and MySQL both has ways to sync MySQL database. If you use a specific function (like a pop up for EU but not for the other areas, different design etc) for one geographical area, then you need to manually update the posts after initial setup. For our guide :
1 2 | Main server : thecustomizewindows.com, US; Incero colocation hosting EU server : eu.thecustomizewindows.com, Italy; Aruba Cloud |
From Dyn DNS or other DNS service, we have to point eu.thecustomizewindows.com
subdomain’s server in EU.
What Nginx Settings Needed to Redirect to Geographically Closer Server With Nginx WordPress?
This step is when you are not use any external traffic redirection service at DNS level. As first call is to US server (thecustomizewindows.com
located at US in our example), thereafter getting a redirection to EU, it is not exactly optimal. However, it is great for marketing purpose like serving specific Ads. We have a guide on Nginx Geo-IP setup, please follow it for all servers you’ll have.
Extra than that guide you need to create a special fast CGI cone file
(usually the normal file is /etc/nginx/fastcgiparams
) :
1 2 3 4 5 6 7 8 9 10 11 12 | fastcgiparam REDIRECTSTATUS 200; fastcgiparam GEOIPADDR $remoteaddr; fastcgiparam GEOIPCOUNTRYCODE $geoipcountrycode; fastcgiparam GEOIPCOUNTRYNAME $geoipcountryname; fastcgiparam GEOIPREGION $geoipregion; fastcgiparam GEOIPREGIONNAME $geoipregionname; fastcgiparam GEOIPCITY $geoipcity; fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code; #EDIT fastcgiparam GEOIPAREACODE $geoipareacode; fastcgiparam GEOIPLATITUDE $geoiplatitude; fastcgiparam GEOIPLONGITUDE $geoiplongitude; fastcgiparam GEOIPPOSTALCODE $geoippostal_code; |
You can name it as /etc/nginx/fastcgiparams_geo
and include in all server’s virtual hosts file where include fastcgiparam;
exists. You can add the above stuff to existing /etc/nginx/fastcgiparams
like file too as alternate way.
For our main server, thecustomizewindows.com
located at US, with two servers thecustomizewindows.com
and eu.thecustomizewindows.com
, we will map in this way (take it as master config) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | map $geoip_city_continent_code $closest_server { default thecustomizewindows.com; AF eu.thecustomizewindows.com; # EU is closer to Africa AS eu.thecustomizewindows.com; # EU is closer to Asia EU eu.thecustomizewindows.com; # Europe NA thecustomizewindows.com; # North America OC eu.thecustomizewindows.com; # Calculate if EU is closer to Oceania? SA thecustomizewindows.com; # US, NA is closer to South America } server { listen 80; listen [::]:80; root /var/www/domain.com/html; index index.php index.html index.htm; server_name thecustomizewindows.com; if ($closest_server != $host) { set $test A; } if ($host ~* $server_name) { set $test "${test}B"; } if ($test = AB) { rewrite ^ $scheme://$closest_server$request_uri break; } location / { try_files $uri $uri/ =404; } # rest of the config |
Obviously for EU server, the server_name
becoming server_name eu.thecustomizewindows.com;
. For the US server, you need slight modification by commenting out SA and NA :
1 2 3 4 5 6 7 8 9 | ... default thecustomizewindows.com; AF eu.thecustomizewindows.com; # EU is closer to Africa AS eu.thecustomizewindows.com; # EU is closer to Asia EU eu.thecustomizewindows.com; # Europe # NA thecustomizewindows.com; # North America OC eu.thecustomizewindows.com; # Calculate if EU is closer to Oceania? # SA thecustomizewindows.com; # US, NA is closer to South America ... |
For the EU server, you need slight modification from that master config :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ... default eu.thecustomizewindows.com; # AF eu.thecustomizewindows.com; # EU is closer to Africa AS eu.thecustomizewindows.com; # EU is closer to Asia # EU eu.thecustomizewindows.com; # Europe NA thecustomizewindows.com; # North America OC eu.thecustomizewindows.com; # Calculate if EU is closer to Oceania? SA thecustomizewindows.com; # US, NA is closer to South America } # notice the redirection to main server server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; server_name www.thecustomizewindows.com; return 301 http://thecustomizewindows.com$request_uri; } # notice the www redirection server { listen 80; listen [::]:80; server_name www.eu.thecustomizewindows.com; return 301 http://eu.thecustomizewindows.com$request_uri; } # notice the working server block server { listen 80; listen [::]:80; root /var/www/domain.com/html; index index.php index.html index.htm; server_name eu.thecustomizewindows.com; |
Run nginx -t
and service nginx restart
. Test with WebPageTest to check the regions and page speed. We gave a cost effective solution, but this may not be a secure setup.