分类 默认分类 下的文章

搞了个github的代理。国外能访问的话,老被VPS告我侵权。直接改成仅国内可访问就得了。
反正国外本来也不用代理。

1,安装geoip库

yum -y install geoip-devel

2,重新编译nginx,添加http_geoip_module模块

先查看原来的参数。
/usr/local/nginx/sbin/nginx  -V

nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44.0.3) (GCC) 
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1k --with-pcre=../pcre-8.44 --with-pcre-jit --with-ld-opt=-ljemalloc

重新编译:
cd /usr/local/src/nginx-1.20.1
#注意,同一级的PCRE和openssl的目录也要存在。
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.1.1k --with-pcre=../pcre-8.44 --with-pcre-jit --with-ld-opt=-ljemalloc --with-http_geoip_module=dynamic

之后,只make,不要make install,因为会覆盖当前nginx的目录

make

3,拷贝新编译的nginx和ngx_http_geoip_module.so到nginx安装目录下

#备份,防止出错
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

cp /home/opc/oneinstack/src/nginx-1.20.1/objs/nginx /usr/local/nginx/sbin/

mkdir /usr/local/nginx/modules

cp /home/opc/oneinstack/src/nginx-1.20.1/objs/ngx_http_geoip_module.so /usr/local/nginx/modules/

4,配置规则
nginx.conf配置文件添加lngx_http_geoip_module.so模块
注意,这里要添加到nginx配置文件的顶级目录,
我这里的两个位置都是可以的,但是放在events和http中间就报错。


#load_module modules/ngx_http_geoip_module.so;
user www www;
worker_processes auto;

error_log /data/wwwlogs/error_nginx.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

load_module modules/ngx_http_geoip_module.so;
events {
  use epoll;
  worker_connections 51200;
  multi_accept on;
}

http {
......
}

5,nginx.conf中添加配置,这个要添加到HTTP段。

    # 访问地理位置限制规则
    geoip_country /usr/share/GeoIP/GeoIP.dat;
 
    # geoip_city    /usr/share/GeoIP/GeoLiteCity.dat;
    # 下面一行根据实际情况编写
    map $geoip_country_code $allowed_country {
        default no;
        CN yes;
    }

6,在站点的配置规则中,添加相关配置:

server {
    listen 80;  
    server_name xx.xx.xx; 
 
    #添加判断
    if ($allowed_country = no) {
        return 403;
    }
    
}

参考:https://www.gaoxiaobo.com/web/server/148.html

总结,设置代理要用小写字母:

export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809

测试过程非常曲折。先说明我这个代理是全局代理。
1,首先我都是用大写来设置的:

export HTTP_PROXY=http://127.0.0.1:10809
export HTTPS_PROXY=http://127.0.0.1:10809

结果curl http://cip.cc 返回真实IP
结果curl https://cip.cc 返回代理IP

2,这次尝试curl -x http://127.0.0.1:10809 http://cip.cc
这次返回的是代理IP
说明本地搭的代理是没问题的。
设置的代理变量HTTP_PROXY失效了。

3, 重新用小写设置一次,

export http_proxy=http://127.0.0.1:10809

这次curl http://cip.cc
终于返回了代理的IP。

但是此时环境变量里面有3个代理设置:

HTTPS_PROXY=http://127.0.0.1:10809
HTTP_PROXY=http://127.0.0.1:10809
http_proxy=http://127.0.0.1:10809

也就是说此时:
HTTPS_PROXY是生效的。
HTTP_PROXY是不生效的。
http_proxy是生效的。

后来经过测试:
https_proxy也是可以生效的。

所以,为了统一,还是都用小写的吧。于是有了开头结论:

export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809

后记:
测试WINDOWS,正好相反。WINDOWS只认大写,不认小写。WINDOWS命令:

set HTTPS_PROXY=http://127.0.0.1:10809
set HTTP_PROXY=http://127.0.0.1:10809

上述测试环境:
Ubuntu 22.04.1 LTS
Debian 10
Windows 11 专业版

一直都不是很熟悉。今天误操作了一下。记录一下:
清空iptables
iptables -F

备份iptables
iptables-save > iptables_bak.txt

恢复
iptables-restore < iptables_bak.txt

允许某个特定IP访问某个端口,常见于反代。

iptables -A INPUT -p tcp --dport 8999 -s V.P.S.IP -j ACCEPT  //允许VPS的IP访问
iptables -A INPUT -p tcp --dport 8999 -j DROP  //不允许其他机器的访问

-A Append,追加在最后。但是如果最后一条是REJECT ALL。那么当添加ACCEPT的时候,就要添加在前面规则的中间,
这时候就要用-I insert。插入到中间。

基本操作就是:
iptables -A|I