使用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

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

8条评论
  • ValfKeeli

    2022年7月28日 00:34

    Buy Silagra 100 Mg cialis buy online

  • Obedoms

    2022年8月16日 13:07

    stromectol walmart Zithromax And Birth Control

  • Haintailk

    2022年9月1日 02:48

    In clinical trials of once-daily tadalafil, the most commonly reported adverse events were headaches, indigestion, back pain, muscle aches, nasal congestion, flushing and limb pain best site to buy cialis online

  • optonse

    2022年9月3日 06:09

    Stop taking VIAGRA and call your healthcare provider right away if you have any sudden vision loss sudden hearing decrease or hearing loss buy priligy pills Emperor Liu s idea and practice, if you want to find a template among the previous emperors, the most similar one is Emperor Yang of Sui, but compared to Emperor cheap ed meds online Yang of Sui, Emperor cheap ed meds online Liu faced much less difficulty

  • Issueks

    2022年9月5日 04:33

    Would you like to see someone who is not as good as you do this Chu many cialis 20mg pill Ride Male Enhancement Pill dead people, want them to destroy the country again If Gu Huo changed to the past, it s penis enlargement pill in united states texas dallas a big deal to say that you will see people s hearts after a long time, you can see if I am a good emperor after a while, but now the time is cialis 20mg pill urgent, taking male enhancement pills at young age and there is no delay cialis coupons Effects of VIAGRA on Blood Pressure When Co-administered with Alpha-Blockers

  • absotatot

    2022年9月7日 06:56

    buy cialis online us Historically, a limited understanding of the physiological mechanism of erections restricted the treatment of ED to vacuum-constriction devices, prosthetic implants, intracavernosal injections, and intraurethral suppositories

  • autolla

    2022年9月10日 04:30

    Kisspeptin signaling pathway is increasingly recognized as essential for normal puberty, gonadotropin secretion, and regulation of reproduction 65-67. buy clomid for men

  • mumClulky

    2022年9月13日 06:34

发表评论

您的电子邮箱地址不会被公开。