【问题标题】:Redirect users to different domains of the same web application according to their ip address location根据用户的 IP 地址位置将用户重定向到同一 Web 应用程序的不同域
【发布时间】:2016-02-11 23:18:11
【问题描述】:

我必须将我的 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;

【问题讨论】:

  • 当您说“国际化”时,您是指以不同语言呈现内容吗?如果是这样,那么基于IP的做法是相当无稽之谈——我坐在中国的网吧里并不意味着我会说中文。 Accept-Language 标头是您应该查看的第一件事。而且您仍然应该允许用户自己选择语言(切换到不同的版本,而不会再次被自动重定向。)
  • 不,我实际上想重定向到不同的 nginx 服务器、不同的域等,除了由 php 中的 i18 库控制的语言

标签: php laravel nginx docker


【解决方案1】:

您可以使用 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 是默认值,如果左侧没有匹配。

【讨论】:

  • 我对 nginx 有点陌生,这个重写规则应该添加到配置文件中还是在哪里?
  • 您的 .conf 文件应该保存更改
  • 我正在使用 docker 设置环境,有什么线索可以在这种情况下更改配置文件吗?
  • 我认为你应该使用映射到docker的本地配置,比如docker run --name some-nginx -v /some/nginx.conf:/etc/nginx/nginx.conf:ro
  • 我已经有一个容器,但我没有自己制作,所以我怎么能在现有的 docker 容器上编辑配置文件?
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
  • 2020-03-28
相关资源
最近更新 更多