我必须将我的网络应用程序国际化到不同的国家/地区我有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;
答案 0 :(得分: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.com
,ru.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
。
default
中的{p> map
是默认值,如果左侧没有匹配的话。