Nginx restricts IP access to certain pages

Nginx restricts IP access to certain pages

1. To prohibit all IP addresses from accessing the three pages a1.htm, a2.htm, and a3.htm, you can write the following in location

location ~* /(a1.htm|a2.htm|a3.htm)$ {
 deny all;
 condition………;
}

2. Only the specified IP is allowed to access the three pages a1.htm, a2.htm, and a3.htm. Access from other IPs is denied.

location ~* /(a1.htm|a2.htm|a3.htm)$ {
 allow 10.0.0.2;
 deny all;
 condition………;
}

With this setting, only the host with IP address 10.0.0.2 can access these three pages, and other IP addresses are rejected.

The same applies to other situations.

For example, I need to specify that only the IP address 8.8.8.8 can access the info.php page. Then you can add the following configuration in nginx-server

If the info.php page is accessed from a URL other than 8.8.8.8, a 403 error is returned.

You need to add the jump address at the end, proxy_pass http://192.168.1.110:10480; otherwise there will be a 404 error.

 location ~/info.php$ {

 if ($remote_addr != '8.8.8.8' ) {
 return 403;
 }
 proxy_pass http://192.168.1.110:10480;
 }
}

You can also add it in the server code

location ~/info.php$ {
 allow 8.8.8.8;
 deny all;
 condition………;
}

Same effect

How to configure and disable IP or IP segment?

The following instructions assume that the nginx directory is in /usr/local/nginx/

First, create a configuration file blockips.conf for blocking IP, then edit this file with vi blockips.conf and enter the IP to be blocked in the file.

deny 1.2.3.4;
deny 91.212.45.0/24;
deny 91.212.65.0/24;

Then save this file, open the nginx.conf file, and add the following line of configuration in the http configuration section:

include blockips.conf;

Save the nginx.conf file, and then test whether the current nginx configuration file is valid:

/usr/local/nginx/sbin/nginx -t

If there is no problem with the configuration, it will output:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful

If there is a problem with the configuration, you need to check where the syntax problem is. If there is no problem, you need to execute the following command to let nginx reload the configuration file.

/usr/local/nginx/sbin/nginx -s reload

Only allow certain IPs to access the page, or prohibit certain IPs from accessing the page

server_name es.mila66.com;
 location / {
 include /etx/nginx/all/ip.conf;
 deny all;

The file format in ip.conf is:

allow 192.168.1.11;
allow 192.168.1.12;

This allows only certain IP addresses to access the page.

If you want to prohibit certain IP addresses from accessing, you only need to modify it as follows: change allow to deny.

server_name es.mila66.com;
 location / {
 include /etx/nginx/all/ip.conf;
 allow all;

The file format in ip.conf is:

deny 192.168.1.11;
deny 192.168.1.12;

nginx -s reload

Restart the server

The above Nginx operation of restricting IP access to certain pages is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • How to use geoip to restrict regions in nginx
  • How to configure nginx to limit the access frequency of the same IP
  • Analysis of Nginx server's method of restricting access through ip and user_gent
  • How to handle malicious calls to SMS interface by nginx restricted IP
  • Nginx limits the number of visits and requests to a certain IP in the same time period. Example code
  • A complete analysis of various situations in which Nginx server restricts IP access
  • How to limit concurrent IP and download speed in Nginx
  • How to set up nginx and apache to limit concurrent IP access and limit IP connections
  • Limiting the number of short-term accesses to a certain IP based on Nginx

<<:  JavaScript offsetParent case study

>>:  Detailed explanation of the principles and implementation methods of Mysql account management

Recommend

Docker uses the Prune command to clean up the none image

Table of contents The creation and confusion of n...

Explain MySQL's binlog log and how to use binlog log to recover data

As we all know, binlog logs are very important fo...

mySql SQL query operation on statistical quantity

I won't say much nonsense, let's just loo...

Docker Stack deployment method steps for web cluster

Docker is becoming more and more mature and its f...

Vue implements two routing permission control methods

Table of contents Method 1: Routing meta informat...

Briefly describe the MySQL InnoDB storage engine

Preface: The storage engine is the core of the da...

Solution to MySQL Chinese garbled characters problem

1. The Chinese garbled characters appear in MySQL...

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, t...

Example of how to create a database name with special characters in MySQL

Preface This article explains how to create a dat...

Examples of using provide and inject in Vue2.0/3.0

Table of contents 1. What is the use of provide/i...

How to install WSL2 Ubuntu20.04 on Windows 10 and set up the docker environment

Enable WSL Make sure the system is Windows 10 200...

Detailed explanation of Vue components

<body> <div id="root"> <...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...