NGINX 根据geoip来源IP跳转

模块

--with-http_geoip_module

官网

GeoIP and GeoLite | MaxMind Developer Portal

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区块

3.相关工具

GeoLiteCity.dat GeoIP.dat

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值