使用openresty根据源ip重定向海外用户访问海外服务器

首先用rust写一个小工具

https://github.com/liu-wh/check-ip-source

接着我们安装openresty

wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/openresty.repo
sudo yum -y install openresty

默认配置文件在/usr/local/openresty/nginx/conf/nginx.conf

 location   / {
      limit_except GET {
        deny  all;
      }

         if ($http_x_forwarded_for = "" ) {
                return 200 "hello world";
           }
           set_by_lua $address '
             local ngx = require "ngx"
             local s = ngx.var.http_x_forwarded_for
             local j = io.popen("/app/shell/rust-ipcheck \'".. s .."\'")
             local x = j:read("*a")
             j.close()
             return x;
        ';
        if ($address ~ '^se$') {
            rewrite ^/(.*) https://xxxxx/$1 redirect;
        }
        root /data/centos7-1810/;
        autoindex       on;
        autoindex_exact_size    off;
        autoindex_localtime     on;
 }

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注