如何利用nginx通过正则拦截指定url请求

这篇文章主要介绍了如何利用nginx通过正则拦截指定url请求的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

nginx服务器

nginx是非常出色web服务器,对于静态文件的处理非常高效,同时它的代理转发功能和其它后台服务器搭配起来也非常的简单高效。

location

我们知道nginx会对请求进行解析,然后回得到关于请求的url等信息,我们只需要对url进行匹配,然后拦截即可。

匹配规则

location / {

if ($request_uri ~* ^/?http(.*)$) {

return 404;

}

}

经过这样的匹配,我们就可以拦截所有请求根目录的网址并且参数为?httpxxx类似的请求都会显示404.

防盗链

返回http代码,例如设置nginx防盗链:

location ~* .(gif|jpg|png|swf|flv)$ {

valid_referers none blocked www.80shihua.com www.menghuiguli.com;

if ($invalid_referer) {

return 404;

}

}

nginx常用变量

nginx解析出很多我们常用的变量,我们只需要拿过来使用即可,下面就是nginx常用的变量。具体使用方法,可以参考官方文档。

$content_length

$content_type

$cookie_

$date_gmt

$date_local

$document_root

$document_uri

$fastcgi_path_info

$fastcgi_script_name

$gzip_ratio

$host

$hostname (ngx_http_core_module)

$hostname (ngx_stream_core_module)

$http2

$http_

$protocol

$proxy_host

$proxy_port

$query_string

$realpath_root

$request

$request_body

$request_uri

$scheme

$server_name

$uri

总结

到此这篇关于如何利用nginx通过正则拦截指定url请求的文章就介绍到这了,希望大家以后多多支持!

Various devices connected to a network attached storage. Digital illustration.

标签

发表评论