我必须将我的 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 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
in map
如果左侧没有匹配,则为默认值。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)