11566.cn 11566.cn

欢迎光临
我们一直在努力
顶部
域名

Nginx禁止部分UserAgent访问的问题解决_nginx_

前言

  • 有时候域名在微信上 访问会红,会有各种异常提示,通过nginx禁止域名在微信上被访问,给出友好提示。
  • 如果红了的域名,加上后过段时间就会恢复,并且正常显示配置的页面。
  • 虽然不能直接在微信 访问,但是总比在微信上报异常链接友好。

一、使用步骤

1.关键代码

检测是否微信、企鹅中访问,如果是$is_blocked_agent返回1

map $http_user_agent $is_blocked_agent {
        default                 0;
        "~*micromessenger"      1;
        "~*QQTheme"             1;
}
# 检查变量
if ($is_blocked_agent = 1) {
    rewrite ^ /error_blocked_agent.html last;
}

# 定义错误页面位置
location = /error_blocked_agent.html {
    internal; # 防止外部直接访问
    root /usr/local/nginx/html/; # 指定网页文件的根目录
    try_files $uri  error_blocked_agent.html last; 
}

页面路径:
/usr/local/nginx/html/error_blocked_agent.html




    
    请使用浏览器打开
    
    


请继续操作

  • 1、点击右上角
  • 2、选择『在浏览器/Safari中打开』
复制链接

2.使用方法

  • 全局使用:将检测UserAgent代码放在http块,在每server块中都可以使用$is_blocked_agent变量
# *** 其它配置
events {
    # *** 其它配置
}
http {
	# *** 其它配置

	#检测User Agent
	map $http_user_agent $is_blocked_agent {
	        default                 0;
	        "~*micromessenger"      1;
	        "~*QQTheme"             1;
	}

 server {
 	# ***其它配置

	# 检查变量
	if ($is_blocked_agent = 1) {
	    rewrite ^ /error_blocked_agent.html last;
	}
	
	# 定义错误页面位置
	location = /error_blocked_agent.html {
	    internal; # 防止外部直接访问
	    root /usr/local/nginx/html/; # 指定网页文件的根目录
	    try_files $uri  error_blocked_agent.html last; 
	}
 }
}
  • 局部使用:将检测UserAgent放在server 块,其它server块无法使用变量$is_blocked_agent
# *** 其它配置
events {
    # *** 其它配置
}
http {
	# *** 其它配置
 server {
 	# ***其它配置

	#检测User Agent
	map $http_user_agent $is_blocked_agent {
	        default                 0;
	        "~*micromessenger"      1;
	        "~*QQTheme"             1;
	}
	
	# 检查变量
	if ($is_blocked_agent = 1) {
	    rewrite ^ /error_blocked_agent.html last;
	}
	
	# 定义错误页面位置
	location = /error_blocked_agent.html {
	    internal; # 防止外部直接访问
	    root /usr/local/nginx/html/; # 指定网页文件的根目录
	    try_files $uri  error_blocked_agent.html last; 
	}
 }
}

二、效果

在这里插入图片描述

在这里插入图片描述

三、总结

优点

  • 对业务零入侵。
  • 域名下的所有路径都会被强制禁止在目标UserAgent访问。

缺点

  • 需要在nginx额外加配置代码。

到此这篇关于Nginx禁止部分UserAgent访问的问题解决的文章就介绍到这了,更多相关Nginx禁止UserAgent内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持! 

【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。
发布内容
-六神源码网 -六神源码网