根据用户的 IP 地址位置将用户重定向到同一 Web 应用程序的不同域


Redirect users to different domains of the same web application according to their ip address location

我必须将我的 Web 应用程序国际化到不同的国家/地区,我每个国家/地区有 4 个域,如何使用 nginx 检测访问者 IP 地址并通过他们的 IP 将他们重定向到正确的域,如果他们从另一个我不支持的国家/地区加入,请将它们重定向到我的"默认域"

我有当前的 nginx.conf 文件

#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;
#    }
#}

}

如何使用

rewrite ^ $scheme://$subdomain.my-domain.com$request_uri? permanent;

您可以使用GeoIP模块 - http://nginx.org/en/docs/http/ngx_http_geoip_module.html

在您的配置中创建这样的地图

map $geoip_country_code $subdomain {
    default en;
    RU ru;
    ES es;
    DE de;
}

然后,您可以重定向,但实际规则取决于您的服务器端。例:

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;
        }
    }
}

显然,每个子域都应该有一个配置(en.myserver.comru.myserver.com,...)。它可以是一个子域的一个服务器部分,也可以是一次用于所有子域的服务器部分。

server {
    server_name en.myserver.com, ru.myserver.com, ...
}

当用户访问您的http://www.myserver.com/ GeoIP 与其 IP 匹配时,map将国家/地区代码(保留为"RU")映射到子域。之后,您的$subdomain变量中有一个带有子域的字符串。然后,您可以根据需要使用该变量。 rewrite从当前位置重定向到$subdomain.myserver.com这在我们的示例中实际上是ru.myserver.com的。

map中的default是默认值,如果左侧没有匹配