使用nginx通过正则拦截指定url请求

作者: dreamfly 分类: 服务器 发布时间: 2020-05-14 13:19

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

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

评论已关闭!