💡 需求:
1.国内用户访问英德文站跳转统一跳转zk.cn
2.国外用户访问固定站 en.zk.com/de.zk.com ( 未作调整)
模块 | —with-http_geoip_module |
---|---|
官网 | https://dev.maxmind.com/ |
1.编译安装Nginx
参考:https://www.cnblogs.com/clsn/p/7750615.html
2.Nginx配置
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
geoip_country /server/tools/nginx-1.20.1/geoip/GeoIP.dat;
geoip_city /server/tools/nginx-1.20.1/geoip/GeoLiteCity.dat;
##############################################################测试 #################################################################
server {
listen 81;
server_name localhost;
root /server/tools/nginx-1.20.1/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
if ($geoip_country_code = CN) {
root /server/tools/nginx-1.20.1/html;
}
if ($geoip_country_code != CN) {
root /server/tools/nginx-1.20.1/html/test;
}
}
}
##############################################################英文 官网#################################################################
server {
listen 443 ssl;
server_name www.zk.com en.zk.com;
root /ftpshare;
index index.php index.html;
ssl_certificate /server/tools/nginx-1.20.1/ssl/6543710__zk.com.pem;
ssl_certificate_key /server/tools/nginx-1.20.1/ssl/6543710__zk.com.key;
ssl_session_timeout 5m; #缓存有效期
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法
location / {
root /ftpshare;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
if ($geoip_country_code = CN) {
rewrite ^/(.*) https://www.zk.cn;
}
}
location ~* .*\.(jpg|gif|png)$ {
root /ftpshare;
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
}
location ~ .*\.(jpg||jpeg|gif|png)$ {
valid_referers none blocked test.zk.com;
if ($invalid_referer) {
return 403;
}
}
location ~ \.php$ {
root /ftpshare;
index index.php index.html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /ftpshare$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name www.zk.com zk.com;
return 301 https://$server_name$request_uri;
}
##############################################################德文 官网#################################################################
server {
listen 443 ssl;
server_name de.zk.com;
root /usr/share/nginx/html;
index index.php index.html;
ssl_certificate /server/tools/nginx-1.20.1/ssl/6543710__zk.com.pem;
ssl_certificate_key /server/tools/nginx-1.20.1/ssl/6543710__zk.com.key;
ssl_session_timeout 5m; #缓存有效期
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #加密算法
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #安全链接可选的加密协议
ssl_prefer_server_ciphers on; #使用服务器端的首选算法
location / {
root /usr/share/nginx/html;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
if ($geoip_country_code = CN) {
rewrite ^/(.*) https://www.zk.cn;
}
}
location ~* .*\.(jpg|gif|png)$ {
root /usr/share/nginx/html;
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/json application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png;
}
location ~ .*\.(jpg||jpeg|gif|png)$ {
valid_referers none blocked test.zk.com;
if ($invalid_referer) {
return 403;
}
}
location ~ \.php$ {
root /usr/share/nginx/html;
index index.php index.html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
} #http区块