工作小总结&小工具类

访问网站需要白名单?是用nginx来解决

在访问A网站的时候,需要白名单,于是想着,能不能在nginx上配置解决这个问题呢?因为凯哥有个公网IP,是固定的,把公网ip配置到A网站白名单中,然后是用nginx来解决的。直接上代码: set $nginx_public_ip "123.123.123.123"; # 请写入你添加A网站的白

在访问A网站的时候,需要白名单,于是想着,能不能在nginx上配置解决这个问题呢?

因为凯哥有个公网IP,是固定的,把公网ip配置到A网站白名单中,然后是用nginx来解决的。

直接上代码:

    set $nginx_public_ip "123.123.123.123"; # 请写入你添加A网站的白名单ip  

		# 配置
		location /myapi/ {
                # 1. 保存原始参数
        set $orig_args $args;
        
        # 2. 构建新参数 - 使用变量避免$符号问题
        set $fixed_params "appkey=123456&pid=mm_49813693_359950439_100059950089&name=heihei        
        # 3. 合并参数(处理空参数情况)
        if ($orig_args = "") {
            set $args $fixed_params;
        }
        if ($orig_args != "") {
            set $args $orig_args&$fixed_params;
        }
        
        # 4. 路径转换 - 使用rewrite但保留参数
        rewrite ^/myapi/(.*)$ /$1?$args break;
        
        # 5. 代理设置
        proxy_pass  # 这里是需要访问的A网站域名
        
        # 6. IP白名单处理
        proxy_set_header Host a.test.cn; # 设置访问头
        proxy_set_header X-Real-IP $nginx_public_ip;
 

原创不易,完成人机校验,阅读全文

相关推荐