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

How to build LNMP environment on Ubuntu 20.04

Simple description Since it was built with Centos...

Solve the problem of HTML automatic formatting after saving in vscode

The version of vsCode has been updated in recent ...

js canvas realizes slider verification

This article example shares the specific code of ...

How to delete extra kernels in Ubuntu

Step 1: View the current kernel rew $ uname -a Li...

webpack -v error solution

background I want to check the webpack version, b...

Summary of common tool functions necessary for front-end development

1. Time formatting and other methods It is recomm...

JavaScript to achieve full or reverse selection effect in form

This article shares the specific code of JavaScri...

The benefits of div+css and web standard pages

The div element is used to provide structure and b...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Solution to Docker image downloading too slowly

Docker image download is stuck or too slow I sear...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...

The implementation principle of Mysql master-slave synchronization

1. What is MySQL master-slave synchronization? Wh...

Vue uses rules to implement form field validation

There are many ways to write and validate form fi...