How to use nginx to intercept specified URL requests through regular expressions

How to use nginx to intercept specified URL requests through regular expressions

nginx server

nginx is an excellent web server that is very efficient in processing static files. At the same time, its proxy forwarding function is also very simple and efficient when combined with other background servers.

location

We know that nginx will parse the request and then get information about the requested URL. We only need to match the URL and then intercept it.

Matching rules

location / {
  if ($request_uri ~* ^/\?http(.*)$) {
         return 404;
     }
  }

After such matching, we can intercept all requests to the root directory URL and the parameter is ?httpxxx. Similar requests will display 404.

Anti-hotlink

Returns the http code, for example, to set up nginx anti-hotlinking:

location ~* \.(gif|jpg|png|swf|flv)$ {
  valid_referers none blocked www.80shihua.com www.menghuiguli.com;
  if ($invalid_referer) {
    return 404;
  }
}

nginx common variables

nginx parses out many variables that we commonly use. We just need to use them. The following are the commonly used variables of nginx. For specific usage, please refer to the official documentation.

$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

Summarize

This is the end of this article on how to use nginx to intercept specified URL requests through regular expressions. For more relevant content about nginx intercepting specified URL requests through regular expressions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use nginx to block a specified interface (URL)
  • Nginx dynamically forwards to upstream according to the path in the URL
  • Practical experience of implementing nginx to forward requests based on URL
  • Introduction to the difference between adding or not adding / after the URL in nginx proxy_pass reverse proxy configuration
  • Solution to the problem of not being able to use Chinese URLs under Nginx
  • Detailed explanation of nginx configuration URL redirection-reverse proxy
  • Detailed explanation of nginx rewrite and location according to URL parameters
  • How to set up URL link in Nginx server

<<:  Vue3+script setup+ts+Vite+Volar project

>>:  How to completely delete the MySQL 8.0 service under Linux

Recommend

Troubleshooting the cause of 502 bad gateway error on nginx server

The server reports an error 502 when synchronizin...

Who is a User Experience Designer?

Scary, isn't it! Translation in the picture: ...

6 inheritance methods of JS advanced ES6

Table of contents 1. Prototype chain inheritance ...

jQuery implements a simple comment area

This article shares the specific code of jQuery t...

Vue uses Canvas to generate random sized and non-overlapping circles

Table of contents Canvas related documents Effect...

Detailed explanation of MySQL precompilation function

This article shares the MySQL precompilation func...

Basic usage of custom directives in Vue

Table of contents Preface text 1. Global Registra...

Solution to the problem that Docker container cannot be stopped or killed

Docker version 1.13.1 Problem Process A MySQL con...

Three steps to solve the IE address bar ICON display problem

<br />This web page production skills tutori...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

Problems encountered by MySQL nested transactions

MySQL supports nested transactions, but not many ...

Django online deployment method of Apache

environment: 1. Windows Server 2016 Datacenter 64...

Detailed explanation of how to use zabbix to monitor oracle database

1. Overview Zabbix is ​​a very powerful and most ...

MySQL extracts Json internal fields and dumps them as numbers

Table of contents background Problem Analysis 1. ...